【问题标题】:Parse Android Studio ListView解析 Android Studio 列表视图
【发布时间】:2023-03-09 13:56:01
【问题描述】:

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

非常感谢!

【问题讨论】:

  • 做一些谷歌搜索并从教程中学习
  • 你从 developer.android.com 开始

标签: android android-listview parse-platform android-parsequeryadapter


【解决方案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

【讨论】:

    【解决方案2】:

    我会告诉你我是怎么做到的。首先,我不会使用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();
    
        }
    

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

    【讨论】:

    • 你好,能不能把这个教程的链接给我或者把项目链接分享到github上?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多