0
我有方法顯示對話框與列表視圖。這裏是我的代碼: 方法:NPE在setAdapter()
private void showWindow(){
Bitmap [] img = {dz, rz, s, ww};
Dialog builder = new Dialog(myContext);
builder.setTitle("to jest list view");
CustomList adapter = new CustomList(myContext,
android.R.layout.simple_list_item_1, img);
builder.setContentView(R.layout.wybierz_kolor);
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(adapter);
builder.show();
listView1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch(position){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
});}
但本着 「listView1.setAdapter(適配器);」我有NPE。我搜索,但沒有。也許我忘了一些東西,我不知道。感謝您的幫助
@ user978758:[android.R.layout.simple_list_item_1](http://grepcode.com /file/repository.grepcode.com/java/ext/com.google.android/android/2.3.4_r1/frameworks/base/core/res/res/layout/simple_list_item_1.xml)佈局使用TextView而不是ImageView來顯示行在ListView中,所以如果你想使用任何其他視圖而不是TextView(就像你的情況ImageView),那麼你將需要通過擴展任何可用的適配器來創建一個自定義適配器 –
我有自定義適配器:public class Custom List extends ArrayAdapter { \t private final Context context; \t private final Bitmap [] obrazki; \t公共CustomList(上下文語境,INT textViewResourceId, \t \t \t位圖[] obrazki){ \t \t超級(上下文,textViewResourceId); \t \t this.context = context; \t \t this.obrazki = obrazki; \t} –
user978758
和getView方法:@Override \t公共視圖getView(INT位置,查看convertView,父的ViewGroup){ \t \t LayoutInflater充氣= LayoutInflater.from(上下文); \t \t查看rowView = inflater.inflate(R.layout.wybierz_kolor_obrazek,null); \t \t ImageView iv =(ImageView)rowView.findViewById(R.id.imageView1); \t \t iv.setImageBitmap(obrazki [position]); \t \t return rowView; \t} – user978758