我正在尝试使用具有自定义适配器的片段在列表视图中显示元素,但它不起作用。有什么帮助吗?
listfragment.xml(它包含listview)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
activity_listview_tickets.xml(这是我的适配器自定义布局)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#F5F5F5"
android:padding="5dip" >
<TextView
android:id="@+id/icono"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:width="48dip"
android:text="A"
android:background="#8EC714"
android:gravity="center"
android:textSize="40dip" />
<TextView
android:id="@+id/descripcion"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icono"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/titulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/descripcion"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icono"
android:gravity="center_vertical"
android:text="Title"
android:textSize="16sp" />
</RelativeLayout>
Fragment_Tickets.java(我的片段类)
public class Fragment_Tickets extends ListFragment {
private ListView listView;
private CustomArrayAdapter mAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listfragment, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, values);
listView.setAdapter(mAdapter);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
CustomArrayAdapter.java(我的自定义适配器类)
public class CustomArrayAdapter extends ArrayAdapter<String> {
Context context;
String[] values;
public CustomArrayAdapter(Context context, int textViewResourceId, String[] values) {
super(context, R.layout.activity_listview_tickets);
this.context = context;
this.values = values;
}
/*private view holder class*/
private class ViewHolder {
TextView txtIcono;
TextView txtTitulo;
TextView txtDescripcion;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
String item = values[position];
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_listview_tickets, null);
holder = new ViewHolder();
holder.txtTitulo = (TextView) convertView.findViewById(R.id.titulo);
holder.txtDescripcion = (TextView) convertView.findViewById(R.id.descripcion);
holder.txtIcono = (TextView) convertView.findViewById(R.id.icono);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txtDescripcion.setText(item);
holder.txtTitulo.setText(item);
holder.txtIcono.setText("A");
return convertView;
}
}
任何人都可以帮助我并告诉我什么是问题?
答案 0 :(得分:0)
更改
super(context, R.layout.activity_listview_tickets);
到
super(context, R.layout.activity_listview_tickets,values);
答案 1 :(得分:0)
请查看CustomArrayAdapter 看看这些行
public CustomArrayAdapter(Context context, int textViewResourceId, String[] values) {
super(context, R.layout.activity_listview_tickets);
this.context = context;
this.values = values;
}
请写下如下
public CustomArrayAdapter(Context context, int textViewResourceId, String[] values) {
super(context, R.layout.activity_listview_tickets,values);
this.context = context;
this.values = values;
}