我花了一整天的时间试图解决这个问题,这让我疯了,希望周围的专家可以帮助我解决这个问题,所以我不再把头发拉出来了。我正在尝试在单击按钮时弹出的对话框窗口中填充ListView。如果我把asynctask代码放在活动开始时启动,一切正常,但是如果我尝试将代码移动到OnClickListener按钮的任何位置,它会开始双重填充listview。
以下是代码:
在我的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;
}
就像我说的,这很好用,除了listview只在活动启动时获取它的数据,而我正试图在点击按钮时发生这种情况。我尝试了两件事。移动“onCreateDialog()。show();”到AsncTask的“onPostExecute”,但这不能正常工作,我试过移动“new LoadAllProducts()。execute();”进入ondialog创建事件,以及按钮单击监听器内联。这两者最终都会不断添加到列表视图中,而不是每次都清除/刷新列表视图。
真的希望有人可以提供帮助,因为我确信这可能是愚蠢的事情,但今天这一直在踢我的屁股。
提前致谢!
答案 0 :(得分:0)
Evan尝试覆盖受保护的Dialog onCreateDialog(int id)
函数以显示对话框,并覆盖public void onPrepareDialog(int id, Dialog dialog)
。在onPrepareDialog函数调用removeDialog(int id)
函数中,这将有助于每次创建一个新的对话框并删除以前缓存的数据。