2017-08-09 7 views
0

sqliteのすべてのデータをリストビューに表示するにはどうすればいいですか?すべてのデータフォームのsqliteデータベースをリストビューに取り込む方法は?

「すべて表示」ボタンをクリックするたびに、複数のデータを追加しても1つのデータしか表示されません。私はどのように私のすべてのデータを表示できますか?

ありがとうございました!

ここに私のコードは次のとおりです。

MainActivity.java

if(view==btnViewAll){ 
     setContentView(R.layout.activity_view_all); 

     TextView id = (TextView)findViewById(R.id.idno); 
     TextView name = (TextView)findViewById(R.id.name); 
     TextView grade = (TextView)findViewById(R.id.grade); 
     TextView school = (TextView)findViewById(R.id.school); 
     TextView program = (TextView)findViewById(R.id.program); 
     TextView course = (TextView)findViewById(R.id.course); 
     TextView email = (TextView)findViewById(R.id.email); 
     TextView bday = (TextView)findViewById(R.id.bday); 
     TextView address = (TextView)findViewById(R.id.address); 
     TextView gender = (TextView)findViewById(R.id.gender); 

     Cursor c = db.rawQuery("SELECT * FROM student", null); 

     if (c.moveToFirst()){ 
      id.setText(c.getString(0)); 
      name.setText(c.getString(1)); 
      grade.setText(c.getString(2)); 
      school.setText(c.getString(3)); 
      program.setText(c.getString(4)); 
      course.setText(c.getString(5)); 
      email.setText(c.getString(6)); 
      bday.setText(c.getString(7)); 
      address.setText(c.getString(8)); 
      gender.setText(c.getString(9)); 
     } 
    } 
} 

Here is the layout where i want to display all my data

enter image description here

答えて

0

非常に軽いコードで行うと余分なコードが必要になるのはなぜですか。

まず、必要なデータのPOJOまたはBeanクラスを作成します。

public class Student { 
    String ID,name,grade,school,program,course,email,bday,address,gender; 
    (getter and setter) 
} 

次は、単に「学生」クラスのリストを作成し、このようなSQLiteのクエリを呼び出す -

List<Student> studentDetailsList= new ArrayList<>(); 
studentDetailsList = db.rawQuery("SELECT * FROM student", null); 

、あなたは簡単にアクセスすることができます。

0

あなたが最初にあなたの質問を明確にすることはできますか?生徒データベースに複数の生徒または複数のデータフィールドを表示しますか?いくつかのヘルプはこちらをご覧ください。

Sqlite how to get string items using cursor

代わりのgetStringで、列番号は変更することができますのでgetColumnIndex()を使用します。またあなたのデータベースを表示できますか?

0
 store data in arraylist and pass this list to adapter. 
    Make class Student 

public class Student { 
String ID,name,grade,school,program,course,email,bday,address,gender; 
(getter and setter) 
} 

     EX : 
     ArrayList<Student> studentDetailsList= new ArrayList<>(); 


     Cursor c = db.rawQuery("SELECT * FROM student", null); 

        if (c.moveToFirst()){ 
         Student s = new Student(); 
         s.setID(c.getString(0)); 
        s.setName(c.getString(1)); 
        s.setGrade(c.getString(2)); 
        s.setSchool(c.getString(3)); 
        s.setProgram(c.getString(4)); 
        s.setCourse(c.getString(5)); 
        s.setEmail(c.getString(6)); 
        s.setBday(c.getString(7)); 
        s.setAddress(c.getString(8)); 
        s.setGender(c.getString(9)); 
        } 
     studentDetailsList.add(s); 


    Now pass this list to adapter and then set data to views 


In adapter: 

Student ss = studentDetailsList.get(position); 
//now set data to your textviews 
//ex. youridtextview.setText(ss.getID); 
関連する問題