简体   繁体   English

Android Listview 和 Dialog 不会显示

[英]Android Listview and Dialog wont display

**Good day everyone ... **今天是个好日子 ...

I badly need your help.我非常需要你的帮助。

Can I ask on why my listview doesnt display and also my alert dialog?我可以问为什么我的列表视图不显示以及我的警报对话框吗?

here is my code I've already inserted table values in the sqlite database and Im confused on why nothing displays on my listview and dialog.这是我的代码,我已经在 sqlite 数据库中插入了表值,我很困惑为什么我的列表视图和对话框上没有显示任何内容。 the listview uses an adapter that i queried in the DBHelper列表视图使用我在 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活动_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>
`

First of all you need to call show() method of dialog like written below,首先,您需要调用对话框的 show() 方法,如下所示,

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

Replace line替换线

ArrayList getQuestion=commandQuery.getAllQuestions();

with

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

And check whether you are getting atleast one data from your database(check list size)并检查您是否从数据库中获取了至少一个数据(检查列表大小)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM