Java Android开发:片段中的自定义ListAdapter
我得到了下面的片段Java Android开发:片段中的自定义ListAdapter,java,android,fragment,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
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静态类DummySectionFragment扩展了Fragment{
ListView-msgList;
ArrayList详细信息;
AdapterView.AdapterContextMenuInfo信息;
公共无效地址(字符串Fach、字符串Raum、字符串Farbe){
详细设计;
Detail=新的StundenplanDetail();
详情:塞特法赫(法赫);
细节。塞特劳姆(拉乌姆);
细节:塞特法布(法布);
详细信息。添加(详细信息);
}
/**
*表示此文件节号的片段参数
*碎片。
*/
XMLParser=新的XMLParser();
字符串xml=null;
公共静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
公共void loadArray(字符串dayArray){
}
公共数据段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
试一试{
字节[]inputBuffer=新字节[20000];
File inputFile=新文件(“sdcard/Android/data/com.approfi.woehlerschule/data.woehler”);
FileInputStream FileInputStream=新的FileInputStream(inputFile);
fileInputStream.read(inputBuffer);
xml=新字符串(inputBuffer);
fileInputStream.close();
}捕获(IOE异常){
Log.d(“Fehler:,e.toString());
}
视图根视图=充气机。充气(
R.layout.fragment_stundenplan_dummy,容器,假);
if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals(“1”)){
int splitpos1=xml.indexOf(“”);
int splitpos2=xml.indexOf(“”+9;
字符串xml2=xml.substring(splitpos1,splitpos2);
org.w3c.dom.Document doc=parser.getdoElement(xml2);
NodeList nl=doc.getElementsByTagName(“Stunde”);
对于(int i=0;i
还有我的列表视图的适配器
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;
}
}
公共类StundenplanAdapter扩展了BaseAdapter{
私有ArrayList_数据;
语境;
StundenplanAdapter(ArrayList数据,上下文c){
_数据=数据;
_c=c;
}
public int getCount(){
返回_data.size();
}
公共对象getItem(int位置){
返回_data.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null)
{
LayoutInflater vi=(LayoutInflater)\u c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.列表\项目\平面图,空);
}
TextView fachText=(TextView)v.findViewById(R.id.textViewStundenplanFach);
TextView raumText=(TextView)v.findViewById(R.id.textViewStundenplanRaum);
视图颜色块=(视图)v.findViewById(R.id.colorBlockStundenplan);
StundenplanDetail msg=_data.get(位置);
fachText.setText(msg.fach);
raumText.setText(msg.raum);
色块。setBackgroundColor(370000000);;
返回v;
}
}
现在的问题是,eclipse告诉我构造函数StundenplanAdapter(ArrayList,Stundenplan.DummySectionFragment)是未定义的,但我想使用我的自定义适配器。万事俱备。我的片段中的列表视图
和自定义适配器
,但如果我想在片段列表视图中使用自定义适配器,则不能使用
我希望你能帮助我
提前感谢,,
MoBr114将碎片活动上下文发送到适配器,而不是碎片本身 尝试:
msgList.setAdapter(new StundenplanAdapter(details, getActivity()));
您在
msgList.setAdapter()
中将错误的参数传递给适配器。它在寻找cont