添加数据库后填充listview

时间:2015-06-15 13:12:13

标签: android sqlite android-listview android-adapter android-gridview

我有一个应用程序,它在片段中显示数据(使用webservice)。 登录后,片段从json获取数据并添加到数据库,之后我想在我的片段中填充它。添加数据库后,我的片段没有显示任何内容。

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类

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 个答案:

答案 0 :(得分:0)

尝试替换此行

 this.issueList = val;

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

BaseAdapter

答案 1 :(得分:0)

更改适配器以扩展ArrayAdapter并在启动适配器类时将列表传递给ArrayAdapter的超级构造函数

在添加到列表后也不要忘记调用notifyDataSetChanged()。

希望这有帮助。

答案 2 :(得分:0)

您必须在 BaseAdapter 添加一个方法才能更新新数据:

_test=False

设置新方法 setUpdatedData 以指定新的 Arraylist 数据

ArrayList<DataVal> issueList = null;

设置新数据后,您必须致电 notifyDataSetChanged();

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

完成