简体   繁体   English

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

[英]Android Development: Custom ListAdapter in Fragment

I got the following 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;
    }
}

And that Adapter for my List View 而我的列表视图的适配器

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;
    }

}

Now the problem is, that eclipse tells me that The constructor StundenplanAdapter(ArrayList<StundenplanDetail>, Stundenplan.DummySectionFragment) is undefined but i want to use my custom adapter . 现在的问题是,日食告诉我The constructor StundenplanAdapter(ArrayList<StundenplanDetail>, Stundenplan.DummySectionFragment) is undefined但我想使用我的自定义适配器 Every thing works. 一切正常。 The List View in my Fragment and the Custom adapter but not if I want to use the custom adapter in my fragment-listview 片段和“ Custom adapterList View ,但是如果我想在片段-列表视图中使用“自定义”适配器,则不需要

I hope you can help me thanks in advance, MoBr114 希望您能提前帮助我,MoBr114

Send your FragmentActivity context to your Adapter not Fragment itself! 将您的FragmentActivity上下文发送到您的适配器,而不是Fragment本身!

Try : 尝试:

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

You are passing the wrong argument to the Adapter in msgList.setAdapter() . 您将错误的参数传递给msgList.setAdapter()的适配器。 It looks for the context of the Activity non the instance of the class DummySectionFragment . 它查找Activity的上下文,而不是DummySectionFragment类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM