Populate listview after the addition of database

馋奶兔 提交于 2019-12-02 18:07:28

问题


I have an app which displays data in a fragment(uses webservice). After login, fragment fetches data from json and add to database, after that I want to populate it in my fragment. My fragment shows nothing after adding database.

public class GridFragment extends Fragment {
Context mContext;
GridView gridd;
GridAdapter adapter;
MagazineGetIssues mgi;
ArrayList<DataVal> Issues = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getActivity();
    mgi = new MagazineGetIssues();
    DBOperation db = new DBOperation(getActivity());
    Issues = db.getIssues();
    adapter = new GridAdapter(getActivity(), Issues);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_grid, container, false);
    gridd = (GridView) rootView.findViewById(R.id.gridViewAA);
    gridd.setAdapter(adapter);
    //getting Issues
    mgi.getIssues(mContext);
    adapter.addList(Issues);
    return rootView;
  }
}

GridAdapter class

public class GridAdapter extends BaseAdapter {

private final FragmentActivity activity;
ImageLoaderConfiguration imageLoaderConfiguration;
DisplayImageOptions displayImageOptions;
String url = "";
DisplayImageOptions imageOptions;
ImageLoader imLoad;
ArrayList<DataVal> issueList = null;

public GridAdapter(FragmentActivity activity, ArrayList<DataVal> cdate) {
    this.activity = activity;
    this.issueList = cdate;

    displayImageOptions = new DisplayImageOptions.
            Builder().cacheOnDisk(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300))
            .build();
    imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(activity)
            .defaultDisplayImageOptions(displayImageOptions).memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();
    ImageLoader.getInstance().init(imageLoaderConfiguration);
    imLoad = ImageLoader.getInstance();
    imageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).resetViewBeforeLoading(true).build();

}


@Override
public int getCount() {
    return issueList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater inflator = activity.getLayoutInflater();
        convertView = inflator.inflate(R.layout.grid_single, null);
        viewHolder = new ViewHolder();
        viewHolder.imView = (ImageView) convertView.findViewById(R.id.CoverPic);
        viewHolder.iss_date = (TextView) convertView.findViewById(R.id.issuedate);
        viewHolder.iss_price = (TextView) convertView.findViewById(R.id.price);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.iss_date.setText(issueList.get(position).getISSUE_DATE());
    viewHolder.iss_price.setText(issueList.get(position).getPRICE());
    imLoad.displayImage(url + issueList.get(position).getCOVER_PAGE_THUMB(), viewHolder.imView, imageOptions);
    return convertView;
}

public void addList(ArrayList<DataVal> val) {
    this.issueList = val;
    this.notifyDataSetChanged();
}

static class ViewHolder {
    protected TextView iss_date;
    protected TextView iss_price;
    protected ImageView imView;
  }
}

回答1:


try to replace this line

 this.issueList = val;

with

this.issueList.clear();
this.issueList.addAll(val);

in your BaseAdapter




回答2:


Change your adapter to extend ArrayAdapter and pass your list to super constructor of ArrayAdapter on initiation of your adapter class

Also don't forget to call notifyDataSetChanged() after adding to list.

Hope this helps.




回答3:


You have to add one method in BaseAdapter for update new data:

ArrayList<DataVal> issueList = null;

Set new method setUpdatedData to assign your new Arraylist of data

 public void setUpdatedData(ArrayList<DataVal> newData){
    this.issueList = newData;
   }

After set new data you have to call notifyDataSetChanged();

adapter.notifyDataSetChanged();

Done



来源:https://stackoverflow.com/questions/30846096/populate-listview-after-the-addition-of-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!