【问题标题】:Custom List View with OnClickListener使用 OnClickListener 自定义列表视图
【发布时间】:2012-09-03 08:22:15
【问题描述】:

我有一个带有 TextView 和 CheckBox 的自定义 ListView 布局。一切正常。 我想要的是,当我单击 CheckBox 或 TextView(在 ListView 的单个视图上)时,两者的行为都应该像一个对象。 (我可以点击CheckBox,它不影响TextView,TextView对CheckBox没有影响。)代码没有问题。

我已经实施了所有可能的解决方案,但问题仍然存在。 (单击列表的每个对象应考虑单击完整的行。)我希望我解释得很好。

主要活动

package com.example.smsplanner;

public class SMSPlanner extends ListActivity      {

    ListView contactsListView;

    private String TAG = "SMSPlanner";    CheckBox check;
    int count;

    List<ContactInfo> list = new ArrayList<ContactInfo>();
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        ph = new String[3];
        phType = new String[3];
        LoadContactListFromPhone();
        ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(contactadAdapter);
    }    
    @Override
    public void onListItemClick(ListView parent, View v, int position, long id)
    {
        TextView tx =(TextView)v.findViewById(R.id.firstname);
        TextView ph =(TextView)v.findViewById(R.id.phone);
        Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();

    }
    final class ContactHolder{
        TextView txtviewfirstname;
        CheckBox chkselected;
        TextView txtviewphone;
    }
    void LoadContactListFromPhone()
    {
        loadlistandreturns();
    }

    void call()
    {
        Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
        toast.show();
    }
}

自定义适配器

public class ContactsAdapter extends ArrayAdapter<ContactInfo> 
{

    private final Activity context;
    int resourceid;

    List<ContactInfo> list = null;
    public ContactsAdapter(Activity context, List<ContactInfo> list) {
        super(context, R.layout.contactrow, list);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup viewgroup){
        View view = null;
        if(convertview == null){
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.contactrow, null);

            ContactHolder holder = new ContactHolder();

            holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
            holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
            holder.chkselected = (CheckBox)view.findViewById(R.id.check);
            view.setTag(holder);
        }
        else{
            view = convertview;
        }
        ContactHolder holder2 = (ContactHolder) view.getTag();
        holder2.txtviewfirstname.setText(list.get(position).firstname);
        holder2.txtviewphone.setText(list.get(position).phonenumber);
        holder2.chkselected.setChecked(list.get(position).selected);
        return view;
    }


    final class ContactHolder{
        TextView txtviewfirstname;
        CheckBox chkselected;
        TextView txtviewphone;
    }
}

布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <RadioGroup
        android:id="@+id/rgStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="15"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/firstname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/phone"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rgStyle2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="85"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/check"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            android:focusable="false"
            android:focusableInTouchMode="false" >

        </CheckBox>
    </RadioGroup>
</LinearLayout>

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    1. 默认情况下,ListView 的所有 Rows 都启用监听点击......

    您必须为 ListView实现onItemClickListener()....

    看这个例子:

    http://www.mkyong.com/android/android-listview-example/

    【讨论】:

      【解决方案2】:

      你可以使用 CheckedTextView,或者你可以创建一个 Checkable Layout,像这样:

      http://tokudu.com/2010/android-checkable-linear-layout/

      【讨论】:

        【解决方案3】:
           i think you should make adapter as
        
         public class ContactsAdapter extends BaseAdapter {
        
            ArrayList<ContactInfo> mlist;
            Context mcontext;
        
        
        public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {      
                mlist =  mchtlist;
                mcontext = context;
        
            }
        
            @Override
            public int getCount() {
                return mlist.size();
            }
        
            @Override
            public Object getItem(int postion) {
                return mlist.get(postion);
            }
        
            @Override
            public long getItemId(int position) {
                return position;
            }
        
            @Override
                public View getView(int position, View convertview, ViewGroup viewgroup){
                    View view = null;
                    if(convertview == null){
                        LayoutInflater inflater = context.getLayoutInflater();
                        view = inflater.inflate(R.layout.contactrow, null);
        
                        ContactHolder holder = new ContactHolder();
        
                        holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
                        holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
                        holder.chkselected = (CheckBox)view.findViewById(R.id.check);
        
                        setOnClickListener(new OnClickListener() {
                @Override
                    public void onClick(View arg0) {
                    // to open the selected file in resp
        
                          // do your work here
                         }});
        
        
            chkselected .setOnClickListener(new OnClickListener() {
                @Override
            public void onClick(View v) {
            // Toast.makeText(context,// "checked is clicke="+pos, 12).show();
                    if (chkselected.isChecked())          
                               {            
        
                                // do your work here
                    } else {
        
             // do your work here                               
                    }
                }
        });
        
        
        
                    view.setTag(holder);
        
                }
                    else{
                        view = convertview;
                    }
                    ContactHolder holder2 = (ContactHolder) view.getTag();
                    holder2.txtviewfirstname.setText(list.get(position).firstname);
                    holder2.txtviewphone.setText(list.get(position).phonenumber);
                    holder2.chkselected.setChecked(list.get(position).selected);
                    return view;
                }
        
        }
        

        【讨论】:

        • 呵呵。我在这里实现了,但后来我无法在我的 smsplanner 活动中访问它。
        • 好的,你能把你的 ArrayAdapter 改成 BaseAdapter 吗?
        • 那么如果 id 这样做会发生什么....
        • 您可以在构造函数中传递您的 List 列表并获取值
        • 这里你已经按照要求做了,你只需要扩展 BaseAdapter
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多