Android 列表视图自定义行

Android listview custom rows

我为 listView 设置的自定义布局有问题。我有一个将元素添加到 listView 的按钮,它使用自定义行,但每次我尝试按下按钮添加时它都会崩溃。谁能就如何解决这个问题提出解决方案?我对 android 很陌生。

代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class ProjectCreateScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondary_layout1);

    Button btn = (Button) findViewById(R.id.addBtn);

    final ArrayList<String> listItems=new ArrayList<String>();
    final ListAdapter addAdapter = new ArrayAdapter<String>(this, R.layout.custom_row,
            listItems);
    ListView lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(addAdapter);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listItems.add("New Project");
            ((ArrayAdapter) addAdapter).notifyDataSetChanged();
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent switchToEdit = new Intent(ProjectCreateScreen.this, teamCreateScreen.class);
            startActivity(switchToEdit);
        }
    });
}

}

此 activity 使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl">

<Button
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:text="@string/AddProject"
    android:id="@+id/addBtn"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="StartProject"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/addBtn"
    android:id="@+id/lv">
    </ListView>

</RelativeLayout>

自定义行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:id="@+id/customRow"/>

</LinearLayout>

编辑:您需要向 ArrayAdapter 构造函数提供 R.id.customRow

这应该可以解决问题:

 final ListAdapter addAdapter = new ArrayAdapter<String>(this, R.layout.custom_row, R.id.customRow,
                listItems);

使用您的原始代码,我向 ArrayList 添加了一些数据,但出现了以下异常:

04-09 13:12:34.992  12418-12418/com.listviewtest.daniel.listviewtest E/ArrayAdapter﹕ You must supply a resource ID for a TextView
04-09 13:12:34.992  12418-12418/com.listviewtest.daniel.listviewtest D/AndroidRuntime﹕ Shutting down VM
04-09 13:12:34.992  12418-12418/com.listviewtest.daniel.listviewtest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41891da0)
04-09 13:12:35.012  12418-12418/com.listviewtest.daniel.listviewtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.listviewtest.daniel.listviewtest, PID: 12418
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Activity 中没有名为 StartProject 的 public 方法。删除此属性:

android:onClick="StartProject"

并修复 layout_below:

android:layout_below="@id/addBtn"

尝试改变

ArrayAdapter<String>(this, R.layout.custom_row, listItems);

ArrayAdapter<String>(this, R.layout.custom_row, R.id.customRow, listItems);

看看ArrayAdapter constructor params