如何在listview中设置imageImageResource?

时间:2015-03-20 18:15:06

标签: android sqlite android-listview imageview

我将图像名称存储在sqlite中,我得到了带有查询的名称,我需要设置图像资源。 我试试这个:

ImageView img_categlist = (ImageView) findViewById(R.id.img_categlist);
int imageid = getResources().getIdentifier(Image[position], "drawable", getApplicationContext().getPackageName());

img_categlist.setImageResource(imageid);    

int save = db.sh_categtest("charterdb", "test");
Image = new String [save];

for (int i = 0; i <save; i++) {
    Image[i]= db.nam_nametest("charterdb", "test", i, 15);
}

但它不起作用,我应该怎么做? (图像是可绘制的,我只是在sqlite中存储名称)

完整代码:

public class Barayetest extends ListActivity {

private database db;
private String[] Name;
private String[] Price;
private String[] Image;

@Override   
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barayetest);
db = new database(this);

refresher();
setListAdapter(new AA());

}

class AA extends ArrayAdapter<String>{

public AA() {
super(Barayetest.this, R.layout.categ_item, Name);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater in = getLayoutInflater();
View row = in.inflate(R.layout.categ_item, parent, false);

TextView txt_cat = (TextView) row.findViewById(R.id.txt_cat);
txt_cat.setText(Name [position]);

TextView txt_numb = (TextView) row.findViewById(R.id.txt_numb);
txt_numb.setText(String.valueOf(position+1));

TextView txt_price = (TextView) row.findViewById(R.id.txt_price);
txt_price.setText(Price [position]);
int imageid = getResources().getIdentifier(Image[position], "drawable", getApplicationContext().getPackageName());

ImageView img_categlist = (ImageView) findViewById(R.id.img_categlist);
img_categlist.setImageResource(imageid);        

return (row);
}
}
private void refresher(){
db.open();
int save = db.sh_categtest("charterdb", "test");
Name = new String [save];
Price = new String [save];
Image = new String [save];

for (int i = 0; i <save; i++) {

Name[i]= db.nam_nametest("charterdb", "test", i, 2);
Price[i]= db.nam_nametest("charterdb", "test", i, 7);
Image[i]= db.nam_nametest("charterdb", "test", i, 15);
}


db.close();
}
    }

1 个答案:

答案 0 :(得分:0)

<强>更新 更新代码后,您必须从查看行获取 ImageView。所以请使用,

ImageView img_categlist = (ImageView) row.findViewById(R.id.img_categlist);
img_categlist.setImageResource(imageid);