我想创建一个由我自己的布局和数据填充的ListView。所以我查看了示例,发现我需要扩展ArrayAdapter<&gt ;. 所以我制作了一个CustomAdapter并通过一个例子做了所有事情。
代码中没有错误,但ListView @ id / dashboard没有出现在我的MainActivity中。 我可以从activity_main.xml看到TextView,但不能看到ListView。 DrawerLayout的滑动菜单也正常工作。
我在CustomAdapter中扩展了overview.xml。
你能给我一个提示,说明它为什么不起作用?我正在努力工作几个小时。
感谢的
CustomAdapter
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;
}
}
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>
我设置适配器的MainActity
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");
}
}
答案 0 :(得分:2)
变化
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
到
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;
}
在arrayAdapter类中超级构造函数句柄大小的列表,因为你没有将你的数据传递给默认设置为0,所以getView根本没有被调用