我尝试使用4个标签构建应用。每个选项卡都有一个链接到它的不同片段。问题是我想为每个片段创建一个自定义listView,但它以一些无法解决的错误结束......我已经与其他开发人员交谈了,但我仍然无法创建一个有效的!真的很令人沮丧!
我有:
这些是我得到的正常错误:
我正在尝试理解和使用的指南之一:
这是我的第一个片段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Fragment1test extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.fragment1, container, false);
//now you must initialize your list view
ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
ListView.setAdapter(new ListAdapter());
return view;
}
}
我的ListAdapter.java代码(来自教程):
import java.util.List;
import android.content.ClipData.Item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_design, null);
}
Item p = items.get(position);
if (p != null) {
TextView tt = (TextView) v.findViewById(R.id.game_txtTitle);
TextView tt1 = (TextView) v.findViewById(R.id.game_txtRelease);
TextView tt3 = (TextView) v.findViewById(R.id.game_txtPlatform);
if (tt != null) {
tt.setText(p.getId());
}
if (tt1 != null) {
tt1.setText(p.getCategory().getId());
}
if (tt3 != null) {
tt3.setText(p.getDescription());
}
}
答案 0 :(得分:12)
public class Fragment1test extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.fragment_basic, container, false);
//now you must initialize your list view
ListView listview =(ListView)view.findViewById(R.id.your_listview);
//EDITED Code
String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
//To have custom list view use this : you must define CustomeAdapter class
// listview.setadapter(new CustomeAdapter(getActivity()));
//getActivty is used instead of Context
return view;
}
}
参考this link&amp; question了解如何创建自定义适配器
注意:不要使用List片段或List活动来创建自定义列表视图
修改
ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter);
答案 1 :(得分:1)
public class fragmentpassword extends Fragment {
String[] name={"A","B","C","D"};
int [] image={R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer};
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layoutpassword, container, false);
ListView li=(ListView)v.findViewById(R.id.listViewPassword);
li.setAdapter(new PasswordAdapter(getActivity(),R.layout.passwordlay,name));
return v;
}
class PasswordAdapter extends ArrayAdapter {
public PasswordAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=((Activity)getContext()).getLayoutInflater().inflate(R.layout.passwordlay,null);
TextView txt1 = (TextView) v.findViewById(R.id.textViewpasslay);
txt1.setText(name[position]);
ImageView img = (ImageView) v.findViewById(R.id.imageViewpasslay);
img.setBackgroundResource(image[position]);
return v;
}
}
}
**XML for customlist: passwordlay.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewpasslay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textViewpasslay"/>
</LinearLayout>
**XML for fragment: layoutpassword.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">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listViewPassword"/>
</LinearLayout>
答案 2 :(得分:0)
片段不能使用setContentView(int)
,因此您无法使用{,1}}使用inflater
中的public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
并返回充气后的视图。至于findViewById(int)
,请执行此操作getActivity().findViewById(int)
。