简体   繁体   中英

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. the listview uses an adapter that i queried in the 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>
`

First of all you need to call show() method of dialog like written below,

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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