ListView不会使用更新的适配器进行更新

时间:2014-06-24 08:44:54

标签: android listview android-fragments android-listview adapter

我在栏中有一个带刷新按钮的活动。 在这个活动里面有一个片段,它调用asyncTask来获取json格式的数据。 第一次创建活动时,工作正常,列表视图显示一切正常。

当我在活动栏中按下刷新按钮时,我将活动调用到一个片段方法'myOnResume',该方法再次调用相同的异步任务。

我检查了任务返回的json是否已更新,但listview没有更新(使用与第一次'showListaGrupos'相同的方法)。

我尝试添加句子“adapter.notifyDataSetChanged();”但仍然是问题。 Listview不会更新,并显示第一次创建活动时的相同内容。

你能帮助我吗?

片段代码:

public class GruposFragment extends Fragment {

    View rootView;  
    private Vector<GruposClass> vectorGrupos;
    private ListView lvShowGroups;

    public GruposFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_grupos, container, false);
        ActionBar ab = this.getActivity().getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);

        lvShowGroups = (ListView)rootView.findViewById(R.id.lvGroups);
   String       id="1";
        GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
        getGrupos.execute(id);

        return rootView;
    }

//called from onPostExecute 
public void showListaGrupos(String result) {
        vectorGrupos=new Vector<GruposClass>();

            //create the vector with json returned.   
                    for(...){
                      vectorGrupos.add(new GruposClass(x,y,z...));
                      }   

if(adapter==null){
        adapter = new adapterGroupsClasses(getActivity());
    }else{
        adapter.changeListVals();
    }

        adapter.notifyDataSetChanged();

        lvShowGroups.setAdapter(adapter);

        lvShowGroups.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                ...
            }
        });
    }

    class adapterGroupsClasses extends ArrayAdapter<GruposClass> {

        Activity context;

        adapterGroupsClasses(Activity context) {
            super(context, R.layout.listitem_groupsline, vectorGrupos);
            this.context = context;
        }

     public void changeListVals(){
      this.clear();
      this.addAll(vectorGrupos);
    }

        public View getView(int position, View convertView, ViewGroup parent) 
    {
        View item = convertView;
        ViewHolder holder;

        if(item == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            item = inflater.inflate(R.layout.listitem_groupsline, null);

            holder = new ViewHolder();
            holder.id = (TextView)item.findViewById(R.id.TvIdGroup);
            holder.nombre = (TextView)item.findViewById(R.id.TvName);
            holder.descripcion = (TextView)item.findViewById(R.id.TvDescription);    

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

        holder.id.setText(vectorGrupos.get(position).getId());
        holder.nombre.setText(vectorGrupos.get(position).getNombre());
        holder.descripcion.setText(vectorGrupos.get(position).getDescripcion());            

        return(item);
    }
    }

    static class ViewHolder {
        TextView id;
        TextView nombre;
        TextView descripcion;
    }

//called from Activity bar button refresh
    public void myOnResume() {
        GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
        getGrupos.execute(id);
    }

}

Vector的类是:

public class GruposClass {
        private String id;
        private String nombre;
        private String descripcion;

        public GruposClass(String i, String n, String d){
            id=i;
            nombre = n;
            descripcion = d;
        }
        public String getNombre(){
            return nombre;
        }
        public String getDescripcion(){
            return descripcion;
        }
        public String getId(){
            return id;
        }
    }

活动是:

public class Grupos extends FragmentActivity {

    GruposFragment mainFragment;
    Bundle instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        instance=savedInstanceState;
        super.onCreate(instance);
        setContentView(R.layout.activity_grupos);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new GruposFragment()).commit();
        }

        mainFragment = (GruposFragment)getFragmentManager().findFragmentById(R.id.listGroup);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        createMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.grupos, menu);
        return true;
    }

    private void createMenu(Menu menu) {
        MenuItem itemRefresh = menu.add(0,1,1,"refresh");
        {
            itemRefresh.setIcon(R.drawable.refresh);
            itemRefresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);           
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        ...
        if (id == 1) {
            mainFragment.myOnResume();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView  android:id="@+id/textViewGroup"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textStyle="bold" 
        android:text="@string/cabeceraGrupos" />

    <ListView android:id="@+id/lvGroups" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" />

</LinearLayout>

(listView是自定义Listview)

提前谢谢你!

1 个答案:

答案 0 :(得分:1)

您每次都重新初始化一个新的适配器,并且您在新的适配器实例上调用notifydatasetchanged,而不是旧适配器实例。 尝试这个结构一次

public class GruposFragment extends Fragment {

...     
private Vector<GruposClass> vectorGrupos;
private ListView lvShowGroups;
private adapterGroupsClasses adapter;

public GruposFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_grupos, container, false);
    ...

    lvShowGroups = (ListView)rootView.findViewById(R.id.lvGroups);

    GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
    getGrupos.execute(id);

    return rootView;
}

//called from onPostExecute

public void showListaGrupos(String result) {
    vectorGrupos=new Vector<GruposClass>();
...

    //create the vector with json returned.      
if(adapter!=null){
    adapterGroupsClasses adapter = 
            new adapterGroupsClasses(getActivity());
            }else{
adapter.changeListVals();
            }

    adapter.notifyDataSetChanged();



    lvShowGroups.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            ...
        }
    });
}

class adapterGroupsClasses extends ArrayAdapter<GruposClass> {

    Activity context;

    adapterGroupsClasses(Activity context) {
        super(context, R.layout.listitem_groupsline, vectorGrupos);
        this.context = context;
    }

   public void changeListVals(){
  this.clear();
  this.addAll(vectorGrupos);

}

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View item = convertView;
        ViewHolder holder;

        if(item == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            item = inflater.inflate(R.layout.listitem_groupsline, null);

            holder = new ViewHolder();
            ... 
        return(item);
    }
}

static class ViewHolder {
    TextView id;
    TextView nombre;
    TextView descripcion;
}

//called from Activity bar button refresh
public void myOnResume() {
    GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
    getGrupos.execute(id);
}

}