Android列表视图未更新

时间:2014-08-18 15:06:42

标签: android android-listview android-viewpager baseadapter

我工作viewpager.i有一些pages.i也有listview女性适应一些图像和文本。 我写函数巫婆有int位置巫婆意味着每个位置我试图适应不同的值。 我在viewpager的setOnPageChangeListener方法中使用此函数,但我无法更新我的listview

这是我的适配器源代码

public class MyAdapter extends BaseAdapter {

private Context activity;
private List<CopyOfServerItems> items;
private CopyOfServerItems objBean;
private int row;

public ImageLoader imageLoader;


public MyAdapter(Context act, int resource, List<CopyOfServerItems> arrayList
        ) {
    super();
    this.activity = act;
    this.row = resource;
    imageLoader = new ImageLoader(act.getApplicationContext());
    this.items = arrayList;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row, null);

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

    if ((items == null) || ((position + 1) > items.size()))
        return view;

    objBean = items.get(position);



    holder.title = (TextView) view.findViewById(R.id.cat_title);
    holder.img = (ImageView) view.findViewById(R.id.cat_image);



    holder.title.setText(objBean.getTitle());





    imageLoader.DisplayImage(objBean.getThumbnail(), holder.img);
    //holder.img.setImageResource(Imageid[position]);

    return view;
}

public class ViewHolder {
    public TextView  title;
    public ImageView img;

}

@Override
public int getCount() {

    return items.size();
}

@Override
public Object getItem(int position) {

    return null;
}




@Override
public long getItemId(int position) {

    return 0;
}
}

这是我的java codee

public class CategoryViewPager extends Fragment {
public final static String TAG = CategoryViewPager.class
        .getSimpleName();
public static List<CopyOfServerItems> arrayOfList;

public static List<CopyOfServerItems> arrayOfList1;

private MyAdapter objAdapter;

public static CategoryViewPager newInstance() {
    return new CategoryViewPager();
}


private ViewPager mViewPager;
private ListView cat_listview;
private PagerTitleStrip mPagerTitleStrip;
String result;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.azercell_category_viewpager,
            container, false);
    arrayOfList = new ArrayList<CopyOfServerItems>();
    arrayOfList1 = new ArrayList<CopyOfServerItems>();
    final List<View> pages = new ArrayList<View>();
    final ArrayList<String> titles = new ArrayList<String>();

    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    mPagerTitleStrip = (PagerTitleStrip) rootView
            .findViewById(R.id.pagertitle);

    LayoutInflater inflater1 = LayoutInflater.from(getActivity());
    View page = inflater1.inflate(R.layout.azercell_category_pages, null);

    page = inflater.inflate(R.layout.azercell_category_pages, null);

    cat_listview = (ListView) page.findViewById(R.id.cat_listview);

    mPagerTitleStrip.setTextColor(Color.BLUE);



    for (int i = 0; i < arrayOfList.size(); i++) {

        page = inflater.inflate(R.layout.azercell_category_pages, null);
        cat_listview = (ListView) page.findViewById(R.id.cat_listview);
        cat_listview.setAdapter(objAdapter);

        titles.add(arrayOfList.get(i).getTitle());




        pages.add(page);



    }
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            try {

                Log.e("position viewpager", String.valueOf(arg0));
                parseJsonFromServer(result, arg0);
                objAdapter.notifyDataSetChanged();

            } catch (JSONException e) {

                e.printStackTrace();
            }

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {


        }

        @Override
        public void onPageScrollStateChanged(int arg0) {


        }
    });


    PagerAdapter p_adapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

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

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(pages.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return titles.get(position);
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(pages.get(position));
            return pages.get(position);
        }

    };
    mViewPager.setAdapter(p_adapter);

    return rootView;
}





public String parseJsonFromServer(String server, int position)
        throws JSONException {
    JSONArray jsonArray = null;
    JSONObject mainJson = new JSONObject(server);
    jsonArray = mainJson.getJSONArray("result");

    JSONArray children = jsonArray.getJSONObject(position).getJSONArray(
            "children");

    for (int j = 0; j < children.length(); j++) {

        CopyOfServerItems objItem = new CopyOfServerItems();
        JSONObject data = children.getJSONObject(j).getJSONObject("data");

        objItem.setTitle(data.getString("title"));
        objItem.setThumbnail(data.getString("thumbnail"));
        Log.e("123123123123", data.getString("title"));

        arrayOfList1.add(objItem);
    }


    setAdapterToListview();
    return server;
}


public void setAdapterToListview() {
    objAdapter = new MyAdapter(getActivity(),
            R.layout.azercell_category_adapter, arrayOfList1);

    objAdapter.notifyDataSetChanged();


}

}

我的parseJsonFromServer解析服务器的位置。当位置发生变化时也必须改变我的json.i做了它。但如果我使用位置0然后位置1我的适配器无法更新。我可以显示所有值(我的意思是0和1位置) 我怎么能解决我的问题? 如果有人知道解决方案,请帮助我

1 个答案:

答案 0 :(得分:0)

修改:您的所有代码都显示错误我很抱歉这可能无效。

首先,最好将位置作为对象id返回:

@Override
public long getItemId(int position) {

    return position;
}

其次,您不应该更改适配器以更新listview。您应该更新适配器数据。

将此添加到您的适配器:

updateData( int resource, List<CopyOfServerItems> arrayList ){

    this.row = resource;
    this.items = arrayList;

}

并使用以下命令更改setAdapterToListView:

updateListView( int resource, List<CopyOfServerItems> arrayList ){

     objAdapter.updateData( resource, arrayList );
     objAdapter.notifyDataSetChanged();

}

我想用土耳其语或阿塞拜疆语进行解释,但网站规则不允许我这样做。所以我能做的最好的就是帮助你。希望它有效。

编辑:将您的代码更改为:

public class CategoryViewPager extends Fragment {

public final static String TAG = CategoryViewPager.class
        .getSimpleName();
public static List<CopyOfServerItems> arrayOfList;

public static List<CopyOfServerItems> arrayOfList1;

private MyAdapter[] objAdapter;

public static CategoryViewPager newInstance() {
    return new CategoryViewPager();
}


private ViewPager mViewPager;

private ListView[] cat_listview;

private PagerTitleStrip mPagerTitleStrip;
String result;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.azercell_category_viewpager,
            container, false);
    arrayOfList = new ArrayList<CopyOfServerItems>();
    arrayOfList1 = new ArrayList<CopyOfServerItems>();
    final List<View> pages = new ArrayList<View>();
    final ArrayList<String> titles = new ArrayList<String>();

    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    mPagerTitleStrip = (PagerTitleStrip) rootView
            .findViewById(R.id.pagertitle);

    mPagerTitleStrip.setTextColor(Color.BLUE);

    cat_listview = new ListView[  arrayOfList.size() ];
    objAdapter   = new MyAdapter[ arrayOfList.size() ];

    for (int i = 0; i < arrayOfList.size(); i++) {

        View page = inflater.inflate(R.layout.azercell_category_pages, null);
        cat_listviews[ i ] = (ListView) page.findViewById(R.id.cat_listview);

        objAdapter[ i ] = new MyAdapter( [put some paremeters here] );

        cat_listview[ i ].setAdapter(objAdapter[ i ] );

        titles.add(arrayOfList.get(i).getTitle());

        pages.add(page);    
    }

    .........


    public String parseJsonFromServer(String server, int position)

        throws JSONException {
    JSONArray jsonArray = null;
    JSONObject mainJson = new JSONObject(server);
    jsonArray = mainJson.getJSONArray("result");

    JSONArray children = jsonArray.getJSONObject(position).getJSONArray(
            "children");

    for (int j = 0; j < children.length(); j++) {

        CopyOfServerItems objItem = new CopyOfServerItems();
        JSONObject data = children.getJSONObject(j).getJSONObject("data");

        objItem.setTitle(data.getString("title"));
        objItem.setThumbnail(data.getString("thumbnail"));
        Log.e("123123123123", data.getString("title"));

        arrayOfList1.add(objItem);
    }


    updateListview( resource, obj, position );
    return server;
}


updateListView( int resource, List<CopyOfServerItems> arrayList ){

     objAdapter[ position ].updateData( resource, arrayList );
     objAdapter[ position ].notifyDataSetChanged();

}

以及我之前发送给你的适配器的代码。