简体   繁体   English

Android ListView自定义行

[英]Android listview custom rows

I have a problem with a custom layout I've set for my listView. 我为listView设置的自定义布局有问题。 I have a button that adds elements to the listView and it uses a custom row but everytime I try to press the button to add it crashes. 我有一个将元素添加到listView的按钮,它使用自定义行,但是每次我尝试按该按钮添加它时都会崩溃。 Can anyone suggest a solution on how to fix this? 谁能提出解决此问题的解决方案? I'm pretty new to android btw. 我是android btw的新手。

Here's the code: 这是代码:

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

} }

The layout this activity uses: 此活动使用的布局:

<?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>

The custom row layout: 自定义行布局:

<?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>

Edit: You need to provide R.id.customRow to the ArrayAdapter constructor. 编辑:您需要向ArrayAdapter构造函数提供R.id.customRow

This should fix it: 这应该解决它:

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

With your original code, I added some data to the ArrayList, and got this exception: 使用您的原始代码,我向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

There is no public method named StartProject in the Activity. 活动中没有名为StartProject的公共方法。 Remove this attribute: 删除此属性:

android:onClick="StartProject"

and fix the layout_below: 并修复layout_below:

android:layout_below="@id/addBtn"

Try to change 尝试改变

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

to

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

Look at ArrayAdapter constructor params 看一下ArrayAdapter构造函数的参数

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

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