Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

Android ListView with delete button

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:

  • 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...