dimanche 3 janvier 2016

Android: How to get all data which a list item hold?

I have a very complicated list item as shown in picture. very complicated list item

it has a title , image , rating (rating bar) , rating count (Text View), favorite indicator (image), favorite count (Text View), like indicator , like count (Text View), tried indicator (image) and tried count (Text View).

it gets all these data from sqlite db via this custom cursor adapter :

 public class RecListCursorAdapter extends CursorAdapter {

public RecListCursorAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context)
            .inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ImageView thumbImg = (ImageView) view.findViewById(R.id.avatar);
    ImageView favImg = (ImageView) view.findViewById(R.id.imageFav);
    ImageView likeImg = (ImageView) view.findViewById(R.id.imageLike);
    ImageView triedImg = (ImageView) view.findViewById(R.id.imageTried);

    thumbImg.setImageResource(cursor.getInt(cursor.getColumnIndex(DBHelper.REC_THUMB_ID)));

    if (cursor.getInt(cursor.getColumnIndex(DBHelper.REC_FAV)) == 1) {
        favImg.setImageResource(R.drawable.heart_on);
    }

    if (cursor.getInt(cursor.getColumnIndex(DBHelper.REC_LIKED)) == 1) {
        likeImg.setImageResource(R.drawable.thumb_on);
    }

    if (cursor.getInt(cursor.getColumnIndex(DBHelper.REC_TRIED)) == 1) {
        triedImg.setImageResource(R.drawable.tried_on);
    }

    TextView title = (TextView) view.findViewById(R.id.text_title);
    TextView favCnt = (TextView) view.findViewById(R.id.txtFavCnt);
    TextView likeCnt = (TextView) view.findViewById(R.id.txtLikeCnt);
    TextView triedCnt = (TextView) view.findViewById(R.id.txtTriedCnt);

    title.setText(cursor.getString(cursor.getColumnIndex(DBHelper.REC_NAME)));
    favCnt.setText(cursor.getString(cursor.getColumnIndex(DBHelper.REC_FAV_CNT)));
    likeCnt.setText(cursor.getString(cursor.getColumnIndex(DBHelper.REC_LIKE_CNT)));
    triedCnt.setText(cursor.getString(cursor.getColumnIndex(DBHelper.REC_TRIED_CNT)));

    RatingBar rating = (RatingBar) view.findViewById(R.id.list_ratingbar);
    rating.setRating(cursor.getFloat(cursor.getColumnIndex(DBHelper.REC_RATING)));
}
}

right now I'm sending row id to a detail activity which query the db to get the same data again which mean that I query the db twice for the same data

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getActivity(), RecipeDetailActivity.class);
            intent.putExtra(RecipeDetailActivity.EXTRA_POSITION, String.valueOf(id));
            getActivity().startActivity(intent);
        }
    });

what I want to do is send that data from list item to details activity to display to save the second query

I thought that I can get reference to those views and get values from them but this will be very complex process because of image views , text views and rating bar.

Sure there is an easier and smarter way to do this but I can't figure it out.

I did a lot of search with no luck.

Can you guide me what can I do here?

Aucun commentaire:

Enregistrer un commentaire