androidandroid-listviewcheckedtextview

CheckedTextview not checkable


Can't get my checkedtextview to check or do anything for that matter. When I touch the listitem it doesn't do anything, it's not clickable. I've tried to add itemclicklistener to the listview but it didn't work. Am I missing something? or is it another way to do this?

MainActivity:

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) {


        }
    });

}

Adapter:

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

}

Model 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"/>

Solution

  • Unlike CheckBox or any other CompoundButton, you need to call setCheck to toggle state on CheckedTextView whenever it is clicked:

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

    And also by the way, this line looks wrong:

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

    It should be:

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