0

Hi i would like to ask for assistance on how to populate my ListView i retrieve data from a remote database and add it to a matrix cursor however it only adds the final record that was retrieved therefore my list only has one record i don't know whether its my loop or how I'm adding data to the cursor please help

here is my current code

  try{
              JSONArray jArray = new JSONArray(result);

              for(i=0;i<jArray.length();i++){
                      json_data = jArray.getJSONObject(i);

                         _id = json_data.getInt("_id");
                         rec_words = json_data.getString("words");
                         rec_word = json_data.getString("word");
                         rec_word2 = json_data.getString("word2");
                         rec_description = json_data.getString("definition");

                           //   cursor.addRow(items);    
                        // System.out.println(mld.query);

                      //  items.add(_id);
                       // items.add(rec_word);
                        //items.add(rec_word2);
                        //items.add(rec_words);
                        //items.add(rec_description);
                        //System.out.println(items);
                      final String[] matrix  = { "_id","words","word", "word2","definition"};
                         cursor = new MatrixCursor(matrix);


                     //cursor.addRow(new Object[] {json_data.getInt("_id"),json_data.getString("words"),json_data.getString("word"),json_data.getString("word2"),json_data.getString("definition")});

                       cursor.addRow(new Object[] {_id,rec_words,rec_word,rec_word2,rec_description});



                       // cursor.addRow(items);
                      Log.i("log_tag","_id: "+json_data.getInt("_id")+
                              ", word: "+json_data.getString("word")+
                              ", word2: "+json_data.getString("word2")+
                              ", definition: "+json_data.getString("definition")
                      );                    

                      //Get an output to the screen

                returnString += "\n\t" + jArray.getJSONObject(i);
                //System.out.println(returnString);

              }
      }catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
      }
4

1 回答 1

0

Use this code to loop through your cursor:

if (myCursor.moveToFirst()) {
    do {
        //get cursor data
    } while (myCursor.moveToNext());
}

EDIT: Remove the line cursor = new MatrixCursor(matrix);

于 2012-07-26T09:36:16.237 回答