在片段中填充ListView

时间:2013-09-19 12:01:21

标签: android listview android-listview android-fragments listadapter

我是Android的新手,我搜索了很多答案,但没有一个能为我工作。

5月基础应用程序是android Navigation Drawer示例,其中有两个片段,一个用于菜单抽屉,另一个用于内容。 我想要做的是当我打开抽屉并点击一个项目(标题的标题)然后我可以从SQLite数据库中检索数据并在ListView中显示项目列表。

这是设置内容片段内容的方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_chapter, container, false);
    int i = getArguments().getInt(ARG_CATEGORY_NUMBER);
    String chapter = getResources().getStringArray(R.array.category)[i];

    // int imageId = getResources().getIdentifier(chapter.toLowerCase(Locale.getDefault()),
    // "drawable", getActivity().getPackageName());
    //دو خط بالا برای انتخاب عکس های مختلف بود، من غیر فعال کردم

    ((ImageView) rootView.findViewById(R.id.imageView1)).setImageResource(R.drawable.map_various);

    //اضافه شده توسط من تا کامنت بعدی

    TextView tv = (TextView) rootView.findViewById(R.id.titleText);
    tv.setText("some text");
    TextView tv2 = (TextView) rootView.findViewById(R.id.commandText);
    tv2.setText("other price text");

    ListView lv= (ListView) getActivity().findViewById(R.id.listView);
    MyListAdapter adapter = new MyListAdapter(getActivity(), lessons);
    lv.setAdapter(adapter);

    //تا اینجا توسط من اضافه شده بود


    getActivity().setTitle(chapter);
    return rootView;
}

这是我的列表适配器。

public class MyListAdapter extends ArrayAdapter<Lesson> {
    Context context;
    List<Lesson> lessons;

    public MyListAdapter(Context context, List<Lesson> lessons) {
        super(context, android.R.id.content, lessons);
        this.context = context;
        this.lessons = lessons;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View view = vi.inflate(R.layout.listitem_lesson, null);

        Lesson lesson = lessons.get(position);

        TextView tv = (TextView) view.findViewById(R.id.titleText);
        tv.setText(lesson.getTitle());

        tv = (TextView) view.findViewById(R.id.commandText);
    tv.setText(Html.fromHtml(lesson.getCommand()));

        return view;
    }
}

我的申请根本没有公开。 如何使用列表适配器在方法和活动中使用列表适配器来显示片段内的列表视图的内容?

0 个答案:

没有答案