Android的ListView控件与删除按钮

Android的ListView控件与删除按钮

本文介绍了Android的ListView控件与删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定列表视图列表。此工程确定,当我创建一个扩展ListActivity的活动,我有一个文本视图在我的布局文件(即活动结合到默认的列表视图中的活动)。不过,我想这样做是有一个包含一个图像按钮(进一步执行该行的deeltion)和文本视图来说明该项目被绑定的名字一个ListView。

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.

任何人都可以点我的方向,将展示如何做到这一点,它包含:

Can anyone point me in the direction that would show how to do this that contains:

  • 在布局文件
  • 活动类

我打了四周,不能似乎得到它的工作,只要我想补充一个ListView /图像按钮布局文件我的code崩溃。我还发现了几个例子,通过谷歌,但没有一个似乎工作!

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!

推荐答案

您可以获取列表的功能,即使你不扩展ListActivity,而且还可以通过扩展活动。要做到这一点,你需要布局文件明确命名的ListView元素,如下图所示。

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.

<?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>

在这里,我有以下一些图像的结果列表。在您的活动类,你必须扩展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);
        }
    }
}

删除按钮onClickListener()调用一些功能来确认删除。当然,它必须在方面做到列表中的当前位置。

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.

这code段只是说明,但我希望这将是有益的解决您的问题。

This code snippet is just illustration, but I hope it will be useful to solve your issue.

这篇关于Android的ListView控件与删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:19