猿问

如何获取列表视图?

如何获取列表视图 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;

}

以上代码已在数据库类中实现。


皈依舞
浏览 223回答 2
2回答

动漫人物

设置你的Adapter外部while loop//get the data to the list view&nbsp; &nbsp; if(result.getCount() == 0 ){&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(StockSearchActivity.this,"Databse is empty",Toast.LENGTH_LONG).show();&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; while (result.moveToNext()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theList.add(result.getString(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theList.add(result.getString(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theList.add(result.getString(2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theList.add(result.getString(3));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theList.add(result.getString(4));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1,theList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView.setAdapter(listAdapter);&nbsp; &nbsp; }

慕运维8079593

你的问题在下一行&nbsp;&nbsp;&nbsp;&nbsp;SQLiteDatabase&nbsp;db&nbsp;=&nbsp;this.getWritableDatabase();当您尝试从数据库中获取数据时,它应该以可读模式打开,如下所示。&nbsp;&nbsp;&nbsp;&nbsp;SQLiteDatabase&nbsp;db&nbsp;=&nbsp;this.getReadableDatabase();
随时随地看视频慕课网APP

相关分类

Java
我要回答