2016-05-11 3 views
0

私は2つのテーブルを持っています。 tblsubjectとtbltopicsです。私は、すべての科目をすべての科目でリストアップしたいと思います。私は私のリストビュー移入これはListViewには、Android Studioのデータベースに2つのテーブルがあります

getSubject()

public Cursor getAllSubject() { 
    String where = null; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

countTopics()

public int countTopics(long subjectid) { 
    String where = KEY_TOPICSUBJECTID + " = " + subjectid; 
    Cursor c = db.query(true, DATABASE_TABLE2, ALL_KEYS2, 
      where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c.getCount(); 
} 

viewList()

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public void viewList() { 
    Cursor cursor = myDb.getAllSubject(); 
    String[] fromFieldNames = new String[]{DBAdapter.KEY_SUBJECTID, DBAdapter.KEY_SUBJECT, DBAdapter.KEY_DATESUBJECT}; 
    int[] toViewIds = new int[]{R.id.txtViewSubjectId, R.id.txtViewSubject, R.id.txtSubjectDateCreated}; 
    SimpleCursorAdapter myCursorAdapter; 
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.activity_view_the_list, cursor, fromFieldNames, toViewIds, 0); 
    ListView myList = (ListView) findViewById(R.id.listView); 
    myList.setAdapter(myCursorAdapter); 
} 

私の問題は、どのようにトピックごとに件数を追加するかです。私は科目を表示することができます。ありがとうございました。

+0

前処理情報 –

+0

なぜあなたはリストビューのカーソルアダプタを作成しませんか?カーソルを取得して、必要なデータに必要なクエリを生成します – xanexpt

答えて

0

データベースからトピック数を返すために、件名のSQL文をサブクエリで更新することができます。 https://robots.thoughtbot.com/back-to-basics-sql#sub-queries

独自のカスタムAsyncTaskとArrayAdapterを使用することをお勧めします。

http://developer.android.com/guide/topics/ui/layout/listview.html

これはあなたのUIの応答性にすると、データ・アクセスおよびレンダリングをカスタマイズできるようになります。

カスタムアダプターを使用すると、結果を制御し追加の取り出しを行うgetView関数を乗り越えることができます。たとえば、画像をリストビュー表示に遅延ロードします。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.subjectitem, null); 
    } 

    if (position % 2 == 1) {    
     v.setBackgroundColor(Color.rgb(234, 234, 234)); 
    } else { 
     v.setBackgroundColor(Color.WHITE); 
    }    

    YOURDATAOBJECT p = itemList.get(position); 
    TextView name = (TextView) v.findViewById(R.id.name); 
    TextView topcount = (TextView) v.findViewById(R.id.topcount); 

    //You can run our additional data fetches here and update the UI 
0

UNIONを使用してダイレクトraw SQLをテーブルから実行できます。このようなもの:

String select = "Select * FROM table1 UNION Select * FROM table2"; 

Cursor c = return db.rawQuery(select, new String[]{}); 
関連する問題