Populate listview after the addition of database

85 views Asked by At

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;
  }
}
3

There are 3 answers

1
Kartheek On

try to replace this line

 this.issueList = val;

with

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

in your BaseAdapter

0
Akshay Mehta On

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.

4
Hiren Patel On

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