如何使用ListAdapter使用adapter.notifyDatasetChanged。请帮助我,我被困在那个代码中。我想要的是在编辑产品数量后更新列表视图。感谢那些愿意帮助的人。
这是我的代码
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class vieworder extends ListActivity {
private static final String Edit_url ="http://192.168.56.1/editorder.php";
private static final String Success="success";
private static final String Message ="message";
private ProgressDialog pDialog;
private static final String Vieworderurl = "http://192.168.56.1/vieworder.php";
public static final JSONParser jParser = new JSONParser();
private static final String TAG_POSTS = "message";
private static final String TAG_ID = "Id";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Price";
private JSONArray order = null;
private ArrayList<HashMap<String, String>> orderlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vieworder);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
new LoadComments().execute();
}
public void updateJSONdata() {
orderlist = new ArrayList<HashMap<String, String>>();
JSONObject json = jParser.getJSONFromUrl(Vieworderurl);
try {
order = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < order.length(); i++) {
JSONObject c = order.getJSONObject(i);
String id = c.getString(TAG_ID);
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID,id);
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
orderlist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, orderlist,
R.layout.single_post, new String[] { TAG_ID, TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{R.id.ID, R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final EditText input = new EditText(vieworder.this);
final class EditOrder extends AsyncTask<String, String, String> {
boolean failure = false;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(vieworder.this);
pDialog.setMessage("Editing Item...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
final String Quantity = input.getText().toString();
final String prodid =((TextView) view.findViewById(R.id.ID)).getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("prodid", prodid));
params.add(new BasicNameValuePair("Quantity", Quantity));
JSONObject json = jParser.makeHttpRequest(
Edit_url, "POST", params);
success = json.getInt(Success);
if (success == 1) {
Intent i = new Intent(vieworder.this,vieworder.class);
startActivity(i);
return json.getString(Message);
}else{
return json.getString(Message);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (file_url != null){
Toast.makeText(vieworder.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
AlertDialog.Builder alert = new AlertDialog.Builder(vieworder.this);
alert.setTitle("Choose Option");
//final EditText input = new EditText(vieworder.this);
// alert.setView(input);
//input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AlertDialog.Builder alert = new AlertDialog.Builder(vieworder.this);
alert.setTitle("Edit");
final String Quantity = ((TextView) view.findViewById(R.id.Quantity)).getText().toString();
final String ID = ((TextView) view.findViewById(R.id.ID)).getText().toString();
alert.setView(input);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setText(Quantity);
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if ((input.getText().length() > 6) || (Integer.parseInt(input.getText().toString()) <= 0)) {
Toast.makeText(vieworder.this, "Invalid Quantity", Toast.LENGTH_LONG).show();
}else{
new EditOrder().execute();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
});
alert.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
});
}
final public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(vieworder.this);
pDialog.setMessage("Loading Order...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
//we will develop this method in version 2
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
//we will develop this method in version 2
updateList();
}
}
}
答案 0 :(得分:0)
每次调用update()
时,您都会创建一个新的适配器。在完成所有private static final
声明之后,将适配器添加到实例变量中,放入
ArrayAdapter adapter;
所以它看起来像
....
import android.widget.Toast;
private ArrayAdapter adapter;
public class vieworder extends ListActivity {
....
然后将下面的所有代码移到onCreate()
方法(来自update()
),而不是制作一个新的适配器,只需将它配置到现在在您的类中随处可见的适配器:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vieworder);
adapter = new ArrayAdapter(this, orderlist,
R.layout.single_post, new String[] { TAG_ID, TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{R.id.ID, R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });
setListAdapter(adapter);
}
然后当你需要更新时,只需写下
adapter.notifyDataSetChanged();
最初您使用了ListAdapter
,它没有方法notifyDataSetChanged
。考虑到您的数据,我建议您改用ArrayAdapter
。 Here is the API
答案 1 :(得分:0)
这里的一个问题是你不断更换适配器。您只需要设置适配器一次。这是一段代码,可以帮助您找到正确的方向。
Rows[table.Rows.Count - 1]