gpt4 book ai didi

android - 动态 ListView 在滚动结束时添加 "Load more items"

转载 作者:IT王子 更新时间:2023-10-28 23:45:37 24 4
gpt4 key购买 nike

我有一个 ListView ,它通过 Json 从 sqlite 数据库中获取数据。

我想把它变成动态 ListView ,在滚动结束时,“加载更多项目”出现在列表的页脚,同时加载更多项目并将它们添加到适配器(例如每次 10 个项目)。我在实现此功能时遇到问题。请帮帮我。谢谢。

public class AllProductsActivity extends Activity {

... defining variables...;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

new LoadAllProducts().execute();

}

class LoadAllProducts extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
... progress dialog...
}

protected String doInBackground(String... args) {
countryList = new ArrayList<Country>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_DESCRIPTION);
String due_date = c.getString(TAG_DUE_DATE);
String staff = c.getString(TAG_STAFF);
String status = c.getString(TAG_STATUS);

country = new Country(id,name,description,
due_date, staff, status);
countryList.add(country);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {

displayListView();

}
});
}
}

private void displayListView() {
dataAdapter = new MyCustomAdapter(this,
R.layout.country_info, countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
}

private class MyCustomAdapter extends ArrayAdapter<Country> {

... blah blah blah ...

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
... blah blah blah ...
}

return convertView;
}
}
}

最佳答案

你试试下面的代码

list.setOnScrollListener(new OnScrollListener() {

public void onScrollStateChanged(AbsListView view, int scrollState) {


}

public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {

if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
additems();
}
}
}
});

additem() 中将接下来的 10 项添加到数组列表中,并将 flag_loading 设置为 false。并调用 notifydatasetchanged。它应该工作。

关于android - 动态 ListView 在滚动结束时添加 "Load more items",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com