Android custom list item in ListView

2.9k views Asked by At

I tried to make an simple custom ListView listitem to help me understand how this works but it won't work.

When I use the commented line in my MainActivity.java, everything works (it uses the built-in layout). But when I try my own layout row_layout instead of the simple_list_item_1 I get an ANR when starting the program.

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

There are 4 answers

0
Bidhan On BEST ANSWER

You need to provide the id of the TextView inside your custom row when you're calling the adapter.

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

Change the text view id to

android:id="@android:id/text1"
0
Hamilton On

Your list adapter cannot know how your text must be appareared on your custom layout.

Show either you must create a custom adapter that will draw your listview rows or you can replace your row_layout.xml with

<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" />

and make your changes on that textview.

(this is the code of the simple_list_item_1 in fact)

1
christoandrew On

The most efficient way to do this is to create your own adapter

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




    }