【问题标题】:Trying to populate ListView in Android using objects from Parse尝试使用 Parse 中的对象在 Android 中填充 ListView
【发布时间】:2012-11-26 21:57:01
【问题描述】:

我对 android 非常陌生,对 Parse 非常陌生。我创建了一个类 StudentInformation,其中包括姓名、地址、电话等列。 我想创建一个列表视图,其中包含添加到班级的所有学生的姓名。

我该怎么做?我已经到了可以吐出所有条目的 objectID 的程度,但不知道如何提取名称并将其仅添加到 ListView。

这是一个sn-p的代码:

    //Set up the listview
    studentListView = (ListView)findViewById(R.id.listViewStudents);
    //Create and populate an ArrayList of objects from parse
    ParseQuery query = new ParseQuery("StudentInformation");
    final ArrayList<Object> studentList = new ArrayList<Object>();
    query.findInBackground(new FindCallback() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Toast.makeText(getApplicationContext(), objects.toString(), Toast.LENGTH_LONG).show();
                for(int i = 0;i < objects.size(); i++){
                    objects.get(i);
                    studentList.add("name".toString());
                }
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }
    });

    //studentList.addAll(Arrays.asList(students));
    listAdapter = new ArrayAdapter<Object>(this,android.R.layout.simple_list_item_1,studentList);
    studentListView.setAdapter(listAdapter);
}

我把吐司留在了我在 public void done.... 方法中吐出 objectID 的地方。

我们将一如既往地为您提供任何帮助。

应该提到(可能),不会抛出任何错误,在 toast 消失后列表视图永远不会被填充。

不知道这是否会对任何人有所帮助,但我从下面的两个帖子中吸取了一些信息,并想出了这个:

    //Set up the listview
    studentList = new ArrayList<String>();
    //Create and populate an ArrayList of objects from parse
    listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
    studentListView = (ListView)findViewById(R.id.listViewStudents);
    studentListView.setAdapter(listAdapter);
    final ParseQuery query = new ParseQuery("StudentInformation");
    query.findInBackground(new FindCallback() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                //Toast.makeText(getApplicationContext(), objects.toString(), Toast.LENGTH_LONG).show();
                for (int i = 0; i < objects.size(); i++) {
                    Object object = objects.get(i);
                    String name = ((ParseObject) object).getString("name").toString();
                    listAdapter.add(name);
               }
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }
    });

【问题讨论】:

    标签: java android parse-platform


    【解决方案1】:

    这里有 2 个问题: 1)您的 listAdapter 包含列表中显示的数据。您需要在运行查询之前创建它,并让您的查询直接将值添加到 listAdapter。然后,您需要在完成填充后调用 listAdapter.notifyDataSetChanged(),以便使用新数据重新绘制列表。 2) 将值添加到 studentList 的函数是添加“name”.toString()。您可能想调用 studentList.add(objects.get(i));

    【讨论】:

    • 我显然很困惑......我只想看到 ListView 上学生的名字。那么在我添加对象不会给我所有对象,包括名称和所有其他属性吗?
    • 是的,如果您按照我所说的进行操作,该列表将包含从“toString()”返回的任何内容。如果您的对象是一个名为“Student”的类,并且 Student 有一个名为 name 的成员变量,那么您需要调用“Student s = (Student)objects.get(i); studentList.add(s.name);
    【解决方案2】:
    public void parseRetrieveAll(View view) throws ParseException {
     findViewById(R.id.listView1);
     ParseQuery query = new ParseQuery("TestObject");
     query.orderByDescending("foo");
     query.findInBackground(new FindCallback() {
     public void done(List<ParseObject> categories, ParseException e) {
        if (e == null) {
           for (int i = 0; i < categories.size(); i++) {
           String c = categories.get(i).getString("foo");
           foos.add(c); 
        } 
    } else {
         Toast.makeText(getApplicationContext(), "Error",
        Toast.LENGTH_LONG).show();
        }
        } 
    });
    ListView lv = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, foos);
    lv.setAdapter(arrayAdapter);
    }
    

    【讨论】:

    • 当你将值添加到列表视图的末尾时,findInBackground 的意义何在?
    【解决方案3】:

    也许这个教程会对你有所帮助。 check一次..

    同时检查How to populate listview from Json

    【讨论】:

      【解决方案4】:
      Here is What I did:
      * "foo" is the column name in my  "TestObject", which is my ParseObject
      1: Inside the For Loop I added each value to a ListArray<String>
      String c = categories.get(i).getString("foo");
      foos.add(c);
      
      2. Once Completed I Added the String Array to my ListView
      ListView lv = (ListView) findViewById(R.id.listView1);
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, foos);
      lv.setAdapter(arrayAdapter);
      3.  After that It worked.  Make sure that you declare the ArrayList<String> in your class.
      
      Example:
      ArrayList<String> foos = new ArrayList<String>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-30
        相关资源
        最近更新 更多