1

I have a problem with a custom layout I've set for my 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. Can anyone suggest a solution on how to fix this? I'm pretty new to 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>
5
  • it simply says that the program 'stopped working', nothing on logcat Commented Apr 9, 2015 at 19:44
  • You may not be getting all of your logcat messages, if it crashes there should be a log trace. Commented Apr 9, 2015 at 19:47
  • 1
    Try to change ArrayAdapter<String>(this, R.layout.custom_row, listItems); to ArrayAdapter<String>(this, R.layout.custom_row, R.id.customRow, listItems); . I think you need to refers the id of text in android resource Commented Apr 9, 2015 at 20:20
  • @Rami I just noticed that you posted your comment before the final edit on my answer. Good eye! I was busy running the code and I didn't see your comment. Commented Apr 9, 2015 at 20:45
  • 1
    @DanielNugent Thanks... I must admit that it wasn't easy to figure out the reason of the problem... +1 to your answer, for solving it Commented Apr 9, 2015 at 21:10

3 Answers 3

2

Edit: You need to provide R.id.customRow to the ArrayAdapter constructor.

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:

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
Sign up to request clarification or add additional context in comments.

3 Comments

It didn't help and the layouts are in the order they're supposed to be in it's just something about that custom_row layout and the adapter that I'm using that's keeping the program from working properly
the activity uses secondary_layout1, the listView uses custom_row
it worked perfectly when i used the simple_list_item1 but when i added the custom_row it's no longer doing it's thing
0

There is no public method named StartProject in the Activity. Remove this attribute:

android:onClick="StartProject"

and fix the layout_below:

android:layout_below="@id/addBtn"

4 Comments

you're right it's no longer necessary but it didn't fix the problem :/
remove the "+" from layout_below
why does that need fixing? The listView is supposed to be below the button
once again, removing the '+' doesn't change anything
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.