从我关于how can I use HashMap for BaseAdapter的帖子继续,我在这里有另一个问题。如何在arraylist hashmap上插入或替换值?
我尝试使用mylist.set(position, map);
,但它无效
我的完整代码:
此代码用于从数据库获取数据并将其放在mylist
上class LoadFarmasi extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LihatFarmasiObat.this);
pDialog.setMessage(Html.fromHtml("Ambil Data Farmasi..."));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Places XML
* */
protected String doInBackground(String... args) {
String xml;
try {
// ----------------------------Make data Parameter for query-----------------
List<BasicNameValuePair> postsku = new ArrayList<BasicNameValuePair>(0);
postsku.add(new BasicNameValuePair("noregis", noregis));
parser = new XMLParser();
xml = parser.getXmlFromUrlWithPost(URL_FARMASI,postsku); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
nodes = doc.getElementsByTagName("result");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* and show the data in UI
* Always use runOnUiThread(new Runnable()) to update UI from background
* thread, otherwise you will get error
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all hospitals
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed Places into LISTVIEW
* */
int leng = nodes.getLength();
for (int i = 0; i < leng; i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodes.item(i);
map.put("nama", parser.getValue(e, "nama"));
map.put("in", parser.getValue(e, "in"));
map.put("id", "");
mylist.add(map);
}
}
});
}
}
这是针对edittext的插入值,并将值放入mylist
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
new LoadFarmasi().execute();
}
notifyDataSetChanged();
}
public int getCount() {
return mylist.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
HashMap<String,String> map =mylist.get(position);
// position gives you the index
String value = map.get("nama");
String value2 = map.get("in");
String value3 = map.get("id");
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_farmasi_obat, null);
holder.caption = (EditText) convertView.findViewById(R.id.editText1);
holder.txtNama = (TextView) convertView.findViewById(R.id.txtListFarmasiNama);
holder.txtIn = (TextView) convertView.findViewById(R.id.txtListFarmasiIn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.txtNama.setText(value);
holder.txtIn.setText(value2);
holder.caption.setText(value3);
holder.txtNama.setId(position);
holder.txtIn.setId(position);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", Caption.getText().toString());
mylist.set(position, map);
}
}
});
return convertView;
}
}
class ViewHolder {
EditText caption;
TextView txtNama;
TextView txtIn;
}
答案 0 :(得分:0)
// try this way here i also know what are changes i have done in your code.
please replace your getView() and let me know still have any problem
First of all i have add final keyword to position and hash so it can be access in onFocusChange()
Second one is we direct access position rather getting from view getTag Id.
Third one is i have replace id value in hashmap and notify adapter so update data reflected in list.
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final HashMap<String,String> map =mylist.get(position);
// position gives you the index
String value = map.get("nama");
String value2 = map.get("in");
String value3 = map.get("id");
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_farmasi_obat, null);
holder.caption = (EditText) convertView.findViewById(R.id.editText1);
holder.txtNama = (TextView) convertView.findViewById(R.id.txtListFarmasiNama);
holder.txtIn = (TextView) convertView.findViewById(R.id.txtListFarmasiIn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.txtNama.setText(value);
holder.txtIn.setText(value2);
holder.caption.setText(value3);
holder.txtNama.setId(position);
holder.txtIn.setId(position);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
EditText Caption = (EditText) v;
map.put("id", Caption.getText().toString());
notifyDataSetChanged();
}
}
});
convertView.setTag(holder);
return convertView;
}