1

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

它有一个自定义数组适配器(作为另一个 java 文件)和一个 setter 和 getter 类(作为另一个 java 文件)。我想将此自定义列表视图放入片段中,但总是出错。这是我之前的主要活动代码: public class MainActivity extends Activity implements 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);
  }

}

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

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);
    } 
}

}

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

4

1 回答 1

0

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

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

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

    public void setThings(ListAdapter adapter) {
        setListAdapter(adapter);
    }
    

希望这可以帮助。

于 2013-10-01T17:19:47.927 回答