我花了一整天的时间试图解决这个问题,这让我发疯了,希望这里的专家能帮助我解决这个问题,这样我就不再拔头发了。我正在尝试在单击按钮时弹出的对话框窗口中填充 ListView。如果我在活动开始时启动 asynctask 代码,一切正常,但如果我尝试将代码移动到按钮 OnClickListener 的任一位置,它会开始双重填充列表视图。
这是代码:
在我的 OnCreate 中:
new LoadAllProducts().execute();
运行以下代码:
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
* */
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_get_classes, "GET",
params);
// Check your log cat for JSON response
Log.d("All Classes: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// classes found
// Getting Array of Classes
classes = json.getJSONArray(TAG_CLASSES);
// looping through All Classes
for (int i = 0; i < classes.length(); i++) {
JSONObject c = classes.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_CLASSID);
String name = c.getString(TAG_CLASSNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CLASSID, id);
map.put(TAG_CLASSNAME, name);
// adding HashList to ArrayList
classesList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
在我的按钮的 OnClickListener 中,我有以下内容:
onCreateDialog().show();
启动以下对话框:
protected Dialog onCreateDialog() {
//Set up dialog
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.checkindialog);
Button canceldialog = (Button) dialog.findViewById(R.id.btncancel);
//Define ListView and set it's empty view in case no results come back
this.lv = (ListView) dialog.findViewById(R.id.lvCheckin);
this.lv.setEmptyView(dialog.findViewById(R.id.empty));
TextView header = (TextView) dialog.findViewById(R.id.lblCheckin);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/roboto-light.ttf");
header.setTypeface(tf);
//ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
// R.layout.checkin_item, R.id.list_content, checkinOptions);
ListAdapter adapter = new SimpleAdapter(Checkin.this, classesList,
R.layout.checkin_item, new String[] { TAG_CLASSID,
TAG_CLASSNAME }, new int[] { R.id.pid, R.id.name });
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listAdapter, View arg1,
int position, long arg3) {
CheckIn checkin = new CheckIn();
TextView txt = (TextView) listAdapter.getChildAt(
position - lv.getFirstVisiblePosition()).findViewById(
R.id.name);
String CheckinMessage = Utilities.getFacebookCheckinMessage(txt
.getText().toString(), context);
checkin.CheckInToFacebook(CheckinMessage, activity);
}
});
canceldialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
return dialog;
}
所以就像我说的,这很好用,除了列表视图只在活动启动时获取它的数据,我试图在单击按钮时让它发生。我已经尝试了两件事。移动“onCreateDialog().show();” 到 AsncTask 的“onPostExecute”,但这不起作用,我尝试移动“new LoadAllProducts().execute();” 进入 ondialog 创建事件,以及内联按钮点击监听器。这两者最终只会不断地添加到列表视图中,而不是每次都清除/刷新列表视图。
真的希望有人能提供帮助,因为我敢肯定这可能是愚蠢的,但这今天一直在踢我的屁股。
提前致谢!