繁体   English   中英

具有自定义ArrayAdapter的Android Listview

[英]Android Listview with Custom ArrayAdapter

我在为程序设置自定义阵列适配器时遇到麻烦。 以下是一些示例代码:

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;
    }       
}

只要在arrayAdapter中调用了holder.populateFrom medthod,程序就会崩溃。 我试了一段时间,但它给了我一个无用的空指针异常。 我觉得适配器持有人类没有连接到activity_list xml文件,但是目前我不确定。 任何建议都会有所帮助。

我知道很多代码,但我认为我只需要重新审视一下。 谢谢您的帮助。

您为列表行夸大了错误的布局。 它应该是:

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

而不是android.R.layout.simple_list_item_1 发生NullPointerException原因是android.R.layout.simple_list_item_1没有ID为listNamelistAddress View

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM