gpt4 book ai didi

android - ArrayAdapter 的 getView() 方法没有被调用

转载 作者:IT老高 更新时间:2023-10-28 23:36:58 33 4
gpt4 key购买 nike

我对 android 开发非常陌生。我正在尝试使用标签(通过 fragment 添加)构建应用程序。在其中一个 fragment 中,我试图显示一个列表。此列表已使用我从 ArrayAdapter 扩展的 ListAdapter 填充,并且我已经重载了 getView() 方法。这是我的 fragment

public class tabcontentActivity extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container,
false);
ListView lv = (ListView) v.findViewById(R.id.listview1);
ListViewAdapter adapter = new ListViewAdapter(container.getContext(),
android.R.layout.simple_list_item_1, R.id.textview1);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);

return v;
}
}

这就是我实现 ListAdapter 的方式

public class ListViewAdapter extends ArrayAdapter {
Context context1;

public ListViewAdapter(Context context,int resource, int textViewResourceId) {
super(context,resource,textViewResourceId);
this.context1 = context;
System.out.println("here aswell!!!!!!");
// TODO Auto-generated constructor stub
}

public View getView(int arg0, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
System.out.println("@@@I AM HERE@@@");
LayoutInflater inflater = LayoutInflater.from(context1);
convertView = inflater.inflate(R.layout.tablayout, null);

TextView wv = (TextView) convertView.findViewById(R.id.textview1);
String summary = "<html><body><h1>This is happening!!!</h1></body></html>";
wv.setText(Html.fromHtml(summary));

convertView.setTag(wv);

return convertView;
}
}

布局xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>

<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="myreader" />

</LinearLayout>

请帮助我找到一种方法来完成这项工作。

最佳答案

您只是忘记向构造函数提供数据,然后在构造函数中进行正确调用:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
super(context,resource,textViewResourceId, items);
this.context1 = context;
// TODO Auto-generated constructor stub
}

关于android - ArrayAdapter 的 getView() 方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9730328/

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