我在爲自己的程序設置自定義數組適配器時遇到問題。下面是一些在下面的示例代碼:使用自定義ArrayAdapter的Android列表視圖
的XML activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow android:id="@+id/row1" >
<TextView android:text="@string/name" >
</TextView>
<EditText android:id="@+id/name" >
</EditText>
</TableRow>
<TableRow android:id="@+id/row2" >
<TextView
android:id="@+id/textView2"
android:text="@string/address" />
<EditText android:id="@+id/address" />
</TableRow>
<TableRow android:id="@+id/row3" >
<TextView
android:id="@+id/textView3"
android:text="@string/type" />
<RadioGroup android:id="@+id/group" >
<RadioButton
android:id="@+id/takeout"
android:text="@string/takeout" />
<RadioButton
android:id="@+id/delivery"
android:text="@string/delivery" />
<RadioButton
android:id="@+id/sitdown"
android:text="@string/sitdown" />
</RadioGroup>
</TableRow>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addr" />
</TableLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
爲activity_list的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="4px">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="4px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/listName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:text="TextView" />
<TextView
android:id="@+id/listAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
這是自定義一個ArrayAdapter類:
// adapter to populate the rows of the listview
private class restaurantAdapter extends ArrayAdapter<restaurant>{
public restaurantAdapter(){
super(MainActivity.this, android.R.layout.simple_list_item_1, restList);
}
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
restaurantHolder holder = null;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
// set the row of data to the specified xml formatting
row = inflater.inflate(R.layout.actitivity_list, null);
holder = new restaurantHolder(row);
row.setTag(holder);
}else{
holder = (restaurantHolder)row.getTag();
}
holder.populateFrom(restList.get(position));
return row;
}
}
// creates a holder for the adapter
private class restaurantHolder{
private TextView rowName = null;
private TextView addrName = null;
private ImageView rowIcon = null;
private View row = null;
public restaurantHolder(View row){
this.row = row;
}
public void populateFrom(restaurant r){
getName().setText(r.getName());
getAddress().setText(r.getAddress());
if(r.getMealType().equals("Delivery")){
getIcon().setImageResource(R.drawable.ball_yellow);
}else if(r.getMealType().equals("Sit Down")){
getIcon().setImageResource(R.drawable.ball_red);
}else{
getIcon().setImageResource(R.drawable.ball_green);
}
}
private TextView getName(){
if(rowName == null){
rowName = (TextView)findViewById(R.id.listName);
}
return rowName;
}
private TextView getAddress(){
if(addrName == null){
addrName = (TextView)findViewById(R.id.listAddress);
}
return addrName;
}
private ImageView getIcon(){
if(rowIcon == null){
rowIcon = (ImageView)findViewById(R.id.icon);
}
return rowIcon;
}
}
一旦holder.populateFrom方法在arrayAdapter中被調用,程序就會崩潰。我試着調試一段時間,但它只是給我一個無用的空指針異常。我覺得適配器持有者類沒有連接到activity_list xml文件,但我不確定在這一點上。任何建議都會有所幫助。
我知道它的很多代碼,但我想我只需要一個新的眼睛就可以了。謝謝您的幫助。
據我看你的代碼,它有點混亂。而且你通過擴展ArrayAdapter來創建自定義適配器,但是我建議你爲定製適配器擴展** BaseAdapter **,那會更好。如果你問,那麼我會建議你一些教程。 –