无法在列表中显示数据

时间:2014-08-04 15:02:40

标签: java android android-layout android-activity parse-platform

我正在尝试使用ArrayAdapter在列表上显示信息,以及从Parse.com检索信息的位置。为此,我在我的布局中创建了一个列表视图,并尝试在下面的代码中关联我的列表视图。在这样做的过程中,我遇到了一些我难以解决的错误。

特别是,我收到以下错误以下行 “方法setAdapter(ArrayAdapter)未定义类型List”

 mUsers.setAdapter(arrayAdapter);

我能够检索以下信息:

           mParseUser.getString("name");
           mParseUser.getNumber("age"); 
           mParseUser.getString("headline");

但是,我想在我的应用程序中显示它,并且最初考虑使用listview。

以下是我的活动代码     公共类MatchingActivity扩展了Activity {

    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.matching);
        // Show the Up button in the action bar.
         //create list variable
        mUsers = (List<ParseUser>) findViewById(R.id.listView1);



    }
    @Override
    protected void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();


        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.findInBackground(new FindCallback<ParseUser>() {
            public void done(List<ParseUser> users, ParseException e) {
                if (e == null) {



                  //add all the users to your list variable 
                    mUsers.addAll(users); 



                } else {
                    // Something went wrong.
                }
            }
        });

        //check the size of your list to see how big it is before accessing it
        final int size = mUsers.size(); 

       //or use a loop to loop through each one
        for(ParseUser mParseUser : mUsers)
        {
              //skip over the current user
           if(mParseUser == ParseUser.getCurrentUser())
               continue; 

           mParseUser.getString("name");
           mParseUser.getNumber("age"); 
           mParseUser.getString("headline");

           ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                   this, 
                   android.R.layout.simple_list_item_1, Unsure what to input here, 
                   as I want to return all three items (name, age, headline) from parse into the list);
           mUsers.setAdapter(arrayAdapter);
        }

    }
    }   

以下是我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

非常感谢任何帮助,提前谢谢。

更新 代码现在使用ParseQueryAdapter,但我遇到了一些错误:

“新的ParseQueryAdapter.OnQueryLoadListener(){}类型必须实现继承的抽象方法  ParseQueryAdapter.OnQueryLoadListener.onLoaded(List,Exception)“

特别是,在以下部分中发现了上述错误:

// Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
         adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
           public void onLoading() {
             // Trigger any "loading" UI
           }

以下几行

  public ParseQuery create() {
                 ParseQuery query = new ParseQuery("User");

                 query.orderByDescending("name");
                 return query;
               }
             };

我设置了“User”具有类名,而“name”具有该类的字符串值。我如何在查询列表中还包括“标题”字符串值和“年龄”数字值等其他项目。

完整的代码如下:

// Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this
 // Adapter.
 ParseQueryAdapter.QueryFactory<ParseObject> factory =
     new ParseQueryAdapter.QueryFactory<ParseObject>() {
       public ParseQuery create() {
         ParseQuery query = new ParseQuery("Customer");
         query.whereEqualTo("activated", true);
         query.orderByDescending("moneySpent");
         return query;
       }
     };

 // Pass the factory into the ParseQueryAdapter's constructor.
 ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
 adapter.setTextKey("name");

 // Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
 adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
   public void onLoading() {
     // Trigger any "loading" UI
   }

   public void onLoaded(List<ParseObject> objects, ParseException e) {
     // Execute any post-loading logic, hide "loading" UI
   }
 });

 // Attach it to your ListView, as in the example above
 ListView listView = (ListView) findViewById(R.id.listview);
 listView.setAdapter(adapter);

更新2 以下是更新的代码 我做了以下调整

1)在public void onloading之上添加了@Override 2)改变了ParseQuery query = new ParseQuery(“User”); to ParseQuery query = new ParseQuery(“User”);

由于以下消息“ ParseQuery是一种原始类型。对泛型类型ParseQuery的引用应该参数化“

以下是更新后的代码

public class MatchingActivity extends Activity {


    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.matching);
        // Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this
         // Adapter.
         ParseQueryAdapter.QueryFactory<ParseObject> factory =
             new ParseQueryAdapter.QueryFactory<ParseObject>() {
               public ParseQuery create() {
                 ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("User");

                 query.orderByDescending("name");
                 return query;
               }
             };

         // Pass the factory into the ParseQueryAdapter's constructor.
         ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
         adapter.setTextKey("name");

         // callback to be fired upon successful loading of a new set of ParseObjects.
         adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
             @Override 
             public void onLoading() {
             // Trigger any "loading" UI
           }

           public void onLoaded(List<ParseObject> objects, ParseException e) {
             // Execute any post-loading logic, hide "loading" UI
           }

        @Override
        public void onLoaded(List<ParseObject> objects, Exception e) {
            // TODO Auto-generated method stub

        }
         });

         // Attach it to your ListView, as in the example above
         ListView listView = (ListView) findViewById(R.id.listViewSingleClick);
         listView.setAdapter(adapter);


    }
        }   

更新3

public class MatchingActivity extends Activity {


    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;   
    protected List<ParseUser> mUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.matching);
        // Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this
         // Adapter.
         ParseQueryAdapter.QueryFactory<ParseObject> factory =
             new ParseQueryAdapter.QueryFactory<ParseObject>() {
               public ParseQuery create() {
                 ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("User");
                query.setLimit(5);
                 query.orderByDescending("name");
                 return query;
               }
             };

         // Pass the factory into the ParseQueryAdapter's constructor.
         ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(this, factory);
         adapter.setTextKey("name");

         // callback to be fired upon successful loading of a new set of ParseObjects.
         adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
             @Override 
             public void onLoading() {
             // Trigger any "loading" UI
           }



        @Override
        public void onLoaded(List<ParseObject> objects, Exception e) {
            // TODO Auto-generated method stub

        }
         });

         // Attach it to your ListView, as in the example above
         ListView listView = (ListView) findViewById(R.id.listViewSingleClick);
         listView.setAdapter(adapter);


    }
        }

1 个答案:

答案 0 :(得分:1)

为什么不使用ParseQueryAdapter https://www.parse.com/docs/android/api/?com/parse/ParseObject.html? 我认为这是从Parse获取数据并将其显示为列表的最佳方式。 如果要在列表中的数据和列表的可视表示之间使用一些简单的映射,只需创建新的ParseQueryAdapter并从中选择列,您将获取数据到ListView项的图像和文本字段。

   ParseQueryAdapter<ParseUser> adapter = new ParseQueryAdapter<>(this, ParseUser.class);
   adapter.setTextKey("username");
   adapter.setImageKey("userpic");
   mListView.setAdapter(adapter);

查看本教程https://www.parse.com/tutorials/mealspotting它会对您有所帮助