【问题标题】:handle results from sqlite and pass them to custom list adapter处理来自 sqlite 的结果并将它们传递给自定义列表适配器
【发布时间】:2012-01-24 15:59:25
【问题描述】:

我使用 sqlite 并从两列(名称、图标)中获取结果。我想将它们传递给自定义适配器,以便将名称设置为 (TextView) 的文本,然后将图标设置为带有 setCompoundDrawablesWithIntrinsicBounds 的 drawableLeft。

首先我在db中的插入是这样的

  "Fruit / Vegetables,R.drawable.ic_launcher",
   "Meat / Fisth,R.drawable.ic_launcher"

在我的 listActivity 中,我在打开数据库等后有此代码。

 mShopCatCursor = mDbHelper.fetchShoppingCategories();
 startManagingCursor(mShopCatCursor);

  String[] names,icons;
while(mShopCatCursor.moveToNext()){
    for(int i=-1,l=mShopCatCursor.getColumnCount(); ++i<l;){
        names[i]= mShopCatCursor.getString(0);
        icons[i]= mShopCatCursor.getString(1);
    }
}

    //this was taken from a tut 
    String[] from = new String[]{AppSQLite.KEY_NAME,AppSQLite.KEY_ICON};

    TextViewWithDrawableListAdapter adap = 
        new TextViewWithDrawableListAdapter(this, from);
    setListAdapter(adap);

我想要的是传递名称和图标,以便自定义适配器执行以下操作

  public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.main_list_item, null);
    }

    TextView textView = (TextView) v.findViewById(R.id.main_menu_list_item);

我想要这个!! 为每个列表项取正确的值。

            textView.setText(names[0]); 
    textView.setCompoundDrawablesWithIntrinsicBounds(icons[0], 0, 0, 0);

    return v;
}

我知道 namew[0] 和 icon 不正确,但我真的找不到路,即使在网上搜索了几个小时。

【问题讨论】:

    标签: android sqlite custom-adapter


    【解决方案1】:

    首先我不清楚你想要什么。如果要将名称和图标数组传递给自定义适配器类,则可以使用自定义适配器类中的构造函数来接收它们。或阅读this tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多