0

List Fragment 的两个问题: 1. 我有一个扩展 ListFragments 的类。在 onCreated 中,我正在设置我的自定义适配器。问题是当用户登录时,适配器为空,并且在用户搜索所有者之前不会有值。因此,当它尝试设置 listadapter 并发现 ArrayAdapter 对象为空时,它会引发异常。我有一个包含列表视图和文本视图的自定义布局,但我仍然收到错误消息。请参阅下面的示例代码。我将问题发生的代码行加粗。此外,我在其他几个我认为可能需要注意的部分加粗。

我在“Owner”类中实现了 Parcelable,以便可以将对象作为 ParcelableArray 传递。即使对象不为空,在 OwnerDetail 类中检索它也会显示为空,就好像我没有传递它一样。我看过几个例子,但我无法正确理解。我在这里做错了什么?注意:如果我在 OwnerDetail 类中调用 AsyncTask 并设置 ListAdapter,它将正常工作。这样做的问题是,一旦用户登录,它就会显示所有者列表,这不是预期的行为。我想要的行为是先登录,搜索车主,显示车主,双击车主,最后显示车主拥有的汽车列表。我正在做这个项目,所以我可以学习如何使用 ListFraments。

// Here is the entire code

package com.mb.carlovers;

import android.os.Parcel;
import android.os.Parcelable;

public class Car implements Parcelable {
    private String _make;
    private String _model;
    private String _year;

    public Car()
    {
      this._make = "";
      this._model = "";
      this._year = "";
    }

    public Car(String make)
    {
      this._make = make;
    }
    public Car(String make, String year)
    {
      this._make = make;
      this._year = year;
    }

    public Car(String make, String model, String year)
    {
      this._make = make;
      this._model  = model;
      this._year = year;
    }

    //Getters
    public String getMake()
    {
        return _make;
    }
    public String getModel()
    {
        return _model;
    }
    public String getYear()
    {
        return _year;
    }

    //Setters
    public void setMake(String make)
    {
         _make = make;
    }
    public void setModel(String model)
    {
        _model = model;
    }
    public void setYear(String year)
    {
        _year = year;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub

    }
}



package com.mb.carlovers;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CarDetail extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.customize_layout,container,false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        String[] myCars = {};
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter<String> carAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, myCars);
        setListAdapter(carAdapter);
    }

}


package com.mb.carlovers;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity implements OnClickListener{
 private Button btnLogin;
 private EditText etUsername, etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initializeVariables();
    }

    public void initializeVariables()
    {
      btnLogin = (Button) this.findViewById(R.id.bLogin);
      etUsername = (EditText)this.findViewById(R.id.etUserName);
      etPassword = (EditText)this.findViewById(R.id.etPassword);
      btnLogin.setOnClickListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        Log.i("Tag", "In Onclick Litener");
        String uName = etUsername.getText().toString();
        String pWord = etPassword.getText().toString();
        if(uName.equals("owner") && pWord.equals("1234"))
        {
            Log.i("Tag", "username =" + uName + "Password =" + pWord);
            Intent intent = new Intent(this, People.class);
            startActivity(intent);
        }
    }

}


package com.mb.carlovers;
import android.os.Parcel;
import android.os.Parcelable;

public class Owner implements Parcelable {

    private String _firstName;
    private String _lastName;
    private String _carId;
    private Car _car;

    public Owner()
    {
      this._firstName = "";
      this._lastName = "";
      this._carId = "";
    }

    public Owner(String lName)
    {
      this._lastName = lName;
    }
    public Owner(String lName, String cId)
    {
      this._lastName = lName;
      this._carId = cId;
    }

    public Owner(String lName, String fName, String cId)
    {
      this._lastName = lName;
      this._firstName  = fName;
      this._carId = cId;
    }

    public Owner(String lName, String fName, String cId, Car car)
    {
         this._lastName = lName;
         this._firstName  = fName;
         this._carId = cId;
         this._car = car;
    }

    //Getters
    public String getFirstName()
    {
        return _firstName;
    }
    public String getLastName()
    {
        return _lastName;
    }
    public String getCarId()
    {
        return _carId;
    }

    public Car getCar()
    {
        return _car;
    }
    //Setters
    public void setFirstName(String fName)
    {
         _firstName = fName;
    }
    public void setLastName(String lName)
    {
        _lastName = lName;
    }
    public void setCarId(String cId)
    {
        _carId = cId;
    }

    public void setCar(Car car)
    {
        _car = car;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(_firstName);
          dest.writeString(_lastName);
          dest.writeString(_carId);
          dest.writeParcelable(_car, flags);
    }

     public Owner(Parcel source){
         _firstName = source.readString();
         _lastName = source.readString();
         _carId = source.readString();
         _car = source.readParcelable(Car.class.getClassLoader());
   }

    public class MyCreator implements Parcelable.Creator<Owner> {
          public Owner createFromParcel(Parcel source) {
                return new Owner(source);
          }
          public Owner[] newArray(int size) {
                return new Owner[size];
          }
    }

}


package com.mb.carlovers;
import com.mb.carlovers.adapter.OwnerAdapter;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OwnerDetail extends ListFragment {

    OwnerAdapter ownerAdapter = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.customize_layout,container, false);
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Owner[] myOwners = null;
        super.onCreate(savedInstanceState);
        Bundle values = getActivity().getIntent().getExtras();
        if(values != null)
        {
           myOwners = (Owner[]) values.getParcelableArray("test");
        } 
        super.onActivityCreated(savedInstanceState);
        ownerAdapter = new OwnerAdapter(getActivity(), R.layout.owner_detail , myOwners);
        ownerAdapter.notifyDataSetChanged();

    }


package com.mb.carlovers;

import java.util.List;
import com.mb.carlovers.asynctask.OnwerAsyncTask;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class People extends FragmentActivity implements OnClickListener, OnItemSelectedListener {
  private Button search;
  private EditText etSearchBy, etSearchByID;
  private Spinner spOption;
  private String selectedOption = null;
  private TextView tvErrorMessage;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.people);
        InitializeVariables();
    }

    private void InitializeVariables()
    {
        etSearchBy = (EditText) this.findViewById(R.id.etByLastName);
        etSearchByID = (EditText) this.findViewById(R.id.etCarID);
        spOption = (Spinner) this.findViewById(R.id.spOption);
        search = (Button) this.findViewById(R.id.bSearch);
        search.setOnClickListener(this);
        tvErrorMessage = (TextView) this.findViewById(R.id.tvErrorMessage);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.spOptions, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spOption.setAdapter(adapter);
        spOption.setOnItemSelectedListener(this);

    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

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

    @Override
    public void onClick(View v) {
        String searchByName = etSearchBy.getText().toString();
        String searchById = etSearchByID.getText().toString();
       if(selectedOption == null || selectedOption == "All")
       {
           if(searchByName.matches("") || searchById.matches(""))
           {
               tvErrorMessage.setText("You must select a last name and car id");
           } else
           {

           }

       } else if(selectedOption == "Name")
       {
           if(!searchByName.matches(""))
           {
                      OnwerAsyncTask asynTask = new OnwerAsyncTask();
                     List<Owner> lt = null;
                      try {
                            lt = asynTask.execute("").get();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Owner myOwners[] = lt.toArray(new Owner[lt.size()]);
                        Bundle data = new Bundle();
                        data.putParcelableArray("test", myOwners);

           } else
           {
               tvErrorMessage.setText("You must enter the last name of the owner.");
           }

       } else if (selectedOption == "ID") 
       {

           if(!searchById.matches(""))
           {
              String st = null;
              String d = st;
           } else
           {
               tvErrorMessage.setText("You must enter the car id that you'd like to search.");
           }
       }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        switch(pos)
        {
        case 0:
            selectedOption = "All";
            break;
        case 1:
            selectedOption ="Name";
            break;
        case 2:
            selectedOption ="ID";
            break;
            default:
                selectedOption = null;
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        selectedOption ="ALL";
    }


}


package com.mb.carlovers.adapter;

import com.mb.carlovers.Car;
import com.mb.carlovers.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CarAdapter extends ArrayAdapter<Car> {

    private Context context;
    private int layoutResourceId;
    private Car data[] = null;

    public CarAdapter(Context context, int resource, Car[] data) {
        super(context, resource, data);
        this.context = context;
        this.layoutResourceId = resource;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        CarHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new CarHolder();
            holder.tvMake = (TextView) row.findViewById(R.id.tvMake);
            holder.tvModel = (TextView) row.findViewById(R.id.tvModel);
            holder.tvYear = (TextView) row.findViewById(R.id.tvYear);
            row.setTag(holder);
        } else
        {
            holder = (CarHolder) row.getTag();
        }

        Car item = data[position];
        holder.tvMake.setText(item.getMake().toString());
        holder.tvModel.setText(item.getModel().toString());
        holder.tvYear.setText(item.getYear().toString());

        return row;
    }

    public static class CarHolder
    {
        TextView tvMake;
        TextView tvModel;
        TextView tvYear;
    }
}


package com.mb.carlovers.adapter;

import com.mb.carlovers.Owner;
import com.mb.carlovers.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class OwnerAdapter extends ArrayAdapter<Owner> {

    private Context context;
    private int layoutResourceId;
    private Owner data[] = null;

    public OwnerAdapter(Context context, int textViewResourceId,Owner[] data) {
        super(context, textViewResourceId, data);
        this.context = context;
        this.layoutResourceId = textViewResourceId;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        OwnerHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new OwnerHolder();
            holder.tvFName = (TextView) row.findViewById(R.id.tvFirstName);
            holder.tvLName = (TextView) row.findViewById(R.id.tvLastName);
            holder.tvCId = (TextView) row.findViewById(R.id.tvCarID);
            row.setTag(holder);
        } else
        {
            holder = (OwnerHolder) row.getTag();
        }

        Owner item = data[position];
        holder.tvFName.setText(item.getFirstName());
        holder.tvLName.setText("Example");
        holder.tvCId.setText("1");
        return row;
    }

    static class OwnerHolder
    {
      TextView tvFName;
      TextView tvLName;
      TextView tvCId;
    }
}


package com.mb.carlovers.asynctask;

import java.util.ArrayList;
import java.util.List;

import com.mb.carlovers.Car;
import android.os.AsyncTask;

public class CarAsyncTask extends AsyncTask<String, Void, List<Car>> {

    private List<Car> item = null;
    @Override
    protected List<Car> doInBackground(String... params) {

        item = new ArrayList<Car>();
        item.add(new Car("Chevy","Caprice","2002"));
        item.add(new Car("Chevy","Malibu","2014"));
        item.add(new Car("Dodge","Stratus","2002"));
        item.add(new Car("Saturn","L300","2004"));

        return item;
    }

    @Override
    protected void onPostExecute(List<Car> result) {
        super.onPostExecute(result);
    }

}


package com.mb.carlovers.asynctask;

import java.util.ArrayList;
import java.util.List;

import com.mb.carlovers.Owner;
import android.os.AsyncTask;

public class OnwerAsyncTask extends AsyncTask<String, Void, List<Owner>> {

    private List<Owner> items = null;
    @Override
    protected List<Owner> doInBackground(String... params) {

        try
        {
            items = new ArrayList<Owner>();
            Owner own = new Owner();
            own.setFirstName("John");
            own.setLastName("Smith");
            own.setCarId("1");
            items.add(own);

            Owner own1 = new Owner();
            own1.setFirstName("Samantha");
            own1.setLastName("Right");
            own1.setCarId("2");
            items.add(own1);

            Owner own2 = new Owner();
            own2.setFirstName("Regie");
            own2.setLastName("Miller");
            own2.setCarId("3");
            items.add(own2);

            Owner own3 = new Owner();
            own3.setFirstName("Mark");
            own3.setLastName("Adam");
            own3.setCarId("4");
            items.add(own3);

        } catch(Exception ex)
        {
            ex.toString();
        }
        return items;
    }

    @Override
    protected void onPostExecute(List<Owner> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}


// car_detail.xml
<?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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Car Detail Page" />

</LinearLayout>


// car_row.xml
<?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="horizontal" >

    <TextView
        android:id="@+id/tvMake"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/tvModel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <TextView
        android:id="@+id/tvYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>


//customize_layout.xml

<?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" >

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

     <TextView
        android:id="@id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No record to be displayed." 
        />
</LinearLayout>


//Login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Login" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="User Name"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
         />

    <EditText
        android:id="@+id/etUserName"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
         >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" 
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/bLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:text="Login" />

</LinearLayout>


//owner_detail.xml
<?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="horizontal" >

 <TextView
        android:id="@+id/tvFirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvLastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tvCarID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView" />

</LinearLayout>


// People.xml

<?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="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
         >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by last name" 
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            />

        <EditText
            android:id="@+id/etByLastName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search by car id" 
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10sp"
            />

        <EditText
            android:id="@+id/etCarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" 
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10sp"
            />

        <Spinner
            android:id="@+id/spOption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:entries="@array/spOptions"
            />

        <TextView
            android:id="@+id/tvErrorMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF0000"
            android:layout_marginTop="10dp"
            android:text="" />

        <Button
            android:id="@+id/bSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Button" />

    </LinearLayout>

   <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:layout_weight="2"
        >

        <fragment
            android:id="@+id/fOwnerDetail"
            android:name="com.mb.carlovers.OwnerDetail"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
             />

        <fragment
            android:id="@+id/fragment1"
            android:name="com.mb.carlovers.CarDetail"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
             />

    </LinearLayout>

</LinearLayout>





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(ownerAdapter);
    }
}
4

1 回答 1

1

你的代码有很多问题:

1永远不要OwnerDetail像在课堂上那样自己调用生命周期方法super.onActivityCreated(savedInstanceState);

2Bundle values = getActivity().getIntent().getExtras(); ... values.getParcelableArray("test");通常会失败,因为这段代码将获取活动引用,获取Intent启动活动的 ,然后尝试查找在该test键下传入的数据Intent。您不会将此类数据传递给People活动,因此将找不到任何东西。您通常希望Bundle在创建片段时传递包含数据的 a。

3 如果您在 xml 布局中使用片段,您将无法使用 aBundle来传递数据,而是您可以使用事务手动添加片段,然后使用 aBundle或在片段类中创建一个 setter 方法并使用它。因此Bundle data = new Bundle(); data.putParcelableArray("test", myOwners);,获取对片段的引用并通过myOwners方法传递数组,而不是什么都不做的。

4AsyncTask如果您将它们与它们一起使用,您将毫无用处,.get()因为该get()方法将等待AsyncTask完成,同时也会阻塞 UI 踏板。相反,您应该只开始AsyncTask然后在onPostExecute()传递数据。

这是一个使用简单方法的示例,它不会更改您的大部分代码(如果您手动添加片段会发生这种情况):

// where you start the owner asynctask
if(!searchByName.matches("")) {
    OnwerAsyncTask asynTask = new OnwerAsyncTask(People.this);
    asynTask.execute("");
}
//...

然后改成OnwerAsyncTask这样:

public class OnwerAsyncTask extends AsyncTask<String, Void, List<Owner>> {

  private List<Owner> items = null;
  private FragmentActivity mActivity;  

  public OnwerAsyncTask(FragmentActivity activity) {
      mActivity = activity;
  }

  // doInBackground()...

  @Override
  protected void onPostExecute(List<Owner> result) {
       //I'm assuming that items is the data you want to return, so 
       // find the OwnerDetail fragment and directly assign the data
       OwnerDetail fragment = (OwnerDetail)mActivity.getSupportFragmentManager().findFragmentById(R.id.fOwnerDetail);
       fragment.setData(); // to setData you'll pass the data you have in the AsyncTask
                           // the items list? or you transform that into an array?         
  }

在 OwnerDetail 片段中:

public class OwnerDetail extends ListFragment {

    OwnerAdapter ownerAdapter = null;

    public void setData() {
       // update the adapter, create a new one etc.
    }
于 2013-07-31T14:08:12.513 回答