checkviewTextView无效的listview

时间:2018-04-25 11:49:45

标签: java android listview

我已经使用CheckedTextView实现了一个列表视图。当我选择特定行并单击复选框时,复选框变为不可见。有时候任何其他行都会被选中。我想选择多个项目。 请帮忙... List_row布局是..

 <CheckedTextView
    android:id="@+id/service_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textSize="20sp"
    android:layout_marginBottom="10dp"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:textColor="#000000"
    android:checked= "false"
   >

CustomListView是:

   import java.util.ArrayList;

   public class CustomListView extends ArrayAdapter {

 //to reference the Activity
   private final Activity context;
   String value;

//to store the list items
   private final String[] nameArray;
   CheckedTextView nameTextField;
   public CustomListView(Activity context, ArrayList nameArra) {

    super(context, R.layout.row_list_view, nameArrayParam);
    this.context = context;
    this.nameArray = nameArrayParam;
}

    public View getView(int position, View view, ViewGroup parent)
   {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.row_list_view, null, true);

    //this code gets references to objects in the listview_row.xml file
     nameTextField = (CheckedTextView) 
      rowView.findViewById(R.id.service_name);

   //this code sets the values of the objects to values from the arrays
    nameTextField.setText(nameArray[position]);


  // perform on Click Event Listener on CheckedTextView
    nameTextField.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.v("customList",";clicked row is " + 
            nameTextField.getText().toString());

            if (nameTextField.isChecked()) {
           // set check mark drawable and set checked property to false
                value = "un-Checked";
                nameTextField.setCheckMarkDrawable(R.color.colorAccent);
                nameTextField.setChecked(false);
            } 
else {

// set check mark drawable and set checked property to true
                value = "Checked";

                nameTextField.setChecked(true);
            }
            Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
        }
    });
    return rowView;

}

在我的MainActivity.java中,我使用了listView setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {

            selected_Item = (String) arg0.getItemAtPosition(arg2);
                        Toast.makeText(AskForService.this, "Clicked item is" + selected_Item, Toast.LENGTH_LONG).show();
        }
    });

实现这些代码后,我可以看到 -

  1. 带有选中TextView的ListView
  2. ListView可以滚动显示所有列表项
  3. 点击一个特定的行..listview点击监听器被调用并显示Toast但复选框变为白色(不可见),有时另一行被选中...请帮助

2 个答案:

答案 0 :(得分:0)

创建一个包含标志的模型类&#34; isChecked&#34;并根据适配器内的模型设置数据

此链接可能会有所帮助:https://stackoverflow.com/a/40285759/8770539

答案 1 :(得分:0)

使用arraylist store checked item's position

为onclick()

调用 notifyDataSetChanged()

使用此,

    if(list.contains(position)) {
         nameTextField.setChecked(true);
    }
    else {
         nameTextField.setChecked(false);
    }
    nameTextField.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(list.contains(position))
            {
                list.remove(position);
            }
            else
            {
                list.add(position);
            }
            notifyDataSetChanged();
         }
    });