首先,我從服務器獲得數據,並且我編寫了ListView,現在我不想在列表視圖滾動到底部時加載更多內容。我的異步類有名稱取所以我認爲我需要調用Fetch.execute();? 另外我需要使用我第一次調用的所有Fetch對象屬性值,我如何在onScroll ListView(Android)中啓動AsyncTask方法
感謝所有!
我的活動在這裏代碼
public class InboxActivity extends Activity implements View.OnClickListener {
......................
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
........
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
...........
this.fetch = new FetchTask();
this.fetch.Selectedmonth = this.Smonth;
this.fetch.Selectedyear = this.Syear;
this.fetch.page = 0;
this.fetch.sess_id = user_id;
ListView ll = (ListView)findViewById(R.id.mailList);
this.fetch.ll = ll;
this.fetch.execute();
ll.setOnScrollListener(new OnScrollListener(){
public void onScrollStateChanged(ListView view, int scrollState)
{
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount==totalItemCount)
{
HERE I NEED TO LAUNCH FETCH object
}
}
}
};
public class FetchTask extends AsyncTask<Void, Void, JSONArray> {
HERE MY PRORPITIES
@Override
protected JSONArray doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("my url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("qw", "er"));
nameValuePairs.add(new BasicNameValuePair("debug", "1"));
nameValuePairs.add(new BasicNameValuePair("t", "0"));
nameValuePairs.add(new BasicNameValuePair("m", Integer.toString(this.Selectedmonth)));
nameValuePairs.add(new BasicNameValuePair("y", Integer.toString(this.Selectedyear)));
nameValuePairs.add(new BasicNameValuePair("st", Integer.toString(this.page)));
nameValuePairs.add(new BasicNameValuePair("sess_id", this.sess_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine());
String line = "0";
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
reader.close();
String result11 = sb.toString();
System.out.println(result11);
this.result_str = result11;
// parsing data
JSONArray arr = new JSONArray(result11);
return new JSONArray(result11);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPreExecute() {
this.pd = new ProgressDialog(InboxActivity.this);
this.pd.setMessage("loading");
this.pd.show();
}
@Override
protected void onPostExecute(JSONArray result)
{
if (result != null)
{
List<String> subjects = new ArrayList<String>();
List<String> emails = new ArrayList<String>();
for(int i = 0; i < result.length(); i++)
{
try
{
JSONObject json_data = result.getJSONObject(i);
emails.add(json_data.getString("mittente"));
subjects.add(json_data.getString("oggetto"));
}
catch (JSONException e)
{
e.printStackTrace();
}
}
if(this.page == 0)
{
this.adapter = new ArrayAdapter<String>(
InboxActivity.this,
R.layout.da_item,
emails
);
this.ll.setAdapter(this.adapter);
}
else
{
for(int i = 0; i < result.length(); i++)
{
JSONObject json_data;
try
{
json_data = result.getJSONObject(i);
this.adapter.add(json_data.getString("mittente"));
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
}
else
{
System.out.println("Messages not found");
}
this.pd.dismiss();
}
}
}
嘗試限制您發佈的代碼僅限於相關區域! –
i'v deone它,請檢查它 – user3323180