问题
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;
}
}
回答1:
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);
来源:https://stackoverflow.com/questions/23338816/listview-is-not-shows-the-correct-images-that-are-in-the-drawable-according-to-t