在android中使用列表视图
我正在制作一个需要使用 ListView
的 Android 应用程序。我想添加一个显示“添加到列表”的菜单按钮,一旦用户按下该菜单按钮,它就会弹出一个弹出窗口,其中包含一个 TextView
、EditText
和两个 按钮,“确定”和“取消”。用户按下“Ok”后,
EditText
内的文本应添加到 ListView
中。取消按钮很明显。我还希望能够长按 ListView
项目来打开包含删除 Button
的弹出窗口。我想使用 XML 设计 ListView
屏幕。我怎样才能使这成为可能???请帮助我并提前非常感谢!到目前为止我正在使用此代码:
ListView Activity:
public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Main screen XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I am making an android application that needs to use a ListView
. I want to add a menubutton that says "Add to list" and once the user presses that menubutton, it pops up a popupwindow containing a TextView
, EditText
and two Buttons
, "Ok" and "Cancel". Once the user presses "Ok", the text inside the EditText
should be added to the ListView
. And the cancel Button
is obvious. I also want to be able to long press on a ListView
item to open a popupwindow containing a delete Button
. I want to design the ListView
screen using XML. How can i make this possible??? Please help me and thanks SO much in advance! I am using this code so far:
ListView activity:
public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Main screen XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您必须创建一个包含 listView 的布局。
您必须创建一个与列表视图的一行相对应的 xml lyout。
您必须创建一个适配器来填充要插入到 listView 中的数据
您必须在按钮上创建一个 onClickListener在列表中添加数据
如果您使用的是 ArrayAdapter 或 CursorAdapter,请添加新的
您创建到列表中的项目或适配器使用的光标以及
(
notifyDataSetChanged()
会自动调用),因此您的适配器将更新listview
来源:
http://developer.android.com/resources/tutorials/views/ hello-listview.html
上一个主题:Dynamic ListView in安卓应用
You have to create a layout containing a listView.
You have to create an xml lyout corresponding to one row of your list view.
You have to create an adapter which will populate data to insert in your listView
You have to create an onClickListener on your button to add data in your list
If you are using an ArrayAdapter or a CursorAdapter, add the new
item you created to the list or the cursor used by your adapter and
(
notifyDataSetChanged()
is automatically called), so your adapter will update thelistview
Source :
http://developer.android.com/resources/tutorials/views/hello-listview.html
Previous topic on it : Dynamic ListView in Android app
更多详细信息:- http://www.listviewinandroid.blogspot.in/
MainActivity.java
more details:- http://www.listviewinandroid.blogspot.in/
MainActivity.java
对于listview尝试用这种方式
1 Mainlayout
2 listitemlayout
3 MainActivity
4 AdapterClass
for listview Try with this way
1 Mainlayout
2 listitemlayout
3 MainActivity
4 AdapterClass
列表视图示例:
Sample List view:
适配器:
Adapter:
MainActivity.java
MainActivity.java