Reputation: 536
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>
Upvotes: 0
Views: 327
Reputation: 536
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.
Upvotes: 1
Reputation: 128448
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));
}
Upvotes: 1