将自定义列表视图添加到片段

时间:2012-12-01 18:35:01

标签: android listview fragment android-listfragment custom-adapter

我有一个包含自定义列表视图的活动。

它有一个自定义数组适配器(作为另一个java文件)和一个setter和getter类(作为另一个java文件)。 我想把这个自定义列表视图放入一个片段,但我总是得到错误。 这是我以前的主要活动代码:    公共类MainActivity扩展Activity实现OnClickListener {

private ListView listView;
private List<NameBean> items;
private NamesAdapter objAdapter = null;
private Button btnGetSelected;
public static final String[] titles = new String[] { "Strawberry",
    "Banana", "Orange", "Mixed" };
String CheckedItems;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
            R.array.spinner_array, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAdapter.setNotifyOnChange(true);

    listView = (ListView) findViewById(R.id.listview);
    btnGetSelected = (Button) findViewById(R.id.btnget);
    btnGetSelected.setOnClickListener(this);

    items = new ArrayList<NameBean>();
    for (int i = 0; i < titles.length; i++) {
        NameBean nb = new NameBean();
        nb.setName(titles [i]);
        nb.setSpinnerAdapter(spinnerAdapter);
        items.add(nb);
    }
    objAdapter = new NamesAdapter(MainActivity.this, items);
    listView.setAdapter(objAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            ...
        }
    });
}

@Override
public void onClick(View v) {

    ...
        }
    }
}

}

这就是我想在Fragment中使用它的方法:

公共类内容扩展片段{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_content, container, false);

    return view;
}
public void setThings(ListAdapter adapter) {
    ListView listView = (ListView) getView().findViewById(R.id.listview);
    listView.setAdapter(adapter);
  }

}

我在另一个片段(在同一个Activity中)中定义了这些参数:

public class MyListFragment extends Fragment {

private ListView listView;
private List<NameBean> items;
private NamesAdapter objAdapter = null;
public static final String[] titles = new String[] { "Strawberry",
    "Banana", "Orange", "Mixed" };
String CheckedItems;
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_items,
            container, false);
    Button button = (Button) view.findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            updateDetail();
        }
    });
    return view;
}

// May also be triggered from the Activity
public void updateDetail() {
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
            R.array.spinner_array, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAdapter.setNotifyOnChange(true);
    items = new ArrayList<NameBean>();
    for (int i = 0; i < titles.length; i++) {
        NameBean nb = new NameBean();
        nb.setName(titles [i]);
        nb.setSpinnerAdapter(spinnerAdapter);
        items.add(nb);
    }
    objAdapter = new NamesAdapter(getActivity(), items);
    Content fragment = (Content) getFragmentManager()
            .findFragmentById(R.id.detailFragment);
    if (fragment != null && fragment.isInLayout()) {
        fragment.setThings (objAdapter);
    } 
}

}

我不知道问题是什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

我不知道这是否足够,但是:

  1. 您的Content类应该扩展ListFragment,而不仅仅是Fragment。默认情况下,ListFragment的视图层次结构包含ListView,而不包含其他内容;如果您想提供自己的布局,那很好,但该布局中的ListView必须具有ID @id/android:list。请参阅ListFragment javadoc。

  2. setThings(ListAdapter)方法中,您应该将ListAdapter传递给片段的setListAdapter()方法,而不是直接传递给ListView,如下所示:

    public void setThings(ListAdapter adapter) {
        setListAdapter(adapter);
    }
    
  3. 希望这有帮助。