简体   繁体   English

在Android Studio中用数据库中的两个表填充ListView

[英]Populate ListView with two tables in database in Android Studio

I have two tables. 我有两张桌子。 tblsubject and tbltopics. tblsubject和tbltopics。 I want to list all the subjects with the number of topics every subject. 我想列出所有主题,并列出每个主题的主题数。

getSubject() 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() 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() this is where i populate my listview 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);
}

My problem is how to add the number of topics every subject. 我的问题是如何增加每个主题的主题数。 I can display the Subjects. 我可以显示主题。 Thank you. 谢谢。

You can update your subject SQL statement with a subquery to return the topic count from the database. 您可以使用子查询更新主题SQL语句,以从数据库返回主题计数。 See https://thoughtbot.com/blog/back-to-basics-sql#sub-queries 参见https://thoughtbot.com/blog/back-to-basics-sql#sub-queries

On a separate note, I would recommend using your own custom AsyncTask and ArrayAdapter. 另外,我建议您使用自己的自定义AsyncTask和ArrayAdapter。

https://developer.android.com/guide/topics/ui/layout/recyclerview https://developer.android.com/guide/topics/ui/layout/recyclerview

This will make your UI more responsive and allow you customize the data access and rendering. 这将使您的UI更具响应性,并允许您自定义数据访问和呈现。

By using a custom adapter you can over ride the getView function to control the result and do additional fetches. 通过使用自定义适配器,您可以超越getView函数来控制结果并进行其他提取。 For example, lazy loading images into the listview display. 例如,将图像延迟加载到列表视图显示中。

@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

You can run a direct raw sql with an UNION from your tables. 您可以从表中使用UNION运行直接的原始sql。 Something like this: 像这样:

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

Cursor c = return db.rawQuery(select, new String[]{});

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

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