嗨,我是非常新的Android,在我的情况下,我有200条以上记录,我必须在我的列表视图中显示,这就是为什么我要求服务器偏移值
这样基于偏移值的服务器发送记录为此我写下面的代码但重复的记录也在我的ArrayList中添加我们如何解决请帮助
我的代码非常清楚,我发送服务器链接也请帮帮我
public class MainActivity extends Activity {
private ListView lv;
private ProgressDialog pDialog;
private ListViewAdapter adapter;
private ArrayList<String> arrayList;
private int scrollState;
private int offset = 0;
private boolean flag = false;
private boolean loadingMore = false;
ProgressBar pb;
private int scrollpos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list);
if (savedInstanceState == null) {
arrayList = new ArrayList<>();
} else {
arrayList = (ArrayList<String>)savedInstanceState.getSerializable("list");
}
//Adding footer for ListView:-
pb = new ProgressBar(this);
lv.addFooterView(pb);
//Setting Adapter:-
adapter = new ListViewAdapter(this, arrayList);
lv.setAdapter(adapter);
if (!flag) {
new loadMoreListView().execute();
flag = true;
}
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
MainActivity.this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
scrollpos = totalItemCount;
int lastPos = firstVisibleItem + visibleItemCount;
if (lastPos == totalItemCount && !loadingMore) {
if (totalItemCount < 250) {
new loadMoreListView().execute();
} else {
lv.removeFooterView(pb);
}
}
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("list", arrayList);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
arrayList = (ArrayList<String>) savedInstanceState.getSerializable("list");
}
private class loadMoreListView extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... unused) {
loadingMore = true;
String result = null;
String url = "http://api.androidhive.info/json/imdb_top_250.php?offset=" + offset;
try {
URL mUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) mUrl.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
buffer.append(str);
}
result = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result) {
System.out.println("Result is"+result);
if (result != null) {
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
arrayList.add(array.getJSONObject(i).getString("title"));
}
System.out.println("arry list count is====>"+arrayList.size());
adapter.notifyDataSetChanged();
offset++;
// offset = offset + 10;
loadingMore = false;
} catch (Exception e) {
e.printStackTrace();
}
} else {
lv.removeFooterView(pb);
}
}
}
}
答案 0 :(得分:0)
不知道你最初获得了多少项目,我认为它是250,而你的偏移应该是你的listize + 1
//after retrieving data offset should be
offset = arraylist.size() + 1;
答案 1 :(得分:0)
移动偏移量++;内部循环因此它将增加到当前记录编号,您将获得下一个记录
目前你只是将它增加到一次,这就是为什么它会从下一个给出记录,即除了第一个以外的重复记录。
for (int i = 0; i < array.length(); i++) {
arrayList.add(array.getJSONObject(i).getString("title"));
offset++;
}