使用自定义适配器更新listfragment中的列表

时间:2014-03-28 06:33:55

标签: android android-listview scroll android-listfragment baseadapter

我使用onScroll onclicklistner来更新listFragment中的listview。

public class WishboardFragment extends ListFragment implements OnScrollListener{

   private ProgressBar progressBar2;
   private TextView finished;
   private Context context = null;
   private ListAdapter wishAdapter = null;
   private final String TAG = "wishboard";
   private final String cachedName = "wishboard";
   JSONObject jsonObject = null;
   JSONArray jsonArray = null;
   private Menu optionsMenu;
   private Integer pageStart = 1;
   private Integer pageEnd = 15;
   private Boolean isDownloading = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    this.context = getActivity();
    return  inflater.inflate(R.layout.activity_dashboard,container,false);
}



@Override
public void onCreate(Bundle savedInstanceState) {

    String url = getWishboardUrl();
    sendRequest(url);
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    wishAdapter = new WishRowAdaptor(getActivity(),this.getWishboardCached());
    setListAdapter(wishAdapter);
}


@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
    this.optionsMenu = menu;
    inflater.inflate(R.menu.dashboard, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    getListView().setOnScrollListener(this);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.btn_refresh:
        Common.setRefreshActionButtonState(true,optionsMenu);
        String url = getWishboardUrl();
        sendRequest(url);
    return true;

    }
    return super.onOptionsItemSelected(item);
}




public void sendRequest(String url) {
    new SendRequest().execute(url);
}

private class SendRequest extends AsyncTask<String, Integer, String> {

    protected String doInBackground(String... requestURL) {

        String data = "";
        HttpURLConnection httpUrlConnection = null;

        try {
            URL url = new URL(requestURL[0]);
            URLConnection connection = url.openConnection();
            connection.setUseCaches(true);

            Object response = connection.getContent();
            Log.i(TAG, "Requesting http "+requestURL[0]);               

            if (response instanceof Bitmap) {
            }

            InputStream in = new BufferedInputStream(
                    connection.getInputStream());

            data = Common.readStream(in);

        } catch (MalformedURLException exception) {
            Log.e(TAG, "MalformedURLException");
        } catch (IOException exception) {
            Log.e(TAG, "IOException");
        } finally {
            if (null != httpUrlConnection)
                httpUrlConnection.disconnect();
        }
        return data;

    }

    protected void onPostExecute(String jsonString) {

        jsonObject = Common.getObjectFromJsonString(jsonString,3);
        ArrayList<HashMap<String, String>> wishList = Common.parseJson(jsonObject);

        if (pageEnd > Constants.limit) {
            wishAdapter = new WishRowAdaptor(getActivity(),wishList);

        }else{
            Common.cacheResponse(context,cachedName,jsonString);
            wishAdapter = new WishRowAdaptor(getActivity(),wishList);
            setListAdapter(wishAdapter);
            Common.setRefreshActionButtonState(false, optionsMenu);
        }       

        isDownloading = false;
    }

}


private ArrayList<HashMap<String, String>> getWishboardCached() {

    String jsonString = Common.getCachedResponse(context,cachedName);

    try {
        jsonObject = Common.getObjectFromJsonString(jsonString,3);
        ArrayList<HashMap<String, String>> wishList = Common.parseJson(jsonObject);
        return wishList;
    } catch (Exception e) {
        // TODO: handle exception
    }
    return null;
}   

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    if (view.getAdapter() != null
            && ((firstVisibleItem + visibleItemCount) >= totalItemCount) 
            &&          isDownloading == false) {


        pageStart = pageEnd + 1;
        pageEnd = pageStart + (Constants.limit -1);
        String url = getUrl();
        sendRequest(url);
        isDownloading = true;
    }
}
}

在上面的代码中如果在用户到达列表末尾后在 onPostExecute()中使用setListAdapter(wishAdapter);。列表视图得到更新,但所有预防性的条目都消失了。

这是我的自定义适配器

public class WishRowAdaptor extends BaseAdapter {


public WishRowAdaptor(Context context,
        ArrayList<HashMap<String, String>> arrayList) {

    // TODO Auto-generated constructor stub
    this.mData = arrayList;
    this.context = context;
    this.listAq = new AQuery(context);
    // mKeys = mData.keySet().toArray(new String[arrayList.size()]);
}

/* private view holder class */
private class ViewHolder {
    ImageView imageView;
    TextView txtWishName;
    ImageButton btn_touchwood;
    ImageButton btn_addwish;
    TextView tvGesture;
    TextView tvNotes;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    final HashMap<String, String> rowItem = (HashMap<String, String>) getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_wish, null);
        holder = new ViewHolder();

        holder.txtWishName = (TextView) convertView
                .findViewById(R.id.tvwishname);
        holder.imageView = (ImageView) convertView
                .findViewById(R.id.ivWishImage);
        holder.btn_touchwood = (ImageButton) convertView
                .findViewById(R.id.btn_touchwood);
        holder.btn_addwish = (ImageButton) convertView
                .findViewById(R.id.btn_addwish);
        holder.tvGesture = (TextView) convertView
                .findViewById(R.id.tvGestures);
        holder.tvNotes = (TextView) convertView
                .findViewById(R.id.tvNotesText);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    return convertView;
}
}

如何更新listview以便它应该将新更改推送到列表而不是替换所有内容。

1 个答案:

答案 0 :(得分:0)

在每次websevice调用后,您正在更改列表适配器的适配器。只需在更改数组列表中的项目后调用notifyDataSetChanged。也不要替换数组列表中的项目。将新内容添加到你的arraylist。