I have a ListFragment with a custom Layout inside of a ViewPager. This
ListFragment and the custom layout performs a ListView and a Button
which calls an AsyncTask.

After work is done, this AsyncTask calls in onPostExecute the refresh
method (created inside ListFragment) in order to update the adapter
(code below). When I press this button everything works fine until an
orientation change occurs.

Then If I press the button, the list is updated but no rows are added
or deleted. So i.e if I only have one row and after updating the
adapter is empty, no changes are appreciated on List. On the other
hand, If some rows should be added to list we only notice that the
existing row has changed its content.

If after this "failed" update an orientation change happens, the list
is shown fine. So we can say that the list only updates well the
adapter's content after orientation change.

A reference to main activity is always passed to ListFragment and the
adapter is updated properly (I checked it whit Logs).

Anyone has any idea? I will appreciate so much.

Code:

<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;
      }

    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to