我想在Load-More
中实施ListView
功能。
这是我的代码:
public class MainActivity extends Activity {
int url_page = 0;
private static String URL = "http://MyURL&page_no=";
ArrayList<ItemInfo> products = new ArrayList<ItemInfo>();
BoxAdapter boxAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTaskParseJson().execute();
}
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONFromUrl(URL+"0");
dataJsonArr = json.getJSONArray("data");
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
String nickname = c.getString("nickname");
String description = c.getString("description");
products.add(new ItemInfo(nickname, description));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg)
{
SetAdapter();
}
}
public void SetAdapter()
{
boxAdapter = new BoxAdapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.list);
lvMain.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
ListView lvMain = (ListView) findViewById(R.id.list);
int pos = lvMain.getPositionForView(view);
ItemInfo p = boxAdapter.getItem(pos);
//
String username = p.username;
String description = p.description;
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
OnClickActivity.class);
in.putExtra("username", username);
in.putExtra("description", description);
startActivity(in);
}
});
lvMain.setAdapter(boxAdapter);
lvMain.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
ListView lvMain = (ListView) findViewById(R.id.list);
if(firstVisibleItem + visibleItemCount >= totalItemCount) {
boxAdapter.loadAdditionalItems();
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我将此添加到onScroll事件:
if (lvMain.getLastVisiblePosition() >= lvMain.getCount())
{
url_page = url_page + 1;
new LoadLastestPost().execute();
}
as asaskask:
public class LoadLastestPost extends AsyncTask<String, String, String> {
final String TAG = "LoadLastestPost.java";
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
JsonParser jParser = new JsonParser();
String newURL = URL + String.valueOf(url_page);
JSONObject json = jParser.getJSONFromUrl(newURL);
dataJsonArr = json.getJSONArray("data");
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
String nickname = c.getString("nickname");
String description = c.getString("description");
products.add(new ItemInfo(nickname, description));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg)
{
LoadMoreItem();
}
}
private void LoadMoreItem()
{
ListView lvMain = (ListView) findViewById(R.id.list);
boxAdapter = null;
boxAdapter = new BoxAdapter(this, products);
lvMain.setAdapter(boxAdapter);
boxAdapter.notifyDataSetChanged();
}
但它不起作用;(那需要修复? P.S我显示10项,并加载已经加载的10个响应。 我想在listview的末尾加载项目。