我是Android开发的新手。我创建了一个列表视图,用于显示自定义格式的项目。我已经给出了我在eclipse中使用过的代码。但我在运行应用程序时收到错误,"内容具有id属性' android.R.id.list'这不是ListFragment"中的ListView类。
这是活动
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CalllogActivity extends ListFragment implements AdapterView.OnItemClickListener{
private CLAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.CallLogs, R.layout.layout_calllogs);
ListView lstDetails= (ListView) getView().findViewById(R.id.lstDtls);
adapter=new CLAdapter(this);
lstDetails.setAdapter(adapter);
lstDetails.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l) {
Toast.makeText(getActivity(), "Item " + i, Toast.LENGTH_SHORT).show();
}
class CLAdapter extends BaseAdapter{
private Context context;
String[] CallLogNames;
String[] CallNo;
int[] images;
String[] CallDateTime;
public CLAdapter(Context context){
this.context=context;
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
public CLAdapter(CalllogActivity calllogActivity) {
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
@Override
public int getCount() {
return CallLogNames.length;
}
@Override
public Object getItem(int position) {
return CallLogNames[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=null;
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_calllogs, parent, false);
}
else
{
row=convertView;
}
TextView txtName=(TextView) row.findViewById(R.id.txtName);
ImageView imgPhoto=(ImageView) row.findViewById(R.id.imgContact);
TextView txtNo = (TextView) row.findViewById(R.id.txtNo);
TextView txtDateTime = (TextView)row.findViewById(R.id.txtDateTime);
txtName.setText(CallLogNames[position]);
//txtNo.setText(CallLogNames[position]);
//txtDateTime.setText(CallLogNames[position]);
//imgPhoto.setImageResource(images[position]);
return row;
}
}
}
这是片段XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lstDtls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
这是我在列表中显示的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:src="@drawable/unknown_contact" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imgContact"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imgContact"
android:layout_alignRight="@+id/txtName"
android:layout_marginBottom="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtName"
android:layout_alignBottom="@+id/txtName"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
答案 0 :(得分:0)
尝试更改listView的ID
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
然后使用R.id.list
而不是 android.R.id.list
答案 1 :(得分:0)
如果要在布局中使用ListFragment或ListActivity列表,必须使用预定义的id:
android:id="@android:id/list"
在您的代码中,您可以使用
检索此列表ListView listview = (ListView)findViewById(android.R.id.list);
ListFragment和ListActivity对此值进行了硬编码,如果找不到要使用的列表,则无效。
答案 2 :(得分:0)
要添加此功能,值得注意的是in the ListFragment spec它提到列表视图名为&#39; list&#39;必须有兄弟android.R.id.empty
,或者不是空的。我的问题是列表视图都是空的,并没有empty
兄弟。
修复是添加一个名为empty的文本视图,
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>