解析 Android Studio ListView

Parse Android Studio ListView

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

非常感谢!

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

所以在 xml 文件中使用 ListView 元素创建列表视图 例如

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

然后在你的主要activity。利用 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

我将向您展示我是如何做到这一点的。首先,我不会使用 query.findInBackground() y 会使用 query.find() 返回您想要的所有对象的列表。要使用 query.find(),您必须在 activity 中创建一个 AsyncTask class 或将您要在其中创建列表视图的片段。

    /* 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 class
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new RemoteDataTask().execute();

    }

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