如何在基本适配器中添加notifyDataSetChanged方法

时间:2013-10-24 06:33:13

标签: android

当我使用此代码GridView时,如何在我的代码中更新cla.notifyDataSetChanged;是说无法解决如何在我的适配器类中添加notifyDataSetChanged方法?请帮帮我,你做什么?我的适配器类没有notifyDataSetChange方法

public class fifthscreen extends Activity {
    private AQuery androidAQuery;
    CategoryListAdapter3 cla;
    androidAQuery = new AQuery(this);
    static ArrayList<String> Category_name = new ArrayList<String>();
    static ArrayList<String> Category_image = new ArrayList<String>();

    cla = new CategoryListAdapter3(fifthscreen.this);
    gidView.setAdapter(cla);

适配器类

public class CategoryListAdapter3 extends BaseAdapter {

    private Activity activity;


    private AQuery androidAQuery;


    public CategoryListAdapter3(Activity act) {
        this.activity = act;
    //  imageLoader = new ImageLoader(act);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return fifthscreen.Category_ID.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        androidAQuery = new AQuery(getcontext());
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.viewitem2, null);
            holder = new ViewHolder();

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


        holder.txtText = (TextView) convertView.findViewById(R.id.title2);
        holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2);

        holder.txtText.setText(fifthscreen.Category_name.get(position));


       androidAQuery.id(holder.imgThumb).image(fifthscreen.Category_image.get(position), true,true);

        return convertView;
    }
    private Activity getcontext() {
        // TODO Auto-generated method stub
        return null;
    }
    static class ViewHolder {
        TextView txtText;
        ImageView imgThumb;
    }

}

3 个答案:

答案 0 :(得分:0)

根据您的问题,我理解的是您想要notifyDataSetChanged()方法的正确位置

解决方案是notifyDataSetChanged()在适配器数据更改时使用,因此最初当您填充数组时它是静态的并且不会被更改,因此这里不需要。所以在那之后根据你的逻辑

static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();

之后必须致电

进行更改
cla.notifyDataSetChanged() 

将刷新列表视图。

答案 1 :(得分:0)

在gidView.setAdapter(cla)之后; 在该行之前,你的适配器尚未设置..

但是您需要决定何时调用它,意味着在对adpater的某个组件进行更改并且希望立即显示它之后调用此行(cla.notifyDataSetChanged())。

答案 2 :(得分:0)

您可以使用意图 自行刷新该活动,但这不是一个好方法!