CheckedTextview不可检查

时间:2018-11-27 22:34:27

标签: android android-listview checkedtextview

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

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


        }
    });

}

适配器:

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

}

模型类:

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

1 个答案:

答案 0 :(得分:0)

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