Android 如何使用查询结果匹配图像资源?

Android 如何使用查询结果匹配图像资源?,android,image,listview,sqlite,Android,Image,Listview,Sqlite,各位晚上好, 我正在使用SQLite和android。我正在尝试将查询结果与存储在drawable文件夹中的图像进行匹配。我试图动态搜索它,然后将其显示到自定义列表视图(图像+文本)。我不知道如何做到这一点。我可以从数据库中检索一个简单的查询结果,并将值显示到listview中,但我不知道如何在listview中显示图像+文本。谁能帮助我,指引我走上正确的道路 以下代码是我活动的一部分 List<YAODeckHieraticBlue> valuesMainimage = dat

各位晚上好,

我正在使用SQLite和android。我正在尝试将查询结果与存储在drawable文件夹中的图像进行匹配。我试图动态搜索它,然后将其显示到自定义列表视图(图像+文本)。我不知道如何做到这一点。我可以从数据库中检索一个简单的查询结果,并将值显示到listview中,但我不知道如何在listview中显示图像+文本。谁能帮助我,指引我走上正确的道路

以下代码是我活动的一部分

  List<YAODeckHieraticBlue> valuesMainimage = datasource2.SQLDeckHieraticBlueTABLESEARCHMAINDECKIMAGE();

  List<YAODeckHieraticBlue> valuesMain = datasource2.SQLDeckHieraticBlueTABLESEARCHMAINDECK();
ImageView thumbnail;
    thumbnail = (ImageView)findViewById(R.list.thumb);

    TextView name;
    name = (TextView)findViewById(R.list.text);

    name.setText(valuesMain);

    thumbnail.setBackgroundResource ( getResourceID  ( valuesMainimage , "drawable", getApplicationContext() ) );

     //if I just take out the previous section it only display Text but I am missing the Image part in the Listview

  ArrayAdapter<YAODeckHieraticBlue> adapterMain = new ArrayAdapter<YAODeckHieraticBlue>(this,
            android.R.layout.simple_list_item_1, valuesMain);

        setListAdapter(adapterMain);
数据库中的我的表如下所示:

  Table Name: [Deck name Hieratic Blue]
  column1:ID    -  Integer        
   column2 [Card Name]  - Text
  column3: Type     - Text
 column4: Deck_typeID - Integer
  Column5: Images  -Text          

注意:图像列只存储图像文件的名称,而不包含文件扩展名(.png)

您正在使用默认的ArrayAdapter将数据放入ListView。您需要的是一个定制的SimpleCorsorAdapter。SimpleCrsorAdapter用于从数据库获取数据。通过扩展此类并重写其方法,您还可以将图片放入列表视图中!扩展SimpleCursorAdapter时,会覆盖newView()和bindView()。还要记住使用视图保持器以获得更好的性能。
如果我说的话没有引起你的注意!别担心。使用此链接 下面将解释如何使用自定义适配器创建带有图像的listView。它不包括SimpleCrsorAdapter,因为它不使用数据库。但是如果你正确理解了这个概念。您也可以轻松地使用SimpleCursorAdapter

我为您编写自定义适配器。我希望有帮助

首先,您需要单行的布局
布局/single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:padding="10dip" >
<TextView
        android:id="@+id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
<ImageView
        android:id="@+id/name_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
最后,在这样的活动中使用自定义适配器

        Cursor cursor = helper.getAllStudents();
    CustomStudentAdapter studentAdapter = new CustomStudentAdapter(this,R.layout.single_row,cursor);
    this.setListAdapter(studentAdapter);
就这样。我没有测试这段代码,代码有一点不工作的可能!在这种情况下,您需要扩展CursorAdapter,因为它是SimpleCursorAdapter的父级,它为您提供了更大的灵活性

public class CustomStudentAdapter extends SimpleCursorAdapter {
private int layout;

public CustomStudentAdapter(Context context, int layout, Cursor c){
    super(context, layout, c, null, null, 1);

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout,parent,false);
    MyViewHolder holder = new MyViewHolder(v);
    v.setTag(holder);

    return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    int nameIndex = cursor.getColumnIndex(StudentTable.NAME);
    int imageNameIndex = cursor.getColumnIndex(StudentTable.IMAGE_URL);

    String name = cursor.getString(nameIndex);
    String imageName = cursor.getString(imageNameIndex);

    MyViewHolder holder = (MyViewHolder) view.getTag();

    TextView nameInput = holder.name;
    ImageView image = holder.image;


    nameInput.setText(name);
    int imageResource; 
    //colculate image resource based on iamgeName!  
    image.setImageResource(imageResource);
}


//View Holder is only a mechanism for performance increase. findViewById is a cup intensive task. View Holder
//just makes sure its not get called when there is no need for it!! 
class  MyViewHolder{
    private TextView name;
    private ImageView image;

    public MyViewHolder(View v){
        name = (TextView) v.findViewById(R.id.name);
        image = (ImageView) v.findViewById(R.id.image);

    }
}

}
        Cursor cursor = helper.getAllStudents();
    CustomStudentAdapter studentAdapter = new CustomStudentAdapter(this,R.layout.single_row,cursor);
    this.setListAdapter(studentAdapter);