我試圖建立自定義列表視圖,包括各行上以下項目:安卓:我試圖建立自定義列表視圖,包括每行3項
- 點擊圖像
- 文本
- 可點擊圖片
我嘗試了很多,我在其他線程發現自定義適配器,但沒有成功,直到如今。兩個可點擊的圖片都將使用該行上的文字。
這是我行佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Minus"
android:src="@drawable/minus" />
<EditText
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:gravity="center_vertical|center_horizontal"
android:inputType="text" >
</EditText>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Plus"
android:src="@drawable/plus" />
</LinearLayout>
這裏是我的列表視圖:
<ListView
android:id="@+id/listViewcart"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
這裏是我當前的自定義適配器:
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.shoppingcart_row, null);
}
Item p = items.get(position);
if (p != null) {
TextView tt = (TextView) v.findViewById(R.id.item);
if (tt != null) {
tt.setText(p.getText());
}
}
return v;
}
}
這裏是我如何努力稱爲我的主類:
List test = new ArrayList<String>();
try {
test.add("test1");
test.add("test2");
test.add("test3");
ListView cart = (ListView) findViewById(R.id.listViewcart);
ListAdapter customAdapter = new ListAdapter(this, R.layout.shoppingcart_row, test);
cart.setAdapter(customAdapter);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(test);
}
我得到一個NPE。爲了以防萬一,我打印了測試列表,並且顯示我添加了3個元素。我認爲問題是在自定義適配器的某個地方,但我無法弄清楚。
我也嘗試使用ArrayList而不是列表,但我不能修改我的自定義適配器來處理。有人能看到我在這裏做錯了嗎?謝謝。
編輯:
logcat的錯誤日誌:
java.lang.NullPointerException
at com.example.hrana.za.vkushti.ShoppingCart.additems(ShoppingCart.java:109)
at com.example.hrana.za.vkushti.ShoppingCart.onCreate(ShoppingCart.java:49)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
[test1, test2, test3]
EDIT 2
爲addItems()方法:
public void additems()
{
List test = new ArrayList<String>();
try{
test.add("test1");
test.add("test2");
test.add("test3");
ListView cart = (ListView) findViewById(R.id.listViewcart);
ListAdapter customAdapter = new ListAdapter(this, R.layout.shoppingcart_row, test);
cart.setAdapter(customAdapter); // line 109
}
catch (Exception e){
e.printStackTrace();
System.out.println(test);
}
}
哪一行你得到NPE? – KEYSAN 2013-03-19 16:37:47
我剛纔在我的問題中加入了我的logcat錯誤 – 2013-03-19 16:43:59
你可以發佈'additems'方法,並顯示哪一行是109. – Kirk 2013-03-19 16:50:08