从数据库SQLite的,当滚动底部列表视图拉MOREDATA [英] Pull Moredata from database SQLite, When scroll bottom in listview

查看:162
本文介绍了从数据库SQLite的,当滚动底部列表视图拉MOREDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何把更多的数据,如果列表视图中滚动钮。
这是我的code:

how to pull more data if listview in scroll botton. This is my code:

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功能查询数据库

Function query to database in file 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()]);
}

如果我在Log.d logcat中看到(MyLog,算:+ TOTALITEMS);
我得到的数:10 ,因为我把它设置为10时,首场演出的ListView

If i see in logcat with Log.d("MyLog", "count: "+totalItems); i get count: 10, because i set it to 10 when first show listview.

我的问题,如何加载列表视图中的多个数据行,当我在底部滚动,因为在数据库中,我有,直到+30行数据。我看到网上的教程,但并不成功。也许在这里,解决我的问题。

My question, how to load more data row in listview when i scroll in bottom, because in database i have until +30 row data. I have see tutorials in internet, but not success. Maybe in here, solved my problem.

感谢。

推荐答案

您需要打开你的数据库,然后在onScroll()如果达到终止查询数据库获取下一个10个项目,并追加到适配器和调用notifyDataSetChanged ()上的适配器。所以,你的应用程序中打开你会拉的第一次,让我们说10行,然后滚动你会查询数据库和拉更多下10行再经过。请检查下面的例子code。

You have to open your database and then in the onScroll() if you reach to end query the Database to get next 10 items and append to adapter and call notifyDataSetChanged() on adapter . So the first time your app opens you will pull, let's say 10 rows, and then after scroll you will query the database and pull more the next 10 rows. please check the below example code.

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) {

}

  }

我认为这会帮助你。

i think this will help you.

这篇关于从数据库SQLite的,当滚动底部列表视图拉MOREDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆