Android Listview和对话框将不会显示

问题描述 投票:1回答:1

**大家好...

我非常需要您的帮助。

我可以问为什么我的列表视图为什么不显示以及我的警报对话框吗?

这是我的代码我已经在sqlite数据库中插入了表值,我对为什么列表视图和对话框中什么都不显示感到困惑。listview使用我在DBHelper中查询的适配器

MainActivity

public class MainActivity extends AppCompatActivity {

    private QuestionaireDB commandQuery;
    private ListView showQuestion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        commandQuery = new QuestionaireDB(this);

        ShowTables();

        ArrayList getQuestion = commandQuery.getAllQuestions();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getQuestion);
        showQuestion = (ListView)findViewById(R.id.viewAll);
        showQuestion.setAdapter(arrayAdapter);
    }

    private  void ShowTables(){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("You have "+commandQuery.numberOfRows()+"");
        alertDialogBuilder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();

            }
        });
    }
}

QuestionaireDB

public class QuestionaireDB extends SQLiteOpenHelper{
    //tblQuestion
    public static final String DATABASE_NAME = "QuestionaireDB.db";
    public static final String DATABASE_TABLE_tblQuestion = "tblQuestion";
    public static final String DATABASE_TABLE_tblChoices = "tblChoices";
    public static final String DATABASE_TABLE_tblAnswers = "tblAnswers";
    public static final String DATABASE_TABLE_tblCategory = "tblCategory";

    public QuestionaireDB(Context context) {
        super(context, DATABASE_NAME , null, 3);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        //tblQuestion
        String CreatetblQueston="CREATE TABLE "+DATABASE_TABLE_tblQuestion +
                "(questionID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,categoryID INTEGER, 
      questionName TEXT)";
        //tblChoices
        String CreatetblChoices="CREATE TABLE "+DATABASE_TABLE_tblChoices +
                "(questionID INTEGER NOT NULL, ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    questionChoices  TEXT)";
        //tblAnswers
        String CreatetblAnswers="CREATE TABLE "+DATABASE_TABLE_tblAnswers+
                "(questionID INTEGER NOT NULL, questionCorrectAnswer TEXT, ID INTEGER PRIMARY KEY 
       AUTOINCREMENT NOT NULL)";
        //tblCategory
        String CreateCategory="CREATE TABLE "+DATABASE_TABLE_tblCategory+
               " (categoryID  INTEGER NOT NULL,categoryName TEXT, PRIMARY KEY (categoryID ASC))";
        db.execSQL(CreatetblQueston);
        db.execSQL(CreatetblChoices);
        db.execSQL(CreatetblAnswers);
        db.execSQL(CreateCategory);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String confirmationQuestion="DROP TABLE IF EXISTS "+DATABASE_TABLE_tblQuestion ;
        String confirmationChoices="DROP TABLE IF EXISTS "+DATABASE_TABLE_tblChoices ;
        String confirmationAnswers="DROP TABLE IF EXISTS "+DATABASE_TABLE_tblAnswers ;
        String confirmationCategory="DROP TABLE IF EXISTS "+DATABASE_TABLE_tblCategory ;
        db.execSQL(confirmationQuestion);
        db.execSQL(confirmationChoices);
        db.execSQL(confirmationAnswers);
        db.execSQL(confirmationCategory);
    }
       public ArrayList<String> getAllQuestions() {
         ArrayList<String> array_list = new ArrayList<String>();

         SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from tblQuestion", null );
        res.moveToFirst();

        while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex("questionName")));
            res.moveToNext();
        }
        return array_list;
    }

    public int numberOfRows(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, DATABASE_TABLE_tblQuestion);
        return numRows;
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      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=".MainActivity">

   <ListView
        android:id="@+id/viewAll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
`
android android-studio android-sqlite sqliteopenhelper
1个回答
1
投票

首先,您需要调用如下所示的对话框的show()方法,

alertDialogBuilder.show(); //add this line in your ShowTables()

替换行

ArrayList getQuestion=commandQuery.getAllQuestions();

with

ArrayList<String> getQuestion=commandQuery.getAllQuestions();

并检查是否从数据库中获取了至少一个数据(检查列表大小)

© www.soinside.com 2019 - 2024. All rights reserved.