mardi 31 décembre 2019

How can I know which row is checked using checkbox and listview?

  1. Below is ListView Item Class

     public class CategoryItem06 {
    
     private String text;
     private boolean checked;
    
     public void setText(String text) {
     this.text = text;
     }
    
     public String getText() {
     return this.text;
     }
    
     //    public void setCheck(boolean checked) {
     this.checked = checked;
     }
    
     //    public boolean getCheck() {
     return this.checked;
     }
     }
    
  2. Below is Adapter

    public class CategoryAdapter06 extends BaseAdapter {
    
      public ArrayList<CategoryItem06> listViewItemList = new ArrayList<CategoryItem06>() ;
    
      public CategoryAdapter06() {
    
      }
    
      @Override
      public int getCount() {
      return listViewItemList.size() ;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
    
      final int pos = position;
      final Context context = parent.getContext();
    
      if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.category_item06, parent, false);
       }
    
      TextView textTextView = (TextView) convertView.findViewById(R.id.textView1) ;
      CheckBox checkBox=(CheckBox) convertView.findViewById(R.id.checkBoxMafia);
    
      CategoryItem06 listViewItem = listViewItemList.get(position);
    
      textTextView.setText(listViewItem.getText());
      checkBox.setChecked(listViewItem.getCheck());
      return convertView;
      }
    
      @Override
      public long getItemId(int position) {
      return position ;
      }
    
      @Override
      public Object getItem(int position) {
      return listViewItemList.get(position) ;
      }
    
      public void addItem( String text) {
      CategoryItem06 item = new CategoryItem06();
      item.setText(text);
    
      listViewItemList.add(item);
      }
      }
    
  3. Below is Checkable Relative Layout

    public class CategoryCheckableRelativeLayout extends RelativeLayout implements Checkable {
    
    public CategoryCheckableRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // mIsChecked = false ;
    }
    @Override
    public boolean isChecked() {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
    return cb.isChecked();
    // return mIsChecked ;
    }
    
    @Override
    public void toggle() {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
    
    setChecked(cb.isChecked() ? false : true);
    // setChecked(mIsChecked ? false : true) ;
    }
    
    @Override
    public void setChecked(boolean checked) {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBoxMafia);
    
    if (cb.isChecked() != checked) {
        cb.setChecked(checked);
    }
    } 
    }
    
  4. Below is Activity that uses ListView

    public class CategorySelection06 extends AppCompatActivity {
    Singleton s1 = Singleton.getInstance();
    ListView listview;
    // Creating Adapter
    CategoryAdapter06 adapter = new CategoryAdapter06();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category_selection06);
    
    listview = (ListView) findViewById(R.id.listview1);
    listview.setAdapter(adapter);
    
    // Adding Items
    adapter.addItem("Pets");
    adapter.addItem("Singers");
    adapter.addItem("Game");
    adapter.addItem("Nations");
    
    Button button = findViewById(R.id.button6);
    
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i < adapter.listViewItemList.size(); i++) {
                if (adapter.listViewItemList.get(i).getCheck()) {
                    s1.ListViewCategory.add(adapter.listViewItemList.get(i).getText());
                }
            }
            Intent intent = new Intent(getApplicationContext(), RoleSelection07.class);
            startActivity(intent);
            finish();
            }
       });
     }
     }
    

My ListView's form is like this: TextView ------- Checkbox

I want to make an Activity like this: if user checks checkbox, then the checked row's text is saved in ArrayList in Singleton class.

For example, if a user checked checkbox of "Pets" and "Nations" then these words goes into the ArrayList s1.ListViewCategory, which is in Singleton class.

I've tried for loops and if statements in CategorySelectionActivity like this:

    button.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View view) {
            for (int i = 0; i < adapter.listViewItemList.size(); i++) {
                if (adapter.listViewItemList.get(i).getCheck()) {
                    s1.ListViewCategory.add(adapter.listViewItemList.get(i).getText());
                }
            }
            Intent intent = new Intent(getApplicationContext(), RoleSelection07.class);
            startActivity(intent);
            finish();
            }

However,getCheck() doesn't work because setCheck() is not in the addItem() in CategoryAdapter class.

I tried to put setCheck() in the addItem() method , but then I have to put another parameter in add(), then I got red lines and errors.

Since I am a novice, I copied these codes from sites, but I don't really get the idea of using CheckableRelativeLayout.

This Layout shows that the checkbox is checked or not, but it doesn't indicate which row is checked.

To sum up, my question is ' how can I get texts from multiple rows that are checked, and know which row is checked ?

I know the question is super long, but I really need to solve this problem... I will be super grateful if someone answers my question Thank you




Aucun commentaire:

Enregistrer un commentaire