2014-02-13 27 views
0

我在为自己的程序设置自定义数组适配器时遇到问题。下面是一些在下面的示例代码:使用自定义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文件,但我不确定在这一点上。任何建议都会有所帮助。

我知道它的很多代码,但我想我只需要一个新的眼睛就可以了。谢谢您的帮助。

+0

据我看你的代码,它有点混乱。而且你通过扩展ArrayAdapter来创建自定义适配器,但是我建议你为定制适配器扩展** BaseAdapter **,那会更好。如果你问,那么我会建议你一些教程。 –

回答

0

你正在为你的列表行膨胀错误的布局。它应该是:

public restaurantAdapter(){ 
     super(MainActivity.this, R.layout.activity_list, restList); 
    } 

而不是android.R.layout.simple_list_item_1。出现NullPointerException是因为没有View,其编号为listNamelistAddressandroid.R.layout.simple_list_item_1中。

此外,考虑将文件重命名为list_item或其他内容,因为它不是活动,而是主活动中列表视图的一个项目。

+0

看来问题是我的程序在我访问第二个xml文件时崩溃。我只是尝试用一个按钮设置一些随机文本到第二个XML文件的文本视图,我的程序崩溃了。任何想法为什么发生这种情况? – TeddyG

+0

发布堆栈跟踪。 – rubenlop