gpt4 book ai didi

java - 安卓开发: Custom ListAdapter in Fragment

转载 作者:行者123 更新时间:2023-12-01 14:35:51 25 4
gpt4 key购买 nike

我得到了以下 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;
}
}

以及我的 ListView 的适配器

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 View在我的 fragment 和 Custom adapter但如果我想在我的 fragment ListView 中使用自定义适配器则不然

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

最佳答案

将 FragmentActivity 上下文发送到适配器而不是 Fragment 本身!

尝试:

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

关于java - 安卓开发: Custom ListAdapter in Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16495187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com