Android:如何获取列表项所包含的所有数据?

时间:2016-01-03 14:11:23

标签: android sqlite

我有一个非常复杂的列表项,如图所示。 very complicated list item

它有标题,图像,评级(评级栏),评分计数(文本视图),收藏指示符(图像),收藏计数(文本视图),像指标,如计数(文本视图),尝试指标(图像) )并尝试计数(文本视图)。

它通过这个自定义游标适配器从sqlite db获取所有这些数据:

 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)));
}
}

现在我将行id发送到详细活动,该活动查询数据库以再次获取相同的数据,这意味着我为相同的数据查询数据库两次

  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);
        }
    });

我想要做的是将列表项中的数据发送到详细信息活动以显示以保存第二个查询

我认为我可以参考这些视图并从中获取值,但由于图像视图,文本视图和评级栏,这将是非常复杂的过程。

当然,有一种更简单,更聪明的方法可以解决这个问题,但我无法解决这个问题。

我做了很多搜索而没有运气。

你能指导我在这里做些什么吗?

1 个答案:

答案 0 :(得分:0)

选项1

未经测试!

在适配器中添加一个方法,类似这样

public void getData(int position) {
    Cursor cursor = getCursor();
    int p = cursor.getPosition;
    cursor.moveToPosition(position);
    // ... get data here
    cursor.moveToPosition(p);
}

将返回类型更改为您喜欢的任何内容

选项2

制作一个ArrayList&lt; Model&gt;从您的光标,其中Model可能类似于

class Model {
    public String title;
    public String image;
    public boolean rated;
    public int ratingCount;
    public boolean favorited;
    public int favoriteCount;
    public boolean liked;
    public int likeCount;
    public boolean tried;
    public int triedCount;
}

并使用[ListAdapter(Context context,int resource,int textViewResourceId,List&lt; T&gt; objects)](http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List))和ListView