我遇到了为listView设置的自定义布局问题。我有一个按钮,它向listView添加元素,它使用自定义行,但每次我尝试按下按钮添加崩溃。任何人都可以建议如何解决这个问题的解决方案?我对android btw很新。
以下是代码:
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);
}
});
}
}
此活动使用的布局:
<?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>
答案 0 :(得分:2)
修改:您需要向R.id.customRow
构造函数提供ArrayAdapter
。
这应该解决它:
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
答案 1 :(得分:0)
Activity中没有名为StartProject的公共方法。删除此属性:
android:onClick="StartProject"
并修复layout_below:
android:layout_below="@id/addBtn"
答案 2 :(得分:0)
尝试更改
ArrayAdapter<String>(this, R.layout.custom_row, listItems);
到
ArrayAdapter<String>(this, R.layout.custom_row, R.id.customRow, listItems);