如果滚动按钮中的listview,如何提取更多数据。 这是我的代码:
DatabaseHandler database;
ListView mylist;
ArrayList<NewsItem> items;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.category, container, false);
mylist = (ListView) view.findViewById(R.id.categoryNewsItemGrid);
mylist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// retrieve the NewsItem at the index of the click
NewsItem item = (NewsItem) mylist.getAdapter().getItem(position);
// view the item
clickHandler.onItemClick(item.getId());
}
});
mylist.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView list, int state) {
if (state == OnScrollListener.SCROLL_STATE_IDLE) {
// check to see if all the visible items have images
int firstVisible = list.getFirstVisiblePosition();
int lastVisible = list.getLastVisiblePosition();
for (int i = firstVisible; i <= lastVisible; i++) {
NewsItem item = (NewsItem) list.getAdapter().getItem(i);
// if this item doesn't have a thumbnail
if (item.getThumbnailBytes() == null) {
// load the thumbnail
Bundle bundle = new Bundle();
bundle.putInt(ResourceService.KEY_ITEM_ID,
item.getId());
service.sendMessageToService(
ResourceService.MSG_LOAD_THUMB, bundle);
}
}
}
}
public void onScroll(AbsListView list, int firstVisible,
int visibleItems, int totalItems) {
if (visibleItems != 0
&& ((firstVisible + visibleItems) >= (totalItems))) {
Log.d("MyLog", "count: "+totalItems);
}
}
});
displayCategory(getArguments().getString("category"));
return view;
}
public void displayCategory(String categoryTitle) {
items = new ArrayList<NewsItem>(Arrays.asList(database.getItems(
categoryTitle, 10)));
mylist.setAdapter(new ItemAdapter(getActivity(), R.layout.list_news_item,
items, 70, 70));
}
对文件DatabaseHandler.java中的数据库进行函数查询
public NewsItem[] getItems(String category, int limit) {
// ask the content provider for the items
Uri uri = Uri.withAppendedPath(
DatabaseProvider.CONTENT_URI_ITEMS_BY_CATEGORY, category);
String[] projection = new String[] { DatabaseHelper.COLUMN_ITEM_ID,
DatabaseHelper.COLUMN_ITEM_TITLE,
DatabaseHelper.COLUMN_ITEM_DESCRIPTION,
DatabaseHelper.COLUMN_ITEM_URL,
DatabaseHelper.COLUMN_ITEM_THUMBNAIL };
String sortOrder = DatabaseHelper.RELATIONSHIP_TABLE + "."
+ DatabaseHelper.COLUMN_RELATIONSHIP_PRIORITY + " ASC, "
+ DatabaseHelper.ITEM_TABLE + "."
+ DatabaseHelper.COLUMN_ITEM_PUBDATE + " DESC";
Cursor cursor = contentResolver.query(uri, projection, null, null,
sortOrder);
// check the cursor isn't null
if (cursor == null) {
// bail here, returning an empty array
return new NewsItem[0];
}
// load the column names
int id = cursor.getColumnIndex(DatabaseHelper.COLUMN_ITEM_ID);
int title = cursor.getColumnIndex(DatabaseHelper.COLUMN_ITEM_TITLE);
int description = cursor
.getColumnIndex(DatabaseHelper.COLUMN_ITEM_DESCRIPTION);
int url = cursor.getColumnIndex(DatabaseHelper.COLUMN_ITEM_URL);
int thumbnail = cursor
.getColumnIndex(DatabaseHelper.COLUMN_ITEM_THUMBNAIL);
// load the items into an array
ArrayList<NewsItem> items = new ArrayList<NewsItem>();
while (cursor.moveToNext() && cursor.getPosition() < limit) {
NewsItem item = new NewsItem(); // initialize a new item
item.setId(cursor.getInt(id));
item.setTitle(cursor.getString(title));
item.setDescription(cursor.getString(description));
item.setUrl(cursor.getString(url));
item.setThumbnailBytes(cursor.getBlob(thumbnail));
items.add(item); // add this item to the array
}
cursor.close();
return items.toArray(new NewsItem[items.size()]);
}
如果我在logcat中看到Log.d(“MyLog”,“count:”+ totalItems); 我得到数:10 ,因为我在第一次显示列表视图时将其设置为10。
我的问题,当我在底部滚动时如何在listview中加载更多数据行,因为在数据库中我有直到+30行数据。我在互联网上看过教程,但没有成功。也许在这里,解决了我的问题。
感谢。
答案 0 :(得分:1)
你必须打开你的数据库然后在onScroll()中,如果你到达结束查询数据库以获得接下来的10个项目并附加到适配器并在适配器上调用notifyDataSetChanged()。因此,当您的应用程序第一次打开时,您将拉动,比方说10行,然后在滚动之后,您将查询数据库并在接下来的10行中拉出更多。请查看以下示例代码。
public class GrowingListViewActivity extends ListActivity implements OnScrollListener {
Aleph0 adapter = new Aleph0();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(adapter);
getListView().setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisible, int visibleCount,
int totalCount) {
boolean loadMore = /* maybe add a padding */
firstVisible + visibleCount >= totalCount;
if (loadMore) {
adapter.count += visibleCount; // or any other amount
adapter.notifyDataSetChanged();
}
}
public void onScrollStateChanged(AbsListView v, int s) {
}
class Aleph0 extends BaseAdapter {
int count = 40; /* starting amount */
public int getCount() {
return count;
}
public Object getItem(int pos) {
return pos;
}
public long getItemId(int pos) {
return pos;
}
public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(GrowingListViewActivity.this);
view.setText("entry View : " + pos);
return view;
}
}
}
public class CustomArrayAdapter extends ArrayAdapter implements
OnScrollListener {
private final Context context;
private final ArrayList<String> values;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public CustomArrayAdapter(Context arg0, int arg1, int arg2,
ArrayList<String> arg3) {
super(arg0, arg1, arg2, arg3);
this.context = arg0;
this.values = arg3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int product_id = 0;
View rowView = new View(context);
try {
ImageView wic_logo = new ImageView(context);
TextView label = new TextView(context);
String p = values.get(position);
label.setText(p);
wic_logo.setImageResource(R.drawable.ic_launcher);
Log.d("Custom Array Adapter", "at" + position);
} catch (Exception e) {
Log.d("Custom Array Adapter", "catch");
}
return rowView;
}
public void onScroll(AbsListView arg0, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.d("entered onScroll", " " + firstVisibleItem + visibleItemCount
+ totalItemCount);
if (((firstVisibleItem + visibleItemCount) >= totalItemCount - 1)) {
Log.d("entered if", " " + firstVisibleItem + visibleItemCount
+ totalItemCount);
// if we're at the bottom of the listview, load more data
addData(totalItemCount, totalItemCount); // values.get(totalItemCount));
}
}
private void addData(int totalItemCount, int productId) {
Toast.makeText(getContext(), "last item", Toast.LENGTH_SHORT).show();
}
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}
}
我认为这会对你有帮助。
答案 1 :(得分:0)
您必须打开数据库,然后在每个滚动声明的更改中查询数据库并将结果限制为您想要的数字!因此,当您的应用程序第一次打开时,您将拉动,比方说10行,然后在滚动之后,您将查询数据库并在接下来的10行中拉出更多!!!
<强>更新强>
让我们更容易一点!使用您的数据类型创建ArrayList!并且make一个私有的int limitCounter = 0;。之后,从数据库中获取所有数据并将其插入ArrayList。然后滚动时,您将更改limitCounter的值并从中提取相应的数据,这样您就不必每次都查询数据库。
更新#2
尝试以下教程,它将为您提供帮助:http://www.androidhive.info/2012/03/android-listview-with-load-more-button/