如何获取列表视图 android studio.this 是我的代码
public class StockSearchActivity extends AppCompatActivity {
SQLiteDatabase sqLiteDatabase;
DbHelper dbHelper;
Cursor cursor;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock_search);
listView = findViewById(R.id.display_listview);
dbHelper = new DbHelper(this);
Cursor result = dbHelper.getAllData();
final ArrayList<String> theList = new ArrayList<>();
//get the data to the list view
if(result.getCount() == 0 ){
Toast.makeText(StockSearchActivity.this,"Databse is empty",Toast.LENGTH_LONG).show();
}else{
while (result.moveToNext()){
theList.add(result.getString(0));
theList.add(result.getString(1));
theList.add(result.getString(2));
theList.add(result.getString(3));
theList.add(result.getString(4));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
}
以下代码是我的列表视图活动代码。这是正确的。代码不起作用
//getAllData
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " + tbName ,null);
return result;
}
以上代码已在数据库类中实现。
动漫人物
慕运维8079593
相关分类