gpt4 book ai didi

android - CustomListAdapter中的CustomListAdapter()无法应用

转载 作者:太空狗 更新时间:2023-10-29 14:05:11 26 4
gpt4 key购买 nike

我是 android 的新手,我正在尝试创建一个应用程序来根据他们的状态显示人员,在应用程序中我使用带有 fragment 的抽屉导航。

当我试图列出特定类别的人时,我遇到了错误。即

Error:(50, 57) error: incompatible types: Past cannot be converted to Activity
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

我收到错误“无法应用 CustomListAdapter 中的 CustomListAdapter()”

   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.ListView;


public class Past extends Fragment {

ListView list;
String[] itemname ={
"John",
"Mike",
"Ria",
"jack",
"leo",

};

Integer[] imgid={
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
};

public Past() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_past, container, false);

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
//here am getting an error
list=(ListView)getView().findViewById(R.id.list);
list.setAdapter(adapter);

/*list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

}
});*/
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

CustomListAdapter.java 代码如下

 import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub

this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}

public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;

};
}

从 MainActivity 调用的 fragment 如下所示

     else if(i==1){
switch (j){

case 0:
Past past = new Past();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde,past);
fragmentTransaction.commit();
break;

case 1:
Current current = new Current();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde, current);
fragmentTransaction.commit();
break;

case 2:
Area area = new Area();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde, area);
fragmentTransaction.commit();
break;

}

最佳答案

除了评论中指出的问题外,您在设置适配器之前从 onCreateView() 方法返回,因此无法访问代码问题。

设置对膨胀 View 的引用,使用该 View 找到 ListView,然后在方法结束时返回 View :

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_past, container, false); //modified

CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid);

list=(ListView) rootView.findViewById(R.id.list); //modified
list.setAdapter(adapter);

return rootView; //added
}

关于android - CustomListAdapter中的CustomListAdapter()无法应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985811/

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