带有删除按钮的 Android ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3750380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ListView with delete button
提问by januszstabik
I am trying to bind a list view to a List. This works ok when I create an activity that extends ListActivity and I have a text view in my layout file (i.e. the activity is binding to the default listview in the activity). However, what I would like to do is have a ListView that contains an image button (to further perform the deeltion of the row) and the text view to illustrate the name of the item being bound.
我正在尝试将列表视图绑定到列表。当我创建一个扩展 ListActivity 的活动并且我的布局文件中有一个文本视图时,这可以正常工作(即该活动绑定到该活动中的默认列表视图)。但是,我想要做的是一个 ListView 包含一个图像按钮(以进一步执行行的删除)和文本视图来说明被绑定的项目的名称。
Can anyone point me in the direction that would show how to do this that contains:
任何人都可以向我指出如何做到这一点的方向,其中包含:
- The layout file
- The activity class
- 布局文件
- 活动类
I have played around and cant seem to get it to work, as soon as I add a ListView / image button to the layout file my code crashes. I've also found a few examples through google, but none seem to work!
我已经玩过了,似乎无法让它工作,只要我向布局文件添加 ListView/图像按钮,我的代码就会崩溃。我还通过谷歌找到了一些例子,但似乎都不起作用!
回答by Zelimir
You can get List functionality even if you do not extend ListActivity, but also via extending Activity. To achieve that, you need layout file with explicitly named ListView element, as illustrated below.
即使您不扩展 ListActivity,也可以通过扩展 Activity 来获得 List 功能。为此,您需要具有明确命名的 ListView 元素的布局文件,如下图所示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Details_RelativeLayout01">
<ImageView android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" android:id="@+id/Details_ImageView01"
android:layout_marginTop="10dip" android:layout_width="60dip"
android:layout_height="60dip"></ImageView>
<ListView android:layout_width="fill_parent"
android:drawSelectorOnTop="false" android:clipChildren="true"
android:fitsSystemWindows="true" android:layout_height="fill_parent"
android:layout_below="@+id/Details_ImageView01" android:id="@+id/Details_ListView01">
</ListView>
</RelativeLayout>
Here I have list of results below some image. In your Activity class you must extend ArrayAdapter. Also, you need to define the look of one list row. In example below it is done in the R.layout.one_result_details_row
.
在这里,我在一些图像下方列出了结果。在您的 Activity 类中,您必须扩展 ArrayAdapter。此外,您需要定义一个列表行的外观。在下面的示例中,它是在R.layout.one_result_details_row
.
public class ListOfDetails extends Activity {
private DetailsListAdapter mDetailsListAdapter;
private Vector<String> mDetailsTimeStringsList;
private Vector<String> mDetailsDateStringsList;
private ListView mDetailsListView;
private int mSelectedPosition;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.detailed_results_list);
ListView mDetailsListView = (ListView) findViewById(R.id.Details_ListView01);
ImageView mSelectedPuzzleIcon = (ImageView) findViewById(R.id.Details_ImageView01);
mDetailsListAdapter = new DetailsListAdapter();
mDetailsListView.setAdapter(mDetailsListAdapter);
mDetailsTimeStringsList = new Vector<String>();
mDetailsDateStringsList = new Vector<String>();
updateTheList();
}
class DetailsListAdapter extends ArrayAdapter<String> {
DetailsListAdapter() {
super(ListOfDetails.this, R.layout.one_result_details_row);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.one_result_details_row, parent, false);
TextView result = (TextView) row.findViewById(R.id.Details_Row_TextView01);
TextView date = (TextView) row.findViewById(R.id.Details_Row_TextView02);
Button deleteButton = (Button) row.findViewById(R.id.Details_Button01);
deleteButton.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
confirmDelete();
}
}
);
return(row);
}
}
}
Delete button onClickListener() calls some function to confirm deletion. Of course, it has to be done in respect to the current position in the list.
删除按钮 onClickListener() 调用一些函数来确认删除。当然,它必须针对列表中的当前位置进行。
This code snippet is just illustration, but I hope it will be useful to solve your issue.
此代码片段只是说明,但我希望它对解决您的问题有用。
回答by januszstabik
Found this in the end which was the most complete example:
最后发现这是最完整的例子:
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html