Reputation:
I am trying to display information on a list using an ArrayAdapter, and where the information are retrieved from Parse.com. To achieve this, I have created a listview in my layout, and have attempted to relate my listview in the code below. In doing so, I have encounter a few errors that I am having difficulty resolving.
In particular, I receive the following error for the below line "The method setAdapter(ArrayAdapter) is undefined for the type List"
mUsers.setAdapter(arrayAdapter);
I have been able to retrieve the following information:
mParseUser.getString("name");
mParseUser.getNumber("age");
mParseUser.getString("headline");
However, I want to display that in my the application, and was thinking initially of using a listview.
Below is my activity code 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);
// 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);
}
}
}
Below is my XML code
<?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>
Any help would be greatly appreciated, thanks in advance.
Update The code now uses ParseQueryAdapter, but I have encountered a few errors such has:
"The type new ParseQueryAdapter.OnQueryLoadListener(){} must implement the inherited abstract method ParseQueryAdapter.OnQueryLoadListener.onLoaded(List, Exception)"
In particular, the above error was found in the following section:
// 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
}
In the following lines
public ParseQuery create() {
ParseQuery query = new ParseQuery("User");
query.orderByDescending("name");
return query;
}
};
I have set "User" has the class name, and "name" has string value of the class. How would I also include other items like "headline" string value and "age" number value into the query list as well.
The complete code is as follow:
// 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);
Update 2 Below is the updated code I have made the following adjustments
1) added @Override on top of public void onloading 2) changed ParseQuery query = new ParseQuery("User"); to ParseQuery query = new ParseQuery("User");
due to the following message " ParseQuery is a raw type. References to generic type ParseQuery should be parameterized"
Below is the updated code
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);
}
}
Update 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);
}
}
Upvotes: 0
Views: 340
Reputation: 2430
Why don't you use ParseQueryAdapter https://www.parse.com/docs/android/api/?com/parse/ParseObject.html? I think it is the best way to fetch data from Parse and display it as a list. If you want to use some simple mappings between data in list and visual representation of a list just create new ParseQueryAdapter and select columns from that you will fetch data to image and to text field of ListView item.
ParseQueryAdapter<ParseUser> adapter = new ParseQueryAdapter<>(this, ParseUser.class);
adapter.setTextKey("username");
adapter.setImageKey("userpic");
mListView.setAdapter(adapter);
Check this tutorial https://www.parse.com/tutorials/mealspotting it will help you
Upvotes: 0