Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义列表适配器和异步任务_Android_Custom Adapter - Fatal编程技术网

Android 自定义列表适配器和异步任务

Android 自定义列表适配器和异步任务,android,custom-adapter,Android,Custom Adapter,只是ListFragment的自定义适配器有问题。 AsyncTask将对象数组返回到ListFragment,它实例化了一个自定义适配器,该适配器传递上下文、要充气的布局和数据(对象数组)。 但是在运行时,我在自定义适配器的getCount()方法中得到一个NullPointerException,因为mData为null 有人知道怎么处理吗?如何“正确地”将数据传递到customAdapter 谢谢 AsyncTask onPostExecute(): @Override protected

只是
ListFragment
的自定义适配器有问题。
AsyncTask
将对象数组返回到
ListFragment
,它实例化了一个自定义适配器,该适配器传递上下文、要充气的布局和数据(对象数组)。 但是在运行时,我在自定义适配器的
getCount()
方法中得到一个
NullPointerException
,因为
mData
为null

有人知道怎么处理吗?如何“正确地”将数据传递到
customAdapter

谢谢

AsyncTask onPostExecute():

@Override
protected void onPostExecute(Listing[] listings) {
    if (listings != null) {

        ListingFragment.mListings = listings;

    }
}
public class ListingFragment extends ListFragment {

public static ItemAdapter mAdapter;
public static Listing[] mListings;

// Constructor
public ListingFragment() {

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FetchListing task = new FetchListing();
    String query = getArguments().getString("test");
    if (query != null) {
        task.execute(query);
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAdapter = new ItemAdapter(getActivity(), R.layout.item_row, mListings);
    setListAdapter(mAdapter);

}
列表片段:

@Override
protected void onPostExecute(Listing[] listings) {
    if (listings != null) {

        ListingFragment.mListings = listings;

    }
}
public class ListingFragment extends ListFragment {

public static ItemAdapter mAdapter;
public static Listing[] mListings;

// Constructor
public ListingFragment() {

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FetchListing task = new FetchListing();
    String query = getArguments().getString("test");
    if (query != null) {
        task.execute(query);
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAdapter = new ItemAdapter(getActivity(), R.layout.item_row, mListings);
    setListAdapter(mAdapter);

}
ItemAdapter

public class ItemAdapter extends BaseAdapter {
Context mContext;
int mLayoutResourceId;
Listing mData[];

// Constructor
public ItemAdapter(Context context, int layoutResourceId, Listing[] data) {
    this.mContext = context;
    this.mLayoutResourceId = layoutResourceId;
    this.mData = data;
}



@Override
public int getCount() {
    return mData.length;
}

@Override
public Object getItem(int position) {
    return mData[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listItem = convertView;
    Listing listingItem = mData[position];

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listItem = inflater.inflate(mLayoutResourceId, parent, false);

    ImageView listingItemImageView = (ImageView) listItem.findViewById(R.id.listing_item_icon);
    TextView listingItemTextView = (TextView) listItem.findViewById(R.id.listing_item_name);

    listingItemImageView.setImageURI(Uri.parse(listingItem.getUrl()));
    listingItemTextView.setText(listingItem.getTitle());

    return listItem;
}
}

您的片段是
ListingFragment
。因此,您需要在这里从
ListFragment.mListings=listings
to
ListingFragment.mListings=listings所以只需移动这个
mAdapter=newitemAdapter(getActivity(),R.layout.item_行,mListings);setListAdapter(mAdapter)UI
将在该方法中更新,而在
onCreate()
方法中,您的
mListings
为空。AsyncTask位于单独的类中,因此无法调用getActivity()或setListAdapter()因此,您需要为您的
AsyncTask
创建一个构造函数,在其中传递您的上下文,当您在活动中调用您的AsyncTask时,则需要将上下文与
YourActivityName一起使用。这将
。然后无法调用setListAdapter()。在传递适配器的ListFragment onCreate()中简单地调用它是行不通的。我刚买了一个永不停止加载的旋转器