CHỈNH SỬA (GIẢI PHÁP) Để có câu trả lời, hãy chuyển đến cuối câu hỏi.Lần nhấp vào Danh sách không hoạt động với các hộp kiểm Android
Tôi có một listview và tôi thổi phồng một số hàng trong đó bằng cách sử dụng một customadapter. Hàng đang được tăng cấp có chứa hộp kiểm. Tôi có thể duy trì trạng thái của hộp kiểm, nhưng vấn đề là, các mục danh sách của tôi không thể nhấp được nữa. Dưới đây là mã của tôi:
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;
}
}
Tôi muốn listitems được nhấp trong trường hợp người dùng muốn bấm duy nhất một mục duy nhất.
EDIT (theo yêu cầu Akhil của) - preann2.xml trong thư mục layouts:
<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 trong thư mục layouts:
<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>
EDIT (SOLVED) Chỉ cần thêm các thuộc tính này vào hộp kiểm:
android:focusable="false"
android:focusableInTouchMode="false"
Nếu bạn muốn checkBox được kiểm tra nếu hàng được nhấp, sau đó đặt các điều kiện với 'onClickListener()' cho chính hàng đó. –
Tôi muốn một số hành động khác cần làm khi tôi nhấp vào các mục trong listview. Hộp kiểm tôi có thể đặt trực tiếp bằng cách nhấp vào chúng, do đó, vấn đề về điều đó. Tôi thực sự muốn gọi một hoạt động khi tôi nhấp vào một mục. – silentkratos
Thử thêm 'log' vào trong' onClickListener' cho hộp kiểm và cho chúng tôi biết nếu nhật ký được đăng ký. –