我发现了关于同一问题的另一个问题,但代码在一个似乎不适用于我的地方有一个错误。抱歉,如果这是重复的。
我有一个 Activity 应该显示用户输入的主题列表。我的问题是,当我运行应用程序时,仅显示 ArrayList 中的第一项。老实说,我不知道错误在哪里,所以我所有的相关代码都在下面,包括 XML。我很感激任何人能给我的任何帮助。
编辑:是的,我已经调试过。光标和 ArrayList 都包含预期的项目,但 ListView 只显示第一个。
我的DatabaseHelper类中的相关函数:
public Cursor getSubjects(int level){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME + " where " + COL_3 + "="+level,null);
return res;
}
我的 Activity 类中的代码:
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);
}
以及我的 xml 文件中的相关代码:
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>
最佳答案
关于只显示1个项目,你调试过Cursor中有多少个项目吗?或者 mArrayList
中添加了多少项?尝试调试一次。
建议:
您应该在辅助类本身中编写游标迭代代码,这是为了避免在 Activity 类中编写数据库操作代码。
在 getSubjects() 方法中包含以下代码并返回 ArrayList 类型:
Cursor subjectCursor = myDb.getSubjects(1);
ArrayList<String> mArrayList = new ArrayList<String>();
for(subjectCursor.moveToFirst(); !subjectCursor.isAfterLast(); subjectCursor.moveToNext()) {
mArrayList.add(subjectCursor.getString(1));
}
关于java - 仅显示 ListView 中的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055184/