【发布时间】:2015-09-27 14:58:45
【问题描述】:
我正在从服务器获取数据,但不知道获取的记录数。我将数据保存在 sqlite 中并使用光标显示它。我想最初在列表视图中显示前 10 条记录。在下一个按钮上单击下一个 10 条记录应显示在同一个列表视图中,依此类推。请给我一个方法。
代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
//stuff that updates ui
/**
* Updating parsed JSON data into ListView
* */
disp();
}
});
public void disp() {
try {
Cursor c;
String sql = "select * from CurrentAffairs LIMIT 10";
c = sdb.rawQuery(sql, null);
int rowCount=c.getCount();
Constants.i=rowCount;
if (c != null) {
// if (c.moveToNext()) {
while (c.moveToNext()) {
String message = c.getString(c.getColumnIndex("message"));
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
map.put(TAG_MESSAGE, message);
result.add(message);
}// while(c.moveToNext());
}
} catch (Exception e) {
// TODO: handle exception
Log.i("In Banking economics", "Error-" + e.toString());
e.printStackTrace();
}
//ListAdapter adapter = new SimpleAdapter(Banking_Economics.this, result,R.layout.list_item, new String[] { TAG_MESSAGE },new int[] { R.id.textView1 });
ListAdapter adapter=new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1, result);
setListAdapter(adapter);
// selecting single ListView item
lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String message = ((TextView) view.findViewById(R.id.textView1))
.getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), News.class);
in.putExtra(TAG_MESSAGE, message);
startActivity(in);
}
});
}
我尝试为每 10 条记录使用单独的数组列表,但由于不知道记录的数量,它不起作用。 另外我不了解分页,所以不能使用它。 任何帮助表示赞赏
注意:来自服务器的数据每天都会更新并添加新记录。我想在第一次加载列表视图时显示这些新记录。
【问题讨论】: