如何使用Sqlite& amp;在可扩展列表视图中添加分页的AsyncTask

时间:2015-04-25 12:39:11

标签: android sqlite android-asynctask expandablelistview

有一个Sqlite数据库,我在Expandable列表视图中遇到了分页实现的问题。

我想获取数据(一次10条记录)并在可解释列表视图中显示这些记录。

如何通过AsyncTask实现此目标,与服务器进行后台交互以显示数据库中的信息?

主要活动

/** The count. */
static int count=0;

listDataHeader = new ArrayList<String>();
childDataHashMap = new HashMap<String, List<ChildInfo>>();
databaseHelper = new DatabaseHelper(this);
// readFromFile();
csvToSqlite();

// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
txtViewLoadMore=(TextView) findViewById(R.id.textViewLoadMore);
// LoadMore button
        Button btnLoadMore = new Button(this);
        btnLoadMore.setText("Load More");
        // Adding Load More button to lisview at bottom
        expListView.addFooterView(btnLoadMore)
txtViewLoadMore.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        count = count+1;
        //call AsyncTask for loading data in Expandable list view in background
        new NearBuyAsyncTask(MainActivity.this).execute();  
    }
});     
getExpandableListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,childDataHashMap);
// setting list adapter
expListView.setAdapter(listAdapter);

}

public void getExpandableListData() {
    // Group data//
    // databaseHelper = new DatabaseHelper(getApplicationContext());{
    Cursor cursor;
    if(count==0)
    {
        cursor = databaseHelper.getGroupData();
    }
    else{
         cursor = databaseHelper.getLoadMoreData(count);
    }
    cursor.moveToFirst();
    Log.i(TAG, "cursor.getCount()" + cursor.getCount());
    do {
        String categoryDescription = cursor.getString(cursor.getColumnIndex("categorydesc"));
        int categoryId = cursor.getInt(cursor.getColumnIndex("CategoryId"));

        // Child data//
        Cursor cursorChild = databaseHelper.getChildData(categoryId);
        List<ChildInfo> childList = new ArrayList<ChildInfo>();
        cursorChild.moveToFirst();
        while (cursorChild.moveToNext()) {
            String businessName = cursorChild.getString(cursorChild .getColumnIndex("BusinessName"));
            phoneNumber = cursorChild.getString(cursorChild.getColumnIndex("ph_Phone"));
            String landMark = cursorChild.getString(cursorChild .getColumnIndex("LandMark"));
            ChildInfo childInfo = new ChildInfo(businessName, phoneNumber,  landMark);
            childList.add(childInfo);
        }
        childDataHashMap.put(categoryDescription, childList);
        } while (cursor.moveToNext());
    cursor.close();
}

NearBuyAsyncTask

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    activity = new MainActivity();
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            MainActivity mainActivity = new MainActivity();
            mainActivity.getExpandableListData();
        }
    });

    return null;
}

我收到以下错误

  

引起:java.lang.RuntimeException:无法在里面创建处理程序   没有调用Looper.prepare()

的线程

0 个答案:

没有答案