0

Androd SQLite display data read from a CursorAdapter class and a ListView in ViewHolder instead.

I wanted a CheckBox for each line, which can put specific updates the database record.

The thing does not work well because when I activate the first line CheckBox you change that appear on the screen last line record.

long _id = todo.getID(); //

checkBoxFavorite.setTag(_id);
checkBoxFavorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) 
    {
        //Update database row
        }
        else if (!isChecked)
        {
        //Update database row
        }
    }
});
Rami
  • 7,879
  • 12
  • 36
  • 66
P3t3r
  • 1
  • 1

2 Answers2

0

Try this.

public class MainActivity extends AppCompatActivity{
private RecyclerView recyclerView;

private ArrayList<String> list;

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

    list=new ArrayList<>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("e");
    recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    TestAdapter adapter = new TestAdapter();
    recyclerView.setAdapter(adapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setHasFixedSize(true);
}

class TestViewHolder extends RecyclerView.ViewHolder
{
    CheckBox checkBox;
    public TestViewHolder(View view)
    {
        super(view);
        checkBox=(CheckBox)view.findViewById(R.id.checkbox);
    }
    public void addDetail(int positon)
    {
        checkBox.setText(list.get(positon));
    }
}
class TestAdapter extends RecyclerView.Adapter<TestViewHolder>
{
    public TestAdapter()
    {

    }

    @Override
    public TestViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.recycler_item,null);
        return new TestViewHolder(view);
    }

    @Override
    public void onBindViewHolder(TestViewHolder viewHolder, int i)
    {
        viewHolder.addDetail(i);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}}
Andy.Zhao
  • 250
  • 3
  • 16
0

you have to use the setTag() and getTag(), on the view you can check out from this blog post Link

This is not a stright forward answer but it will get started.

Irfan
  • 956
  • 9
  • 16
  • getTag() = +9 row. Image: [link](http://kepfeltoltes.hu/150902/Screenshot_2015-09-02-09-47-41_www.kepfeltoltes.hu_.jpg) – P3t3r Sep 02 '15 at 15:16