1

伙计们,我只是想从 Parse 查询中创建一个对象列表视图。所以我对android中的列表视图了解不多,但有人可以告诉我如何实现吗?或者你有可以解释我应该怎么做的教程吗?

非常感谢你!

4

2 回答 2

1

ListView是一个显示可滚动项目列表的视图组。列表项使用适配器自动插入到列表中,该适配器从诸如数组或数据库查询之类的源中提取内容并将每个项结果转换为放置到列表中的视图。

ListView因此,例如使用元素在 xml 文件中创建您的列表视图

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView> 

然后在你的主要活动中。使用Adapter来填写您的清单

final ListView listview = (ListView) findViewById(R.id.list);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
        "Android", "iPhone", "WindowsMobile" };

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

这会将值数组中的所有项目填充到列表中

更多示例和教程http://developer.android.com/guide/topics/ui/layout/listview.html

于 2015-02-22T15:32:25.860 回答
1

我会告诉你我是怎么做到的。首先,我不会使用query.findInBackground()y 会使用query.find()返回您想要的所有对象的列表。要使用它,query.find()您必须AsyncTask在活动中创建一个类或将您要在其中创建列表视图的片段。

    /* the arraylist of RowItemChapter is becouse im making use of a 
    * custom adapter for my listview
    */
    ArrayList<RowItemChapter> grupos;
    ParseObject course;
    // this list is the one whe are going to use to save what the query gives back
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    // this would be my custo adater
    ChaptersAdapter adapter;
    // this is my expandableListView
    ExpandableListView listView;

// RemoteDataTask AsyncTask
    class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setTitle("Looking for Chapters");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            grupos = new ArrayList<RowItemChapter>();
            try {

                // Locate the class table named "Chapters"
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Chapters");
                // look only for objects witch column is equal to the ParseObject course.
                query.whereEqualTo("Curso", course);
                // get data in an ascending list
                query.orderByAscending("position");
                // execute the query and get back a list of the data you where looking for
                ob = query.find();
                // make a for inside the list and set the data to the adapter
                for (ParseObject chapter : ob) {
                    // Locate images in flag column
                    RowItemChapter item = new RowItemChapter((String) chapter.get("name"), new SubrowItemChapter(10 , 5, 80), 80);
                    grupos.add(item);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in the XML 
            listView = (ExpandableListView) getActivity().findViewById(R.id.listaChapters);
            // Pass the results into ListViewAdapter.java
            adapter = new ChaptersAdapter(getActivity(), grupos);
            // Binds the Adapter to the ListView
            listView.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

您不应该忘记在 onCreate 中执行 AsyncTask 类

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new RemoteDataTask().execute();

    }

你可能不需要我所做的,它是我所做的项目的摘录,如果你不使用任何自定义适配器会更容易。希望能帮助到你。

于 2015-03-04T00:48:13.677 回答