संपादित करें (सोल्वड) उत्तर के लिए प्रश्न के निचले भाग पर जाएं।Listitem क्लिक चेकबॉक्स के साथ काम नहीं करता है एंड्रॉइड
मेरे पास एक सूचीदृश्य है और मैं कस्टमैप्टर का उपयोग करके इसमें कुछ पंक्तियां बढ़ाता हूं। बढ़ने वाली पंक्ति में एक चेकबॉक्स होता है। मैं चेकबॉक्स की स्थिति को बनाए रख सकता हूं, लेकिन समस्या यह है कि, मेरी सूची आइटम और क्लिक करने योग्य नहीं हैं।
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fl=(ListView)findViewById(R.id.MainMenu);//Mainmenu is listview
fl.setAdapter(new CustomAdapter(Annotate.this,R.layout.preann2,_dir));//preann2 is row and _dir is the list of objects
//fl.setOnItemClickListener(this); tried this too
}
public void onItemClick(AdapterView<?> a, View view, int pos, long id)
{
//Implementations
}
public class CustomAdapter extends ArrayAdapter<String>
{
public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId,objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.preann2, parent, false);
try
{
TextView Name=(TextView)row.findViewById(R.id.title);
Name.setText("xyz");
ImageView icon=(ImageView)row.findViewById(R.id.icon);
row.setFocusable(true);
row.setClickable(true);
row.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkState[position]=!checkState[position];
}
});
CheckBox c=(CheckBox)row.findViewById(R.id.checkBox);
c.setChecked(checkState[position]);
c.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton v,
boolean isChecked) {
// TODO Auto-generated method stub
CheckBox checkBox=(CheckBox)v;
if (isChecked) {
checkState[position]=true;
}
else
checkState[position]=false;
}
});
/* tried this too
c.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox checkBox=(CheckBox)v;
if (checkBox.isChecked()) {
//checkBox.setChecked(false);
checkState[position]=true;
}
else
checkState[position]=false;
}});*/
icon.setImageResource(R.drawable.ic_launcher);
}catch(Throwable err)
{
err.printStackTrace();
}
return row;
}
}
मैं ListItems मामले में क्लिक करने योग्य उपयोगकर्ता केवल एक आइटम क्लिक करने के लिए चाहता है होना चाहता हूँ: यहाँ मेरी कोड है।
(अखिल के अनुरोध पर) संपादित करें - लेआउट फ़ोल्डर में preann2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip"
>
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="4dip"/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:singleLine="true"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
लेआउट फ़ोल्डर में main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/MainMenu"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
संपादित करें (हल) बस इन गुणों को चेकबॉक्स में जोड़ें:
android:focusable="false"
android:focusableInTouchMode="false"
यदि आप पंक्ति क्लिक करते हैं तो चेकबॉक्स को चेक करना चाहते हैं, तो पंक्तियों के लिए 'onClickListener()' के साथ स्थितियां रखें। –
मैं कुछ अन्य कार्यों को करना चाहता हूं जब मैं सूचीदृश्य में आइटम पर क्लिक करता हूं। चेकबॉक्स मैं सीधे उन पर क्लिक करके सेट कर सकते हैं, इसलिए इसके बारे में समस्याएं। जब मैं किसी आइटम पर क्लिक करता हूं तो मैं वास्तव में एक गतिविधि को कॉल करना चाहता हूं। – silentkratos
चेकबॉक्स के लिए 'onClickListener' के अंदर 'लॉग' जोड़ने का प्रयास करें और लॉग इन पंजीकृत होने पर हमें बताएं। –