Only first item in ListView is displaying - java

I found another question about this same problem but the code had an error in a place that doesn't seem to apply to mine. Sorry if this is a repeat.
I have an activity that should display a list of subjects that have been entered by the user. My problem is that when I run the app, only the first item in the ArrayList is being displayed. I honestly have no clue where the error is, so all my relevant code is below, including the XML. I'd appreciate any help anyone could give me.
EDIT: yes I have debugged. Both the cursor and ArrayList contain the expected items, but the ListView is only displaying the first one.
The relevant function in my DatabaseHelper class:
public Cursor getSubjects(int level){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME + " where " + COL_3 + "="+level,null);
return res;
}
The code from my activity class:
public class Level1Activity extends Activity implements OnClickListener {
private Button subject1Btn, level2Btn, level3Btn;
DatabaseHelper myDb;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
myDb = new DatabaseHelper(this);
//set up buttons
subject1Btn = (Button)findViewById(R.id.subject1);
level2Btn = (Button)findViewById(R.id.Level2);
level3Btn = (Button)findViewById(R.id.Level3);
level2Btn.setOnClickListener(this);
level3Btn.setOnClickListener(this);
lv = (ListView)findViewById(R.id.subjectList);
Cursor subjectCursor = myDb.getSubjects(1);
ArrayList<String> mArrayList = new ArrayList<String>();
for(subjectCursor.moveToFirst(); !subjectCursor.isAfterLast(); subjectCursor.moveToNext()) {
mArrayList.add(subjectCursor.getString(1));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, R.layout.subject_list_item, mArrayList);
lv.setAdapter(arrayAdapter);
}
And the relevant code from my xml files:
subject_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="10dip"
android:textSize="16dip"
android:textColor="#ffffffff"
android:textStyle="bold" >
</TextView>
activity_level1.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/app_back">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp">
<Button
android:id="#+id/subject1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:onClick="onClick"
android:text="Subject 1"
android:textColor="#ff000000"
android:textSize="20sp"
android:textStyle="bold"
android:background="#ffff0000"
android:layout_margin="4dp"/>
<ListView
android:id="#+id/subjectList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:id="#+id/newSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:background="#ff444444"
android:onClick="onClick"
android:text="+"
android:textColor="#ffff0000"
android:textSize="80sp" />
</LinearLayout>
</ScrollView>

Regarding only 1 item is displaying, did you debug how many items are there in Cursor? Or how many items are getting added in mArrayList? Try debugging it once.
Suggestion:
You should write cursor iteration code in helper class itself, this is to avoid database operations code in Activity class.
Include below code in getSubjects() method and return ArrayList type:
Cursor subjectCursor = myDb.getSubjects(1);
ArrayList<String> mArrayList = new ArrayList<String>();
for(subjectCursor.moveToFirst(); !subjectCursor.isAfterLast(); subjectCursor.moveToNext()) {
mArrayList.add(subjectCursor.getString(1));
}

I figured out an answer to my problem. Thanks everyone for the help and suggestions with debugging.
If anyone else is having this same problem here is the solution:
ListView itself is scrollable. It will not work inside of a ScrollView, hence in my case it was only displaying one a single item from an ArrayList of several. By taking it out of the ScrollView you can assign the height you want it to take up on your screen and it will scroll through the items automatically.

Related

Checkboxes of a ListView are changing theirs values when the List is scrolled

I'm using Android Studio to develop an application and i'm having a problem. In my app, there is a ListView that shows subjects added to a database using an arrayAdapter and the list gives you the option to select or not a subject (with checkboxes). Here is the XML file with the ListView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cliente.matriadada.AddTurmaActivity">
<ListView
android:id="#+id/listaConteudosTurma"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:choiceMode="multipleChoice"
android:clickable="true"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
</android.support.constraint.ConstraintLayout>
Here is the Activity code (in Java):
public class AddTurmaActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_turma);
ListView lista = (ListView) findViewById(R.id.listaConteudosTurma);
MainActivity.BancoController crud = new MainActivity.BancoController(getBaseContext());
Cursor cursor = crud.carregaConteudos();
ArrayList<String> valores = new ArrayList<>();
if (cursor.moveToFirst()){
do {
valores.add(cursor.getString(1));
} while(cursor.moveToNext());
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,
R.layout.linha_com_check_layout, R.id.txtComCheck, valores);
lista.setAdapter(adaptador);
}
}
And finally the XML file that the adaptor uses:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtComCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxLista"
android:layout_toEndOf="#+id/checkBoxLista"
android:layout_toRightOf="#+id/checkBoxLista"
android:text="TextView"
android:textSize="18sp" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtComCheck" />
</android.support.constraint.ConstraintLayout>
When I launch my app and check some checkboxes, if I scroll the list other checkboxes get checked too. I think it's a problem with RecyclerView or something like this.
How can I fix this?
View Holder holds your views for a fixed position, and when this position is shown on the screen it will extract the view.
Please follow this link
Using ViewHolder
which helped me quite a while ago

ListView only shows first item

I want to display all items of an ArrayList<String> on a ListView. I use an ArrayAdapter<String> for this. The problem is that only the first item ('foo' in my example) of the ArrayList is shown. Can someone tell me why? Am I missing something?
I've made an minimal example using the generated code (Tabbed Action Bar Spinner Activity) from Android Studio 2.
My FooActivity:
public class FooActivity extends AppCompatActivity {
...
public static class PlaceholderFragment extends Fragment {
private ListView fooListView;
private ArrayAdapter<String> mAdapter;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_foo, container, false);
fooListView = (ListView) rootView.findViewById(R.id.foo_list);
updateList();
return rootView;
}
private void updateList() {
ArrayList<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getContext(),
R.layout.item_foo,
R.id.foo_string,
strings);
fooListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
}
My fragment_foo.xml:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.foo.activities.FooActivity$PlaceholderFragment">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/foo_list"
android:layout_below="#+id/total_activities"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And the item_foo.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/foo_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_string"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
If it helps I can post the generated activity_foo.xml (I didn't made any changes there) and the complete FooActivity class too.
The Problem actually was my activity_foo.xml which had a NestedScrollView. I've added android:fillViewport="true" and now it shows every item.
My NestedScrollView now looks like this:
<android.support.v4.widget.NestedScrollView
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true"/>
On any ListView, if you use android:layout_height="wrap_content", you're only going to see one item. Try android:layout_height="match_parent". If the ListView only takes up part of the layout, you'll need a LinearLayout with weights or a RelativeLayout with constraints. But wrap_content won't work for a height on a ListView, ever.
Your Listview look something like this
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/foo_list"
android:layout_marginTop="10dp"/>
I have removed from your list view android:layout_below="#+id/total_activities"
and
android:layout_alignParentBottom="true"
For others: It's okay to nest a ListView inside another layout, and it's also fine to use android:layout_height="wrap_content" on Layouts surrounding the ListView. (So long as the outer-most Layout allows for match_parent)
What is not fine, however, is when the TextView being used to populate the ListView is defined with layout_height="match_parent". It will cover up all of the subsequent rows after the first.
Bad:
<TextView
android:id="#+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Good:
<TextView
android:id="#+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

Dynamic list of checkboxes with onclick functions

I have a page that returns a list of items backs from a database. I want to add each item to my android fragment as a checkbox dynamically with an onClick, that can tell if an item is being checked or un-checked.
How can I add checkboxes dynamically with on-clicks and different titles for each?
Below is the xml I am inserting the checkboxes into:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="#+id/styleDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox" />
</LinearLayout >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="horizontal"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<Button
android:id="#+id/buttonAddList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create List"
style="?android:attr/borderlessButtonStyle"
android:textColor="#color/orange"
android:textStyle="bold"
/>
</LinearLayout >
</FrameLayout>
</LinearLayout>
</ScrollView>
I currently have one checkbox in the above code. I plan on removing this. That checkbox is just to show where I want my check boxes to show up.
What you need to do first is add an id to your LinearLayout (in that XML file), the one which is going to hold the CheckBoxes. Then, in the code you need to get that LinearLayout by its id and use addView() to add CheckBoxes that you create dynamically. I imagine in pseudocode it'd look like this:
for (int i = 0; i < numberOfCheckBoxes; i++) {
CheckBox checkBox = new CheckBox();
checkBox.setTitle("Your title");
checkBox.setOnClickListener(new OnClickListener() {
// Your code to be executed on click
});
linearLayout.addView(checkBox);
}
Does this help?
PS: It'd be nice if you kept your code clean - ADT (and I believe Eclipse too) gives you the Shift+Ctrl+F shortcut to indent your code automatically - use it as often as possible ;)
Since you are processing database items, I suggest using a CursorAdapter to do the heavy work for you. A CursorAdapter, like any of the Adapter classes can process the database items and custom-fit them into a layout of your choice, to use in a ListView.
You have to make adjustments to your code:
Create a layout file that contains whatever you want to put in the dynamic list. This is an example, say it's named list_contents.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="#drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="#+id/styleDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox" />
</LinearLayout >
</FrameLayout>
Then, instead of returning a List from your AsyncTask, return the Cursor itself
from your database. This Cursor will be processed by CursorAdapter. I recommend this guide:
http://www.gustekdev.com/2013/05/custom-cursoradapter-and-why-not-use.html
Implement the CursorAdapter methods:
In your implementation of newView(), inflate list_contents.xml (Note that if you use ResourceCursorAdapter you wouldn't need to do this)
In your implementation of CursorAdapter.bindView() do this:
CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
checkbox.setText(cursor.getString(cursor.getColumnIndex(YOUR_DATABASE_COLUMN_NAME_FOR_CHECKBOX_VALUES)));
checkbox.setOnCheckedChangedListener(listenerInitializedSomewhereFromFragmentCode);
Change your ScrollView to a ListView (it can be inside any Layout), and give it an id, say R.id.listview.
Finally, in the part where you process the List from the database, where we now have a Cursor instead, just do this:
CustomCursorAdapter cca = new CustomCursorAdapter(getActivity(), resultFromDatabase, 0);
listView.setAdapter(cca);
Note: getActivity() is for when you are working inside a Fragment. It should be a context, so inside an Activity it can just be "this".
Note2: listView should have been initialized at this point via findViewById.
Note3: If listView already has an Adapter and Cursor set, you should consider calling listView.getAdapter().changeCursor() instead.
Simple Code In Kotlin
fun createCheckbox() {
var arr_cb = arrayOfNulls<CheckBox>(checkbox_size)
val layout = findViewById<View>(R.id.layout_checkbox) as ViewGroup
val ll = LinearLayout(this)
ll.orientation = LinearLayout.VERTICAL
for (i in 0 until arr_cb.size) {
arr_cb[i] = CheckBox(this)
arr_cb[i]?.text = health_status.get(i).toString()
arr_cb[i]?.setPadding(25, 0, 0, 0)
arr_cb[i]?.id = i
arr_cb[i]?.tag = health_status[i]
arr_cb[i]?.setTextColor(resources.getColor(R.color.title_color))
arr_cb[i]?.setOnCheckedChangeListener(
arr_cb[i]?.let {
handleCheck(it)
})
arr_cb[i]?.buttonTintList =
ColorStateList.valueOf(resources.getColor(R.color.theme_color))
ll.addView(arr_cb[i])
}
layout.addView(ll)
}
handleCheck method
private fun handleCheck(chk: CheckBox): CompoundButton.OnCheckedChangeListener? {
return object : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
if (!isChecked) {
//uncheck
} else {
//check
}
}
}
}
and you want to do something use direct checkboxList object like as
val layout = findViewById<View>(R.id.layout_checkbox) as ViewGroup
val ll = LinearLayout(this#MedicalHistoryActivity)
ll.orientation = LinearLayout.VERTICAL
for (i in 0 until health_status.size) {
arr_cb[i]?.isEnabled = true
// do something like change color or more
}
layout.addView(ll)
for enable checkbox or more.
Thank you

I want to put multiple values from the SQLite database in a listview row

Android beginner here, I am having trouble populating items in a list view. I can always get one of the database entry values to appear but I would like to have two items per list row. Here is my class for populating the listview. I want to bring the Values for KEY_TITLE and KEY_BODY into a listview. Ideally, I would like KEY_BODY to be a sub item of the listview, but I think that may require a two item listview which I do not know how to implement, even after reading the API's.
#SuppressWarnings("deprecation")
private void fillData() {
Cursor moviesCursor = mDbHelper.fetchAllMovies();
startManagingCursor(moviesCursor);
String[] from = new String[]{
MyMoviesDBAdapter.KEY_ROWID,
MyMoviesDBAdapter.KEY_BODY,
MyMoviesDBAdapter.KEY_TITLE
};
int[] to = new int[] {
R.id.text3,
R.id.text2,
R.id.text1
};
ListAdapter movies = new SimpleCursorAdapter(this, R.layout.listtextview,
moviesCursor, from , to);
moviesCursor.moveToNext();
setListAdapter(movies);
}
Here is my XML layout
<LinearLayout >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#a4a4a4"
android:textSize="10sp"
/>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#ffffff"
android:textSize="24sp"
/>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip"
android:textColor="#a4a4a4"
android:textSize="15sp"
/>
</LinearLayout>
You have missed BaseColumns._ID
String[] from = new String[]{
MyMoviesDBAdapter.KEY_ROWID,
MyMoviesDBAdapter.KEY_BODY,
MyMoviesDBAdapter.KEY_TITLE,BaseColumns._ID
};
The Problem was in my XML layout. I neglected to specify details in the parent "linear layout" which caused my runtime exception. Hope this helps somebody

ListAdapter from a HashMap

I'm trying to get my ArrayList of HashMaps to try to get a working ListAdapter. When I run this, I'm getting a
java.lang.RuntimeException: Your content must have a ListView whose id attribute is android.R.id.list
error. Here's what I have:
gameList is where I'm getting my data from:
gameList.toString() returns: [{turn=1, opponent=UserTwo, streak=4, hintX=1234, player=UserOne, solution=MySolution, hintY=5678}, {turn=0, opponent=UserThree, streak=12, hintX=1344, player=UserOne, solution=SolutionTwo, hintY=5428}]
My ListActivity class:
public class GameListActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_list);
String GAME_ITEM = "game";
String PLAYER_ITEM = "player";
String OPPONENT_ITEM = "opponent";
String SOLUTION_ITEM = "solution";
String HINTX_ITEM = "hintX";
String HINTY_ITEM = "hintY";
String STREAK_ITEM = "streak";
String TURN_ITEM = "turn";
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
AppVars gameVars = ((AppVars) getApplicationContext());
gameList = gameVars.getState();
String[] keyList = new String[] { OPPONENT_ITEM, STREAK_ITEM, TURN_ITEM };
ListAdapter adapter = new SimpleAdapter(this, gameList, R.layout.game_list_item,
keyList,
new int[] {R.id.opponent, R.id.streak, R.id.turn});
setListAdapter(adapter);
}
XML:
game_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:src="#android:drawable/alert_dark_frame" />
<TextView
android:id="#+id/opponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Entry"
android:textSize="30sp" />
<TextView
android:id="#+id/streak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Streak"
android:textSize="20sp" />
<TextView
android:id="#+id/turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/streak"
android:layout_centerHorizontal="true"
android:text="Your turn!"
android:textSize="20sp" />
</RelativeLayout>
game_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blackboard" >
<ListView
android:id="#+id/gameList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is android.R.id.list
So error log tells you everything
You need to add to your XML file of ListView android.R.id.list
<ListView
android:id="#android:id/list"
// next attributes
/>
Exactly when your Activity extends from ListActivity, you should to use this.
Here is info from docs
ListActivity has a default layout that consists of a single,
full-screen list in the center of the screen. However, if you desire,
you can customize the screen layout by setting your own view layout
with setContentView() in onCreate(). To do this, your own view MUST
contain a ListView object with the id "#android:id/list" (or list if
it's in code)
So for more information have look at ListActivity.
Mind that When you extends ListActivity Then your xml file must have listview which id is
#android:id/list.
USe #android:id/list
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
You are extending ListActivity, so your view should have list defined with id "list"
See this link for working example.
id of list is "#+id/gameList" should be "#android:id/list" as you are using ListActivity...
Remove the following line from your code:
setContentView(...)
ListActivity handles the ListView for you so you access the ListView using this.
If you need a more complex layout subclass Activity instead.

Categories