1

我有带有 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>
4

3 回答 3

1

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

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

看这个例子:

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

于 2012-09-03T08:26:16.887 回答
0
   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;
        }

}
于 2012-09-03T08:35:49.737 回答
0

您可以使用 CheckedTextView,或者您可以创建一个 Checkable 布局,如下所示:

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

于 2012-09-03T08:27:12.020 回答