1

I have custom ListView layout with a TextView and CheckBox. Everything works fine. What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.

I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.

MAIN ACTIVITY

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();
    }
}

CUSTOM ADAPTER

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;
    }
}

LAYOUT

<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 のすべての行がクリックをリッスンできるようになっています。

ListView に実装 する必要があります....onItemClickListener()

次の例を参照してください。

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

于 2012-09-03T08:26:16.887 に答える
0

CheckedTextView を使用するか、次のような Checkable Layout を作成できます。

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

于 2012-09-03T08:27:12.020 に答える
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 に答える