gpt4 book ai didi

android - 带图标的 ListFragment

转载 作者:行者123 更新时间:2023-11-30 03:40:42 27 4
gpt4 key购买 nike

我可以设法制作具有相同图标的列表 fragment 。但似乎我无法用不同的图标制作该列表。因此列表结果对于不同的项目列表将是不同的图标。我试试看。当我尝试构建它时。它没有显示错误。但是当我运行它时。它的力量关闭。这是我的日志。

04-03 16:49:13.513: W/dalvikvm(9236): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-03 16:49:13.602: E/AndroidRuntime(9236): FATAL EXCEPTION: main
04-03 16:49:13.602: E/AndroidRuntime(9236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.surat.pendek/com.surat.pendek.utama}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.os.Looper.loop(Looper.java:137)
04-03 16:49:13.602: E/AndroidRuntime(9236): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-03 16:49:13.602: E/AndroidRuntime(9236): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list''

这是我扩展 listfragment 的示例 fragment ,这个 fragment 也包含适配器:

public class SampleListFragment extends ListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity(), null);
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
}
setListAdapter(adapter);
}

private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

private final Context context;
private final String[] values;


public SampleAdapter(Context context, String[] values) {
super(context, R.layout.row);
this.context = context;
this.values = values;
}

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);

String surat = title.getText().toString();
if (surat.equalsIgnoreCase("Huruf Hijaiyah")) {
icon.setImageResource(R.drawable.aien);
} else
if (surat.equalsIgnoreCase("About")) {
icon.setImageResource(R.drawable.alief);
} else
if (surat.equalsIgnoreCase("Surat+arti")) {
icon.setImageResource(R.drawable.ba);
} else
if (surat.equalsIgnoreCase("Surat+latin")) {
icon.setImageResource(R.drawable.emo_im_angel);
} else
{
icon.setImageResource(R.drawable.emo_im_happy);
}



return convertView;
}

}

这是我扩展 listfragment 的 fragment :

public class MenuFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] surat = getResources().getStringArray(R.array.surat_pendek);
ArrayAdapter<String> sampleAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.row, R.id.row_title, surat);
setListAdapter(sampleAdapter);
}

最佳答案

有点晚了,但我希望this answer帮助。

正如 logcat 所说:您的内容必须有一个 ListView,其 id 属性为“android.R.id.list”

您似乎有一个明确的 xml 布局文件:R.layout.list。此布局应定义一个 ID 为 android.R.id.list 的列表。

关于android - 带图标的 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15790551/

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