Android自定义适配器和ListView不显示任何内容
我想创建一个由我自己的布局和数据填充的列表视图。所以我看了一些例子,发现我需要扩展ArrayAdapter。 因此,我制作了一个CustomAdapter,并按照一个示例进行了所有操作 代码中没有错误,但ListView@id/dashboard没有出现在我的MainActivity中。 我可以从activity_main.xml中看到TextView,但不能看到ListView。 抽屉布局的滑动菜单也在工作 我在CustomAdapter中膨胀overview.xml 你能给我一个提示为什么它不起作用吗?我花了好几个小时在做这件事 谢谢 自定义适配器Android自定义适配器和ListView不显示任何内容,android,Android,我想创建一个由我自己的布局和数据填充的列表视图。所以我看了一些例子,发现我需要扩展ArrayAdapter。 因此,我制作了一个CustomAdapter,并按照一个示例进行了所有操作 代码中没有错误,但ListView@id/dashboard没有出现在我的MainActivity中。 我可以从activity_main.xml中看到TextView,但不能看到ListView。 抽屉布局的滑动菜单也在工作 我在CustomAdapter中膨胀overview.xml 你能给我一个提示为什么它
public class CustomAdapter extends ArrayAdapter<Ssoverview> {
private int layoutId;
private Context context;
private ArrayList<Ssoverview> list;
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
v = (RelativeLayout) ((Activity)context).getLayoutInflater().inflate(this.layoutId, null); //, parent, false);
}
Ssoverview ss = this.list.get(position);
if (ss != null) {
TextView titleTV=(TextView) v.findViewById(R.id.title);
titleTV.setText(ss.getTitle());
TextView authorTV=(TextView) v.findViewById(R.id.author);
authorTV.setText(ss.getAuthor());
}
Log.d("Deb", "Fabrication de la View : "+position);
return v;
}
}
公共类CustomAdapter扩展了ArrayAdapter{
私人内部布局;
私人语境;
私有数组列表;
公共CustomAdapter(上下文上下文、int资源、ArrayList ssList){
超级(上下文、资源);
this.layoutId=资源;
this.context=上下文;
this.list=ssList;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
v=(RelativeLayout)((活动)上下文).getLayoutInflater().inflate(this.layoutId,null);/,parent,false);
}
Ssoverview ss=this.list.get(位置);
如果(ss!=null){
TextView titleTV=(TextView)v.findViewById(R.id.title);
titleTV.setText(ss.getTitle());
TextView authorTV=(TextView)v.findViewById(R.id.author);
authorTV.setText(ss.getAuthor());
}
日志d(“Deb”,“制造视图:+位置”);
返回v;
}
}
activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ole" />
<ListView
android:id="@+id/dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
</FrameLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
overview.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/grey"
android:layout_marginTop="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre" />
<TextView
android:id="@+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:text="Note : x/5 y votes" />
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:text="John Doe" />
</RelativeLayout>
还有我设置适配器的主要功能
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.dashboard);
//Declaration factices des Ssoverview
ArrayList<Ssoverview> ssList = new ArrayList<Ssoverview>();
Ssoverview ss1 = new Ssoverview("Dune", "David", "4");
Ssoverview ss2 = new Ssoverview("Le petit poucet", "MLP", "2");
Ssoverview ss3 = new Ssoverview("Locked", "Hervé", "4,2");
ssList.add(ss1);
ssList.add(ss2);
ssList.add(ss3);
Log.d("D", ssList.get(0).getTitle());
//Declaration de l'adapter avec le layout et la ssList
CustomAdapter adapter = new CustomAdapter(this, R.layout.overview, ssList);
lv.setAdapter(adapter);
Log.d("D","Fin de onCreate");
}
}
公共类MainActivity扩展活动{
私有ListView lv;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.dashboard);
//声明事实des Ssoverview
ArrayList ssList=新的ArrayList();
Ssoverview ss1=新Ssoverview(“沙丘”、“大卫”、“4”);
Ssoverview ss2=新Ssoverview(“Le petit poucet”、“MLP”、“2”);
Ssoverview ss3=新Ssoverview(“锁定”、“Hervé”、“4,2”);
ssList.add(ss1);
ssList.add(ss2);
ssList.add(ss3);
Log.d(“d”,ssList.get(0.getTitle());
//关于适配器布局和列表的声明
CustomAdapter=新的CustomAdapter(此,R.layout.overview,ssList);
低压设置适配器(适配器);
Log.d(“d”,“Fin de onCreate”);
}
}
更改
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
公共CustomAdapter(上下文上下文、int资源、ArrayList ssList){
超级(上下文、资源);
this.layoutId=资源;
this.context=上下文;
this.list=ssList;
}
到
公共CustomAdapter(上下文上下文、int资源、ArrayList ssList){
超级(上下文、资源、ssList);那么,您在哪里填充此列表?您在哪个活动中创建适配器?首先,您也在引用哪个ListView..另外,如果它是较低的ListView,则会出现问题,因为FrameLayout取代了屏幕的所有位置抱歉,缺少详细信息,ListView是@id/dashboard,我在填充的MainActivity中编辑了我的文章此ListView您应该使用
ListActivity
而不是Activity
如果我遗漏了什么,是否有理由将您的上下文强制转换为v=(RelativeLayout)((Activity)context.)中的某个活动。getLayoutInflater().inflate(this.layoutId,null);
?这就是为什么我确保在创建适配器时使用所有3个默认构造函数的原因,它可能不是“最佳的”练习,但它可以帮助我避免这种情况,方法是确保我已根据构造函数正确设置了内容。@zgc7009如果扩展BaseAdapter类,则必须重写其他3个方法,在ArrayAdapter中,您不需要,但我始终使用BaseAdapter我也始终使用BaseAdapter,甚至没有意识到您必须使用所有3个方法有了它,每天都能学到新东西。不管怎样,默认情况下这样做会让我感觉好多了哈哈。我知道BaseAdapter需要更多的工作,但伙计,我真的很喜欢它让你控制一切的方式。它太简单了,非常感谢。我的手机上的Eclipse Logcat desync有很多问题。所以我大部分时间都不支持它e来检查方法中的具体情况。欢迎各位,很乐意提供帮助,但如果您有此问题,请使用调试模式来解决您的问题
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource , ssList); <-- change this line
this.layoutId = resource;
this.context = context;
this.list = ssList;
}