我不能在我的片段中设置我的LISTVIEW适配器

时间:2015-08-04 14:40:33

标签: java android

这是我的片段类。我有一个带有虚拟数据的管理器类,以及一个扩展基本适配器的adpater类。我的片段中有错误

(questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());) 

我似乎不明白这个错误。因为我在questtionsAdapter构造函数

中传递了正确的参数和参数
       package sandwich.playmaker.com.afterschool.fragments;

import android.app.Activity;
import android.net.Uri;
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.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import sandwich.playmaker.com.afterschool.R;
import sandwich.playmaker.com.afterschool.adapters.QuestionsAdapter;
import sandwich.playmaker.com.afterschool.manager.QuestionsManager;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link QuestionsFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link QuestionsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QuestionsFragment extends Fragment {
    private ListView lvQuestions;
    private EditText etSearchQuestion;
    private Spinner spImportance;
    private QuestionsAdapter questionsAdapter;
    private QuestionsManager questionsManager;

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


}

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment QuestionsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static QuestionsFragment newInstance(String param1, String param2) {
        QuestionsFragment fragment = new QuestionsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public QuestionsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View v = inflater.inflate(R.layout.fragment_questions, container, false);

        etSearchQuestion = (EditText) v.findViewById(R.id.etSearchQuestion);
        spImportance = (Spinner) v.findViewById(R.id.spImportance);
        lvQuestions = (ListView) v.findViewById(R.id.lvQuestions);

        questionsManager =new QuestionsManager();
       questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());
        lvQuestions.setAdapter(questionsAdapter);

        return v;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

这是我的经理班

   public class QuestionsManager {

private ArrayList<QuestionsModel>allQuestions;


public QuestionsManager() {
    this.allQuestions = allQuestions;
}

public ArrayList<QuestionsModel>getAllQuestions(){
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Banku", "Food",
            "9:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Kenkey", "Food",
            "9:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Rice", "Food",
            "10:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Kelewele", "Food",
            "11:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Orange", "Food",
            "12:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Banana", "Food",
            "13:00pm"));
    allQuestions.add(new QuestionsModel(R.drawable.monarki, "How to eat Gob3", "Food",
            "14:40pm"));

    return allQuestions;
}

}

这是我的适配器类

        public class QuestionsAdapter extends BaseAdapter {
private Context ctx;
//private QuestionsModel questionsModel;
private ArrayList<QuestionsModel>allQuestions;

public QuestionsAdapter(Context ctx,  ArrayList<QuestionsModel> allQuestions) {
    this.ctx = ctx;
    this.allQuestions = allQuestions;

   // this.questionsModel = questionsModel;
}


@Override
public int getCount() {
    return allQuestions.size();
}

@Override
public Object getItem(int position) {
    return allQuestions.get(position);
}

@Override
public long getItemId(int position)
{
    return allQuestions.get(position).getIvProfilePic();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    QuestionsModel questionsModel = allQuestions.get(position);
    LayoutInflater  inflater=  (LayoutInflater) ctx.getSystemService(Context
            .LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.activity_questions, null);
    ImageView ivProfilePic = (ImageView) convertView.findViewById(R.id.ivProfilePic);
    TextView tvQuestion = (TextView) convertView.findViewById(R.id.tvQuestion);
    TextView tvTag = (TextView) convertView.findViewById(R.id.tvQuestion);
    TextView tvTime= (TextView) convertView.findViewById(R.id.tvQuestion);

    ivProfilePic.setImageResource(questionsModel.getIvProfilePic());
    tvQuestion.setText(questionsModel.getTvQuestion());
    tvTag.setText(questionsModel.getTvQuestion());
    tvTime.setText(questionsModel.getTvTime());

    return convertView;
}

}

1 个答案:

答案 0 :(得分:3)

您实际上从未将allQuestions设置为任何实例。

public QuestionsManager() {
    this.allQuestions = allQuestions;
}

应该是

 public QuestionsManager() {
    allQuestions = new ArrayList<QuestionsModel>();
}

此外,对未来有一些建议:尝试阅读堆栈跟踪。

由于您似乎在理解如何撰写问题时遇到问题,因此让每个人都知道此行存在错误也会有所帮助:

questionsAdapter = new QuestionsAdapter(this, questionsManager.getAllQuestions());

这说明了以下内容:

Incompatible types. Required: android.app.Context. Found: blah blah blah my fragment

将违规行更改为:

questionsAdapter = new QuestionsAdapter(getActivity(), questionsManager.getAllQuestions());

Fragment不是Context,而是Activity