我有一個數據庫映像名稱,我有兩列 第一列的名稱:圖 第二列的名稱:名稱 我想向他們展示在列表視圖 有顯示名稱 沒有問題,但顯示圖片時出現問題解決方法是什麼?檢索源碼
Adbter列表視圖
public class adbter_listview extends ArrayAdapter<String> {
Activity activity;
ArrayList<Integer> icons;
ArrayList<String> name;
public adbter_listview(Activity activity, ArrayList<Integer> icons, ArrayList<String> name) {
super(activity, R.layout.custom_listview, name);
this.activity = activity;
this.icons = icons;
this.name = name;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View v = inflater.inflate(R.layout.custom_listview, parent, false);
ImageView iv = (ImageView) v.findViewById(R.id.imageView);
TextView tv = (TextView) v.findViewById(R.id.textView);
iv.setImageResource(icons.get(position));
tv.setText(name.get(position));
return v;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
ListView con;
ArrayList<String> icons_name = new ArrayList<>();
ArrayList<Integer> icons = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con = (ListView) findViewById(R.id.con);
db connect = new db(MainActivity.this);
SQLiteDatabase read = connect.getReadableDatabase();
Cursor save = read.rawQuery("select image from game",null);
save.moveToFirst();
while(save.isAfterLast()==false)
{
icons_name.add(save.getString(0));
icons.add(R.drawable.user);
save.moveToNext();
}
adbter_listview adb = new adbter_listview(MainActivity.this,icons,name);
con.setAdapter(adb);
}
}
請幫我
'但顯示圖片有問題?哪個問題?怎麼了? –
你的列名是Img和Name,但你選擇的是'image'?另外,你正在向圖標的數組列表添加可繪製資源,而不是從你的數據庫中刪除 –