ListView的是不显示了在可拉伸的正确图像根据它们在源码名 [英] ListView is not shows the correct images that are in the drawable according to their name in sqlite

查看:106
本文介绍了ListView的是不显示了在可拉伸的正确图像根据它们在源码名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用SQLite作为一个Android应用程序的 DB 并获取从 DB ,然后在 ListView控件显示这些数据

问题是,它不是根据自己的SQLite数据库名称,以便如何修复这个错误显示正确的图像?

谁能帮助我?

CustomAdapterMatchSchedule.java

  @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中ARG2){
    // TODO自动生成方法存根
    ItemDetails itemdetail = itemdetailsList.get(位置);    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_list_match_schedule,
                空值);
    }
    // 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
            可拉伸,context.getPackageName());
    ImageView的imagenow =(ImageView的)convertView
            .findViewById(R.id.imageView1);
    imagenow.setImageResource(imageid);    // FLAG2
    INT imageid2 = context.getResources()则getIdentifier(croatian_flag
            可拉伸,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());    //组
    TextView的txtGroup =(TextView中)convertView.findViewById(R.id.textGroup);
    txtGroup.setText(itemdetail.getGroup());    返回convertView;
}

MatchScheduleList.java

 包com.devleb.expandablelistdemo3;进口的java.util.ArrayList;进口android.os.Bundle;
进口android.app.Activity;
进口android.app.ListActivity;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.support.v4.widget.SimpleCursorAdapter;
进口android.util.Log;
进口android.view.Menu;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;公共类MatchScheduleList延伸活动{    私有静态最后弦乐DB_NAME =world_cup.db;
    // *****表名************ //
    私有静态最后弦乐TABLE_NAME =match_list;    // ***** Tbale名************ //    公共静态最后弦乐PLACE_ID =_id;
    公共静态最后弦乐STAD_NAME =stad_name;
    公共静态最后弦乐TEAM1 =TEAM1;
    公共静态最后弦乐TEAM2 =TEAM2;
    公共静态最后弦乐STAGE =舞台;
    公共静态最后弦乐MATCH_DATE =match_date;
    公共静态最后弦乐GROUP =group_team;
    公共静态最后弦乐FLAG1 =FLAGS1;
    公共静态最后弦乐FLAG 2 =flags2;    公共静态最后的String [] = ALL_KEYS新的String [] {PLACE_ID,STAD_NAME,
            TEAM1,TEAM2,舞台,MATCH_DATE,FLAG1,FLAG2,GROUP};
    // ***** Tbale名************ //    私人SQLiteDatabase数据库;
    私人的ListView myList中;
    私人的ArrayList< ItemDetails>名单=新的ArrayList< ItemDetails>();    ItemDetails listdetail;    私人ExternalDbOpenHelper extDB;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_match_schedule_list);        ExternalDbOpenHelper extDB =新ExternalDbOpenHelper(这一点,DB_NAME);
        数据库= extDB.openDataBase();
        populateLitsFromDB();
    }    公共光标getAllRows(){
        字符串,其中= NULL;        光标C = database.query(真,TABLE_NAME,ALL_KEYS,其中,空,
                NULL,NULL,NULL,NULL);
        如果(C!= NULL){            c.moveToFirst();            listdetail =新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);        }        返回℃;    }    私人无效populateLitsFromDB(){
        // TODO自动生成方法存根        光标光标= getAllRows();        // allo0w活动管理光标生活cicle        startManagingCursor(光标);        CustomAdapterMatchSchedule customAdapter =新CustomAdapterMatchSchedule(
                这一点,清单);        Log.d(在getAllRows,customAdapter.toString());        //设置适配器TE的ListView
        myList中=(ListView控件)findViewById(R.id.list);        myList.setAdapter(customAdapter);    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.match_schedule_list,菜单);
        返回true;
    }}


解决方案

而不是硬codeD文本 - brazil_flag ,写名字从数据库中取出。就像,

  INT imageid = context.getResources()则getIdentifier(itemdetail.getFlag1(),绘制,context.getPackageName());
ImageView的imagenow =(ImageView的)convertView.findViewById(R.id.imageView1);
imagenow.setImageResource(imageid);

I have an android application that use sqlite as DB and fetch data from DB then display these data in the ListView.

The problem is that it is not display the correct images according to their names in the sqlite database so how to fix this error?

can anyone help me?

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

}

解决方案

Instead of hard-coded text - brazil_flag, write the name fetched from db. Like,

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

这篇关于ListView的是不显示了在可拉伸的正确图像根据它们在源码名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆