我试图显示一个片段中的列的ListView(使用自定义ListViewAdapter)。
这是我得到的例外:
05-30 14:21:32.913: E/AndroidRuntime(3662): FATAL EXCEPTION: main
05-30 14:21:32.913: E/AndroidRuntime(3662): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.view.ViewGroup.addViewInner(ViewGroup.java:3509)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.view.ViewGroup.addView(ViewGroup.java:3380)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.view.ViewGroup.addView(ViewGroup.java:3325)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.view.ViewGroup.addView(ViewGroup.java:3301)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.os.Handler.handleCallback(Handler.java:730)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.os.Looper.loop(Looper.java:137)
05-30 14:21:32.913: E/AndroidRuntime(3662): at android.app.ActivityThread.main(ActivityThread.java:5103)
MyFragment.java:
public class MyFragment extends ListFragment {
private ArrayList<HashMap<String, String>> list;
public static final String FIRST_COLUMN = "Column 1";
public static final String SECOND_COLUMN = "Column 2";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setContentView(R.layout.fragment_my);
ListView lview = (ListView)getActivity().findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(getActivity(), list);
lview.setAdapter(adapter);
return lview;
}
private void populateList() {
list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> temp1 = new HashMap<String, String>();
temp.put(FIRST_COLUMN,"first column key");
temp.put(SECOND_COLUMN, "first column value");
list.add(temp);
HashMap<String, String> temp2 = new HashMap<String, String>();
// and so on...
}
}
listviewAdapter.java
public class listviewAdapter extends BaseAdapter
{
public static final String FIRST_COLUMN = "Column 1";
public static final String SECOND_COLUMN = "Column 2";
public ArrayList<HashMap<String,String>> list;
Activity activity;
public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.rows, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.column1);
holder.txtSecond = (TextView) convertView.findViewById(R.id.column2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
holder.txtFirst.setText(map.get(FIRST_COLUMN));
holder.txtSecond.setText(map.get(SECOND_COLUMN));
return convertView;
}
}
fragment_my.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/listview"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/column1"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/column2"
android:layout_width="70dip"
android:layout_height="wrap_content" />
</LinearLayout>
我究竟做错了什么?我在Android中有点菜鸟,但我猜问题出在onCreate / onCreateView方法上……有人可以帮忙吗?
最佳答案
你应该改变这个
getActivity().setContentView(R.layout.fragment_my);
至
View view = inflater.inflate(R.layout.fragment_my, container, false);
并使您的
onCreateView(...)
像 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
ListView lview = (ListView)view.findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(getActivity(), list);
lview.setAdapter(adapter);
return view;
}
并改变这个
public class MyFragment extends ListFragment
至
public class MyFragment extends Fragment
这里没有使用
ListFragment
的意思。关于java - fragment 内有两列的ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23954936/