Reputation: 121
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>
Upvotes: 1
Views: 89
Reputation: 43322
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
Upvotes: 2
Reputation: 7929
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
Upvotes: 0
Reputation: 17367
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"
Upvotes: 0