0

我有一个从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("");
            }
        });
    }
}

但我的名单没有得到刷新..

你对此有什么想法吗?谢谢你。

4

2 回答 2

0

getItemId() 应该为列表中的每个条目返回唯一值。尝试仅返回位置。

于 2013-09-22T03:03:24.993 回答
0

我认为主要原因是缺少调用notifyDataSetChanged()方法

实际上,您的“活动代码”并不完全是活动,而是获取数据的 asnyctask。使用 adaper 的一种更常见的方法是在您的活动中使用 Adaper 以及 ListView。在你的fetching-data-method(asynctask or loader)中,调用adaper的change底层数据接口来改变数据,然后调用adaper的notifyDataSetChanged()方法。

有点伪代码可能看起来像:

适配器:

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();
}
于 2013-09-22T03:33:53.340 回答