ListFragment更新问题

时间:2012-06-08 11:47:57

标签: android performance android-fragments android-viewpager android-listfragment

我在ViewPager中有一个自定义布局的ListFragment。此ListFragment和自定义布局执行ListView和调用AsyncTask的Button。

完成工作后,此AsyncTask在onPostExecute中调用refresh方法(在ListFragment中创建)以更新适配器(下面的代码)。 当我按下此按钮一切正常,直到方向发生变化

然后,如果我按下按钮,列表会更新,但没有添加或删除行。因此,如果我只有一行,并且在更新适配器后为空,则不会对List进行任何更改。另一方面,如果某些行应添加到列表中,我们只会注意到现有行已更改其内容。

如果在此“失败”更新后发生方向更改,则列表显示正常。所以我们可以说列表只会在方向更改后更新适配器的内容

对主要活动的引用总是传递给ListFragment,适配器已正确更新(我选中了白色日志)。

任何人都有任何想法?我会非常感激。

布局:

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

    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="9" />

    <Button android:id="@+id/Refresh" 
        android:layout_width="wrap_content" 
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:text="Update" /> 

</LinearLayout>

代码:

    public class ResFragment extends ListFragment {
    View view = null;
    public static AdapterRes adapter; 

    public static ArrayList<NodeP> mItems = new ArrayList<NodeP>();
    public static MyAppActivity ref ;   


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new AdapterRes(ref, ref.result);
        adapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.result,null);
        this.setListAdapter(adapter);

        Button refresh = (Button) view.findViewById(R.id.Refresh);
        refresh.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ref.initDownloadTask();             
            }
        });

        return view; 
    }

    public void refresh(){ 
        mItems.clear();
        adapter = new AdapterRes(ref, ref.result);
        this.setListAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    class AdapterRes extends ArrayAdapter<NodeP> {

      Activity context = ref;
      AdapterRes(Activity context, List<NodeP> objects) {
          super(context, R.layout.row_res, objects); 
          this.context = ref;   
      }

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

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

              holder =  new ViewHolder();   

              holder.local = (TextView)item.findViewById(R.id.local);
              holder.resPartido = (TextView)item.findViewById(R.id.res);
              holder.foreign = (TextView)item.findViewById(R.id.foreign);

              item.setTag(holder); 
          }
          else                                        
          {                         
              holder = (ViewHolder)item.getTag(); 
          }
          holder.local.setText(ref.result.get(position).local);   
          holder.res.setText(ref.result.get(position).res);
          holder.foreign.setText(ref.result.get(position).foreign);
          return(item); 
      }

      public class ViewHolder {
          public TextView local;
          public TextView res;
          public TextView foreign;
      }

    }
}

0 个答案:

没有答案