Tell me how to make a handler for clicking on CardView, created programmatically. They are not arranged in a row, but are located in RelativeLayout under the desired elements. I load them with a loader

ArrayList<CardView> card = new ArrayList<CardView>(); int count; public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { count = 0; if (cursor.moveToFirst()) { do { //здесь считываю данные и расчитываю на какой высоте выставлять card.add((CardView)cv.findViewById(R.id.cardview)); card.get(count).getLayoutParams().height = здесь высота card.get(count).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //отсюда запускаю фрагмент с описанием этой cardview } }); ++count; } while (cursor.moveToNext()); } } 

as a result, going beyond (invalid index)

  • 2
    You are doing completely wrong. To organize the lists that display information from the database, use a ListView with a SimpleCursorAdapter or RecyclerView . Believe me, this is 100 times more convenient than what you are doing now - Flippy
  • 2
    @msp, why do you have the count variable moved out of the method? And you do not create cards programmatically, but pull out the same: cv.findViewById(R.id.cardview) - that is, the entire list will contain a bunch of links to one card. From here and "invalid index" - as it is impossible to add one twist to the container twice. - woesss
  • cards are created View cv = mInflater.inflate (R.layout.event, r_ayout, false); and drawn without problems. But the handler can not do. They do not go list, and scattered on the layout, this is the problem. I know ListView and RecyclerView, but I don’t understand how to screw them here - msp
  • 2
    But still in onLoadFinished you refer to the same card, since you take it from the same cv container. I think you need to fill out the list of card immediately when creating them with a inflater. In general, show the error and more code of the code - from this piece it’s not very clear - woesss

1 answer 1

Programmatically View created by either the designer:

  View view = new View(context); 

either from xml using LayoutInflater :

  View view = LayoutInflater.from(context).inflate(R.layout.filename, root);