use*_*934 15 android android-arrayadapter android-layout android-fragments
我是android开发的新手.我正在尝试构建一个带有标签的应用程序(通过片段添加).在其中一个片段中,我试图显示一个列表.这个列表已经使用我从ArrayAdapter扩展的ListAdapter填充,并且我重载了getView()方法.这是我的片段
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我实现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;
}
}
Run Code Online (Sandbox Code Playgroud)
布局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>
Run Code Online (Sandbox Code Playgroud)
请帮助我找到一种方法来完成这项工作.
fis*_*_zh 50
您只是忘了向构造函数提供数据,然后在构造函数中进行正确的调用:
public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
super(context,resource,textViewResourceId, items);
this.context1 = context;
// TODO Auto-generated constructor stub
}
Run Code Online (Sandbox Code Playgroud)
Shu*_*ayu 19
您目前没有向其提供任何数据.如果您仍想查看列表中发生的更新,请覆盖getCount()函数.
我认为这是某种东西.
public int getCount(){
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这将在列表中绘制一个项目.并且还调用你的getView().并且,一旦您了解如何提供数据源,请将getCount()更改为您的列表大小.在这里查看教程
http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items
当我们创建自定义arrayadapter时,我们必须使用其中任何一个
public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)
public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)
Run Code Online (Sandbox Code Playgroud)
如果我们不使用上面提到的适配器,我们需要覆盖getCount()方法.
归档时间: |
|
查看次数: |
29913 次 |
最近记录: |