ListView 未根据 sqlite 中的名称显示可绘制对象中的正确图像

Posted

技术标签:

【中文标题】ListView 未根据 sqlite 中的名称显示可绘制对象中的正确图像【英文标题】:ListView is not shows the correct images that are in the drawable according to their name in sqlite 【发布时间】:2014-06-13 20:46:09 【问题描述】:

我有一个使用 sqlite 作为 DB 并从 DB 获取数据然后在ListView 中显示这些数据的 android 应用程序。

问题是在sqlite数据库中没有根据他们的名字显示正确的图像,如何解决这个错误?

谁能帮帮我?

CustomAdapterMatchSchedule.java

@Override
public View getView(int position, View convertView, ViewGroup arg2) 
    // TODO Auto-generated method stub
    ItemDetails itemdetail = itemdetailsList.get(position);

    if (convertView == null) 
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_list_match_schedule,
                null);
    
    // Stad_name
    TextView txtStadName = (TextView) convertView
            .findViewById(R.id.textLocation);
    txtStadName.setText(itemdetail.getStad_name());
    // team1
    TextView txtTeam1 = (TextView) convertView.findViewById(R.id.textName1);
    txtTeam1.setText(itemdetail.getTeam1());
    // team2
    TextView txtTeam2 = (TextView) convertView.findViewById(R.id.textName2);
    txtTeam2.setText(itemdetail.getTeam2());

    // flag1
    int imageid = context.getResources().getIdentifier("brazil_flag",
            "drawable", context.getPackageName());
    ImageView imagenow = (ImageView) convertView
            .findViewById(R.id.imageView1);
    imagenow.setImageResource(imageid);

    // flag2
    int imageid2 = context.getResources().getIdentifier("croatian_flag",
            "drawable", context.getPackageName());
    ImageView imagenow2 = (ImageView) convertView
            .findViewById(R.id.imageView2);
    imagenow.setImageResource(imageid2);

    // match_date
    TextView txtmatch_date = (TextView) convertView
            .findViewById(R.id.txtDate);
    txtmatch_date.setText(itemdetail.getDate_match());

    // group
    TextView txtGroup = (TextView) convertView.findViewById(R.id.textGroup);
    txtGroup.setText(itemdetail.getGroup());

    return convertView;

MatchScheduleList.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MatchScheduleList extends Activity 

    private static final String DB_NAME = "world_cup.db";
    // *****Tables name**************************//
    private static final String TABLE_NAME = "match_list";

    // *****Tbale name******************************//

    public static final String PLACE_ID = "_id";
    public static final String STAD_NAME = "stad_name";
    public static final String TEAM1 = "team1";
    public static final String TEAM2 = "team2";
    public static final String STAGE = "stage";
    public static final String MATCH_DATE = "match_date";
    public static final String GROUP = "group_team";
    public static final String FLAG1 = "flags1";
    public static final String FLAG2 = "flags2";

    public static final String[] ALL_KEYS = new String[]  PLACE_ID, STAD_NAME,
            TEAM1, TEAM2, STAGE, MATCH_DATE, FLAG1, FLAG2, GROUP ;
    // *****Tbale name******************************//

    private SQLiteDatabase database;
    private ListView myList;
    private ArrayList<ItemDetails> list = new ArrayList<ItemDetails>();

    ItemDetails listdetail;

    private ExternalDbOpenHelper extDB;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_schedule_list);

        ExternalDbOpenHelper extDB = new ExternalDbOpenHelper(this, DB_NAME);
        database = extDB.openDataBase();


        populateLitsFromDB();
    

    public Cursor getAllRows() 
        String where = null;

        Cursor c = database.query(true, TABLE_NAME, ALL_KEYS, where, null,
                null, null, null, null);
        if (c != null) 

            c.moveToFirst();

            listdetail = new ItemDetails();
            listdetail
                    .setDate_match(c.getString(c.getColumnIndex("match_date")));

            listdetail.setGroup(c.getString(c.getColumnIndex("group_team")));

            listdetail.setTeam1(c.getString(c.getColumnIndex("team1")));

            listdetail.setTeam2(c.getString(c.getColumnIndex("team2")));

            listdetail.setStad_name(c.getString(c.getColumnIndex("stad_name")));
            listdetail
                    .setDate_match(c.getString(c.getColumnIndex("match_date")));

            listdetail.setFlag1(c.getString(c.getColumnIndex("flags1")));

            listdetail.setFlag2(c.getString(c.getColumnIndex("flags2")));

            list.add(listdetail);

        

        return c;

    

    private void populateLitsFromDB() 
        // TODO Auto-generated method stub

        Cursor cursor = getAllRows();

        // allo0w activity to manage life cicle of the cursor

        startManagingCursor(cursor);

        CustomAdapterMatchSchedule customAdapter = new CustomAdapterMatchSchedule(
                this, list);

        Log.d("in the getAllRows", customAdapter.toString());

        // set the adapter for te listView
        myList = (ListView) findViewById(R.id.list);

        myList.setAdapter(customAdapter);

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.match_schedule_list, menu);
        return true;
    


【问题讨论】:

看起来您没有将来自 DB 的值传递给 imageView。但是已经传递了两个硬编码值 brazil_flagcroatian_flag 硬编码值的图像也不显示? @挖掘你的评论是什么意思我做错了什么??以及如何根据我的代码中的名称从我的 sqlite 数据库中获取这些数据(图像)??? 我的意思是你是否通过编写以下代码获得图像int imageid = context.getResources().getIdentifier("brazil_flag", "drawable", context.getPackageName()); ImageView imagenow = (ImageView) convertView .findViewById(R.id.imageView1); imagenow.setImageResource(imageid); 是的,我得到了图像,但问题是我在可绘制文件夹中有超过 50 张图像,并且它们的名称存在于 sqlite 数据库中,所以如何使所有这些图像出现而不仅仅是 (brazil_flag) 在所有列表中?? 【参考方案1】:

而不是硬编码文本 - brazil_flag,写从 db 获取的名称。喜欢,

int imageid = context.getResources().getIdentifier(itemdetail.getFlag1(), "drawable", context.getPackageName());
ImageView imagenow = (ImageView) convertView.findViewById(R.id.imageView1);
imagenow.setImageResource(imageid);

【讨论】:

以上是关于ListView 未根据 sqlite 中的名称显示可绘制对象中的正确图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Sqlite 数据库中的特定数据上更改 Listview 中的行颜色

使用sqlite意图后如何从listview中获取项目名称?

Listview和复选框(如何放入数据库(sqlite))

如何根据sqlite数据库中的名称从drawable中获取图像,然后在列表视图中显示

PageView 内的 Listview - NotifyDatasetChanged 未调用

单击Sqlite中的List Item并根据列表显示相关列