Click Listener on ListView

丶灬走出姿态 提交于 2019-12-01 00:11:59

This should allow you to interact with selected item the way you want.

MyListAdapter.java

package com.example.simplelist;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Groups;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends ListActivity {

    List<ContactGroup> groups = new ArrayList<ContactGroup>(); 

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.groups = getContacts();
        ContactGroupAdapter cAdapter = new ContactGroupAdapter(this); 
        setListAdapter(cAdapter);
    }

    private List<ContactGroup> getContacts(){
        List<ContactGroup> grps = new ArrayList<ContactGroup>();
        ContentResolver cr = getContentResolver();
        Cursor groupCur = cr.query(Groups.CONTENT_URI, null, null, null, null);
        if (groupCur.getCount() > 0) {
            while (groupCur.moveToNext()) {
                ContactGroup newGroup = new ContactGroup();
                newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
                newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
                grps.add(newGroup);
            }
        }
        return grps;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        ContactGroup selectedGroup = new ContactGroup();
        selectedGroup = groups.get(position);
        Toast.makeText(getBaseContext(), selectedGroup.Name + " ID #" + selectedGroup.Id, 1).show();
    }


    public class ContactGroupAdapter extends BaseAdapter{
        public ContactGroupAdapter(Context c) {
            mContext = c;
        }
        @Override
        public int getCount() {
            return groups.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                LayoutInflater vi = LayoutInflater.from(this.mContext);  
                convertView = vi.inflate(R.layout.two_line_list_item, null);
                holder = new ViewHolder();
                convertView.setTag(holder); 
            }
            else {
                //Get view holder back
                holder = (ViewHolder) convertView.getTag();
            }
            ContactGroup c = groups.get(position);
            if (c != null) {
                //Name
                holder.toptext = (TextView) convertView.findViewById(R.id.text1);
                holder.toptext.setText(c.Name);
                //ID
                holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
                holder.bottomtext.setText(c.Id);
            }
            return convertView;
        }
        private Context mContext;

    }
    public static class ViewHolder {
        TextView toptext;
        TextView bottomtext;
    }

    public class ContactGroup{
        public String Id;
        public String Name;
    }

}

two_line_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

         <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/text2"
         android:textSize="16sp"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>

</LinearLayout>

Since you are using ListActivity you need to override onListItemClick().

protected void onListItemClick(ListView l, View v, int position, long id) {
    // position is the position in the adapter and id is what adapter.getItemId() returns.
    // use one of them to get the group id from the data.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!