Android ListView 中的自定义列表项

Android custom list item in ListView

我试图制作一个简单的自定义 ListView 列表项来帮助我理解它是如何工作的,但它不起作用。

当我在 MainActivity.java 中使用注释行时,一切正常(它使用内置布局)。 但是当我尝试自己的布局 row_layout 而不是 simple_list_item_1 时,我在启动程序时遇到了 ANR。

MainActivity.java

public class MainActivity extends Activity {

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


    String[] colors = {"blue", "red", "yellow", "green", "purple", "orange"};

    //ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, colors);
    ListAdapter listAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, colors);

    ListView listView = (ListView)findViewById(R.id.theListView);

    listView.setAdapter(listAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String auswahl = "Auswahl:" + String.valueOf(parent.getItemAtPosition(position));

            Toast.makeText(MainActivity.this, auswahl, Toast.LENGTH_LONG).show();
        }
    });
}

}

activity_main.xml

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/theListView"></ListView></LinearLayout>

row_layout.xml

<LinearLayout 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:orientation="horizontal"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:textSize="30sp"
    android:textStyle="bold"
    android:padding="15dp"/></LinearLayout>

将文本视图 ID 更改为

android:id="@android:id/text1"

调用适配器时,您需要在自定义行中提供 TextView 的 ID。

ListAdapter listAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.textView1, colors);

您的列表适配器无法知道您的文本必须如何出现在您的自定义布局上。

显示您必须创建一个自定义适配器来绘制您的列表视图行,或者您可以将 row_layout.xml 替换为

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

并在该文本视图上进行更改。

(其实就是simple_list_item_1的代码)

最有效的方法是创建自己的适配器

public class YourAdapter extends BaseAdapter {

    private final Context context;
    private TextView mTextView
    private LayoutInflater inflater;
    private String[] colors;

    public YourAdapter(Context context, String[] colors) {
        this.context = context;
        this.colors = colors;

    }

    /**
     * How many items are in the data set represented by this Adapter.
     */
    @Override
    public int getCount() {
        return colors.length;
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return position;
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view  = convertView;



        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) view = inflater.inflate(R.layout.row_layout,              null);
        mTextView = view.findItemById(R.id.textview1);
        String color = colors[position];
        mTextView.setText(color);

        return view;
    }




    }