3

私はアンドロイドにはかなり慣れていませんが、パースには非常に慣れていません。名前、住所、電話番号などの列を含むクラス StudentInformation を作成しました。クラスに追加されたすべての生徒の名前を含むリストビューを作成したいと考えています。

どうすればいいですか?すべてのエントリの objectID をトーストできるところまで来ましたが、名前だけを抽出して ListView に追加する方法がわかりません。

コードのスニペットを次に示します。

    //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 を toatst する場所にトーストを残しました。

いつものように、どんな助けでも大歓迎です。

(おそらく)言及する必要がありますが、エラーはスローされません。トーストが消えた後、リストビューにデータが入力されることはありません。

これが誰かに役立つかどうかはわかりませんが、以下の両方の投稿から少し抜粋して、これを思いつきました:

    //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();
            }
        }
    });
4

4 に答える 4

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);
}
于 2012-12-09T01:44:03.640 に答える
2

ここには 2 つの問題があります。1) listAdapter には、リストに表示されるデータが含まれています。クエリを実行する前にこれを作成し、クエリを取得して値を listAdapter に直接追加する必要があります。新しいデータを使用してリストを再描画するには、データの入力が完了したら listAdapter.notifyDataSetChanged() を呼び出す必要があります。2)studentListに値を追加する関数は、「name」.toString()を追加しています。おそらく、studentList.add(objects.get(i)); を呼び出したいと思うでしょう。

于 2012-11-27T02:41:56.093 に答える
1

このチュートリアルが役立つかもしれません。一度チェック..

また、このHow to populate listview from Jsonもチェックしてください

于 2012-11-27T03:35:42.277 に答える
1
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>();
于 2012-12-09T01:48:22.020 に答える