Android开发:片段中的自定义ListAdapter

时间:2013-05-11 08:19:29

标签: java android fragment

我得到了以下片段

public static class DummySectionFragment extends Fragment {

    ListView msgList;
    ArrayList<StundenplanDetail> details;
    AdapterView.AdapterContextMenuInfo info;

    public void addVertretung (String Fach, String Raum, String Farbe) {
        StundenplanDetail Detail;
        Detail = new StundenplanDetail();
        Detail.setFach(Fach);
        Detail.setRaum(Raum);
        Detail.setFarbe(Farbe);
        details.add(Detail);
    }



    /**
     * The fragment argument representing the section number for this
     * fragment.
     */

    XMLParser parser = new XMLParser();
    String xml = null;

    public static final String ARG_SECTION_NUMBER = "section_number";

    public void loadArray (String dayArray) {

    }

    public DummySectionFragment() {
    }

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

        try{
            byte[] inputBuffer = new byte[20000];
            File inputFile = new File("sdcard/Android/data/com.approfi.woehlerschule/data.woehler");
            FileInputStream fileInputStream = new FileInputStream(inputFile);
            fileInputStream.read(inputBuffer);
            xml = new String(inputBuffer);
            fileInputStream.close();
            }catch(IOException e){
                Log.d("Fehler:", e.toString());
            }


        View rootView = inflater.inflate(
                R.layout.fragment_stundenplan_dummy, container, false);


            if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("1")){
                int splitpos1 = xml.indexOf("<Montag>");
                int splitpos2 = xml.indexOf("</Montag>") + 9;

                String xml2 = xml.substring(splitpos1, splitpos2);
                org.w3c.dom.Document doc = parser.getDomElement(xml2);
                NodeList nl = doc.getElementsByTagName("Stunde");


                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element)nl.item(i);
                    String Fach = parser.getValue(e, "Fach"); // name child value
                    String Raum = parser.getValue(e, "Raum"); // cost child value
                    String Farbe = parser.getValue(e, "Farbe"); // description child value
                    Log.d("Fail:", Fach + Raum + Farbe);
                }
            }



            /*if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("2")){
                ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, dienstagArray);
                listViewStundenplan.setAdapter(arrayAdapter);
                }


            if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("3")){
                ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, mittwochArray);
                listViewStundenplan.setAdapter(arrayAdapter);
                }


            if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("4")){
                ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, donnerstagArray);
                listViewStundenplan.setAdapter(arrayAdapter);
                }


            if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("5")){
                ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, freitagArray);
                listViewStundenplan.setAdapter(arrayAdapter);
                }
            */


            msgList = (ListView) rootView.findViewById(R.id.listViewStundenplan);

            msgList.setAdapter(new StundenplanAdapter(details, this));


        return rootView;
    }
}

我的列表视图的适配器

public class StundenplanAdapter extends BaseAdapter {

private ArrayList<StundenplanDetail> _data;
Context _c;

StundenplanAdapter (ArrayList<StundenplanDetail> data, Context c){
    _data = data;
    _c = c;
}

public int getCount() {
    return _data.size();
}

public Object getItem(int position) {
    return _data.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
     View v = convertView;
     if (v == null)
     {
        LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item_stundenplan, null);
     }

       TextView fachText = (TextView)v.findViewById(R.id.textViewStundenplanFach);
       TextView raumText = (TextView)v.findViewById(R.id.textViewStundenplanRaum);
       View colorBlock = (View)v.findViewById(R.id.colorBlockStundenplan);

       StundenplanDetail msg = _data.get(position);
       fachText.setText(msg.fach);
       raumText.setText(msg.raum);
       colorBlock.setBackgroundColor(37000000);;

    return v;
    }

}

现在的问题是,eclipse告诉我The constructor StundenplanAdapter(ArrayList<StundenplanDetail>, Stundenplan.DummySectionFragment) is undefined但是我想使用我的自定义适配器。每件事都有效。我的片段中的List ViewCustom adapter但如果我想在片段列表视图中使用自定义适配器则不行

我希望你能帮助我 提前致谢, MoBr114

2 个答案:

答案 0 :(得分:1)

将FragmentActivity上下文发送到Adapter而不是Fragment本身!

尝试:

msgList.setAdapter(new StundenplanAdapter(details, getActivity()));

答案 1 :(得分:0)

您将错误的参数传递给msgList.setAdapter()中的适配器。它查找Activity非上下文DummySectionFragment实例的上下文。