我有一个自定义适配器,它扩展自 BaseAdapter ..
自定义适配器代码:
public class SearchListViewAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private JsonArray searchResults;
public SearchListViewAdapter(Context context, JsonArray searchResults) {
this.searchResults = searchResults;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchResults.count();
}
@Override
public Object getItem(int position) {
return searchResults.get(position);
}
/*public ListAdapter updateResults(JsonArray results) {
searchResults = results;
notifyDataSetChanged();
return null;
}*/
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
JsonObject searchResult = (JsonObject)getItem (position);
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_search_result, null);
holder = new ViewHolder();
holder.txtFullName = (TextView) convertView.findViewById(R.id.FullName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtFullName.setText(searchResult.getString ("FirstName") + searchResult.getString ("LastName"));
return convertView;
}
static class ViewHolder {
TextView txtFullName;
}
}
活动代码:
// After displaying the list in an onPostExecute Method of an AsyncTask class
// I call another async task : BarcodeAction by giving the param : records
new BarcodeAction(records).execute("");
private class BarcodeAction extends AsyncTask<String, Void, JsonArray> {
private JsonArray records;
public BarcodeAction(JsonArray result)
{
this.records = result;
}
@Override
protected JsonArray doInBackground(String... params) {
// Processing... if it's success the onPostExecute method receive : records
if (resultType.equals("success"))
return records;
return null;
}
@Override
protected void onPostExecute(final JsonArray records) {
final ListView lv1 = (ListView) findViewById(R.id.ListViewSearchResults);
// EDIT : notifyDataSetChange doesn't work
SearchListViewAdapter svla1 = new SearchListViewAdapter(SearchActivity.this, records);
lv1.setAdapter(svla1);
svla1.notifyDataSetChanged();
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
JsonObject response = (JsonObject)o;
SearchActivity.VISITOR_BARCODE = response.getString("Barcode");
new BarcodeAction(records).execute("");
}
});
}
}
但我的名单没有得到更新......
你对此有任何想法吗?揍你。
答案 0 :(得分:0)
getItemId()应该为列表中的每个条目返回唯一值。尝试仅返回位置。
答案 1 :(得分:0)
我认为主要原因是缺少调用notifyDataSetChanged()
方法。
实际上,您的“活动代码”并不完全是活动,而是获取数据的asnyctask。使用adaper的一种更常见的方法是在您的活动中使用Adaper和ListView。在您的获取数据方法(asynctask或loader)中,调用adaper的更改底层数据接口来更改数据,并调用adaper的notifyDataSetChanged()
方法。
有点psudeo代码可能如下:
Adaper:
public class SearchListViewAdapter extends BaseAdapter {
private JsonArray searchResults;
......
public setDataSet(JsonArray newData) {
searchREsults = newData;
}
......
}
的活动:
public class MyActivity extends Activity {
ListView mResultListView;
SearchListViewAdaper mResultViewAdaper;
@override
OnCreate(...) {
......
//init mResultListView
mResultListView = (ListView) findViewById(R.id.xxxx);
mResultViewAdaper = new SearchListViewAdapter();
mResultListView.setAdapter(mResultViewAdaper);
......
}
......
}
的AsyncTask: public fetchDataTask扩展AsyncTask { ......
onPostExecute(JsonArray records) {
mResultViewAdaper.setDataSet(records);
// IMPORTANT: notify data change
mResultViewAdaper.notifyDataSetChanged();
}