CheckedTextview 不可检查

CheckedTextview not checkable

无法让我的 checkedtextview 检查或做任何事情。当我触摸列表项时,它什么也没做,它不可点击。 我试图将 itemclicklistener 添加到列表视图,但它没有用。 我错过了什么吗?还是另一种方法?

主要活动:

public class MainActivity extends AppCompatActivity {


DatabaseReference databaseItems;




TextView itemlist;
ListView listitem;
ImageView addlistitem;

List<Items> listitems;


{

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    databaseItems = FirebaseDatabase.getInstance().getReference("Items");



    itemlist = (TextView) findViewById(R.id.tvitemlist);

    listitem = (ListView) findViewById(R.id.lvitemlist);

    ArrayAdapter<Items> adapter = new ArrayAdapter<Items>(this, android.R.layout.simple_list_item_multiple_choice);
    itemlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    listitem.setAdapter(adapter);




    listitem.setItemsCanFocus(false);

    listitem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    databaseItems.orderByChild("items").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            listitems.clear();

            for (DataSnapshot itemsnapshot : dataSnapshot.getChildren()){

                final Items items = itemsSnapshot.getValue(Items.class);

                listitems.add(items);

                final listItems adapter = new listItems(MainActivity.this, listitem);
                listitem.setAdapter(adapter);

                adapter.notifyDataSetChanged();









            }



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {


        }
    });

}

适配器:

public class listItems extends ArrayAdapter<Items> {

private Activity context;
private List<Items> listitems;

public listItems(Activity context, List<Items> listitems){
    super (context, R.layout.list_items, listitems);
    this.context = context;
    this.listitems = listitems;

}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.list_items, null, true);

    final Items items = listitems.get(position);

    final TextView tvitemname = (CheckedTextView) listViewItem.findViewById(R.id.tvitemname);
    tvitemname.setText(item.getItemname());
    ((CheckedTextView) tvitemname).setChecked(listitems.get(position).setSelected(true));

    tvitemname.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(((CheckedTextView) tvjitemname).isChecked()) {
                items.setSelected(false);
                notifyDataSetChanged();
            }else{
                items.setSelected(true);
                notifyDataSetChanged();
            }
        }
    });

    return listViewItem;
}

}

型号class:

public class Items {

String itemId;
String itemname;
private boolean IsSelected;

public Items(String itemId, String itemname, boolean isSelected) {
    this.itemId = itemId;
    this.itemname = itemname;
    IsSelected = isSelected;
}

public String getItemId() {
    return itemId;
}

public String getItemname() {
    return itemname;
}



public boolean isSelected() {
    return IsSelected;
}

public boolean setSelected(boolean selected) {
    IsSelected = selected;
    return false;
}

}

<?xml version="1.0" encoding="utf-8"?>



<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:id="@+id/tvitemname"
    android:checkMarkTint="@color/colorPrimary"/>

CheckBox 或任何其他 CompoundButton 不同,您需要调用 setCheck 来在 CheckedTextView 被点击时切换状态:

    if (((CheckedTextView) tvjitemname).isChecked()) {
        tvjitemname.setChecked(false);
        items.setSelected(false);
        notifyDataSetChanged();
    } else {
        tvjitemname.setChecked(true);
        items.setSelected(true);
        notifyDataSetChanged();
    }

顺便说一下,这一行看起来不对:

// Wrong
((CheckedTextView) tvitemname).setChecked(listitems.get(position).setSelected(true));

应该是:

((CheckedTextView) tvitemname).setChecked(listitems.get(position).isSelected();