如何为嵌套片段实现自定义适配器

时间:2017-04-15 23:40:29

标签: java android imageview fragment adapter

我正在尝试在嵌套片段中创建一个列表视图,该片段将作为元素图像保存在其下方,但我不断在我创建的自定义适配器中获取nullpointer错误以填充列表视图。这是代码:

自定义适配器:

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

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

public class ImageAdapter extends ArrayAdapter<Image> {

    ArrayList<Image> imageList;
    private Context mContext;
    int resource;

    public ImageAdapter(Context context, int resource, ArrayList<Image> imageObjects) {
        super(context, resource, imageObjects);
        this.imageList = imageObjects;
        this.mContext = context;
        this.resource = resource;
    }

    public ArrayList<Image> getImageList() {
        return imageList;
    }

    public void setImageList(ArrayList<Image> imageList) {
        this.imageList = imageList;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public int getResource() {
        return resource;
    }

    public void setResource(int resource) {
        this.resource = resource;
    }

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

        if(convertView==null){
            LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);

        }

        Image image = getItem(position);

        ImageView layoutImage = (ImageView)convertView.findViewById(R.id.membershipimage);
        layoutImage.setImageResource(image.getImageResourceID());
        TextView layoutText = (TextView)convertView.findViewById(R.id.membershipCaption);
        layoutText.setText(image.getImageCaption());

        return super.getView(position, convertView, parent);
    }
}

我使用的片段,在另一个片段中:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;

import java.util.ArrayList;

public class MembershipFragment extends Fragment {

    ArrayList<Image> imageList;
    ListView membershipListView;

    public static MembershipFragment newInstance() {
        MembershipFragment fragment = new MembershipFragment();
        return fragment;
    }

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

    }



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

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        imageList = new ArrayList<>();
        for(int i=0; i<3; i++){
            Image image = new Image(R.drawable.cinnamon_apple_breakfast, "Test Caption");
            imageList.add(image);
        }
        membershipListView = (ListView)view.findViewById(R.id.membershipListView);
        try{
            ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item, imageList);
            membershipListView.setAdapter(adapter);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

当我尝试将图像资源ID设置为imageview时,应用程序在启动时不断崩溃,在getView方法的自定义适配器中抛出nullpointer异常。问题在哪里?

1 个答案:

答案 0 :(得分:0)

我认为你的布局令人困惑。

你应该在这里使用getActivity

ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item

你在那里传递了那个布局资源。但在适配器内部,您使用的是不同的布局。

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

    if(convertView==null){
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);

    }

你假设了一个getResource方法,出于某种原因?你永远不会叫它。

我认为您不希望Fragment布局包含Fragment布局列表(因为递归)

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

    return view;
}