I am trying out this code i have that uses Parse.com to get restaurant names and put the in a listView.
When you click on one of the names then it takes you to a details page where you can get detail information about the restaurant.
Problem is when i click on the name on the ListView it goes to the details pages but displays nothing.
I am new to android and parse and don't fully understand how it should work.
here is the code in my mainActivity
public class MainActivity extends Activity {
private CustomAdapter urgentTodosAdapter;
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView listView;
List<ParseObject> Name =new ArrayList<ParseObject>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
mainAdapter = new ParseQueryAdapter<ParseObject>(this,"RestaurantDetail");
urgentTodosAdapter = new CustomAdapter(this);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
// Binds the Adapter to the ListView
listView.setAdapter(urgentTodosAdapter);
// Capture button clicks on ListView items
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Send single item click data to SingleItemView Class
Intent random1 = new Intent(MainActivity.this, SingleItemView.class);
if (Name.size() > i)
// Pass data "name" followed by the position
random1.putExtra("Name", Name.get(i).getString("Name"));
// Open SingleItemView.java Activity
startActivity(random1);
}
});
}
}
and my customAdapter class
public class CustomAdapter extends ParseQueryAdapter<ParseObject> {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("RestaurantDetail");
/* query.whereEqualTo("Name", true);*/
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.activity_main, null);
}
super.getItemView(object, v, parent);
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("Logo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView3);
titleTextView.setText(object.getString("Name"));
return v;
}
}
and the class that suppose to display the name of the restaurant etc.
#ParseClassName("RestaurantDetail")
public class SingleItemView extends Activity {
// Declare Variables
protected TextView txtname;
String Name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from SingleItemView.xml
setContentView(R.layout.single_item_view);
txtname = (TextView) findViewById(R.id.name);
// Retrieve data from MainActivity on item click event
Intent random1 = getIntent();
// Get the name
Name = random1.getStringExtra("Name");
// Locate the TextView in singleitemview.xml
// Load the text into the TextView
txtname.setText(Name);
}
}
Please see my comment in the OP's comment section to get some context:
Answer:
In your MainActivity you didn't create a query to add to the adapter. Take a look below and adjust the ParseQuery to your needs:
MainActivity Class
public class MainActivity extends Activity {
private CustomAdapter urgentTodosAdapter;
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView listView;
List<ParseObject> Name =new ArrayList<ParseObject>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.listView1);
//I'm not sure how you initialized your custom adapter - i'll show you how i did it, but yours might work too
urgentTodosAdapter = new urgentTodosAdapter(MainActivity.this, new ArrayList<RestaurantDetail>());
listView.setAdapter(urgentTodosAdapter);
//Here is where you forgot to add your ParseQuery for 'Restaurants'
ParseQuery<RestaurantDetail> query = ParseQuery.getQuery(RestaurantDetail.class);
query.findInBackground(new FindCallback<RestaurantDetail>() {
public void done(List<RestaurantDetail> resutarantList, ParseException e) {
if (e == null) {
if (resutarantList.size() > 0) {
for (int i = 0; i < resutarantList.size(); i++) {
//you forgot to do this - didn't add anything for the adapter to show.
urgentTodosAdapter.add(resutarantList.get(i));
}
} else {
// do something if empty
}
} else {
// do something if null
}
}
});
//This is fine here, but you can also do the same in your adapter class; I'll keep this here and show you where to put it
//in the adapter as well.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Send single item click data to SingleItemView Class
Intent random1 = new Intent(MainActivity.this, SingleItemView.class);
if (Name.size() > i)
// Pass data "name" followed by the position
random1.putExtra("Name", Name.get(i).getString("Name"));
// Open SingleItemView.java Activity
startActivity(random1);
}
});
}
}
CustomAdapter class:
public class CustomAdapter extends ArrayAdapter<RestaurantDetail> {
private final Context mContext;
private List<RestaurantDetail> restrauants;
public CustomAdapter(Context context, List<RestaurantDetail> objects) {
super(context, R.layout.include_restaurants, objects);
this.mContext = context;
this.restrauants = objects;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
v = mLayoutInflater.inflate(R.layout.include_restaurants, null);
}
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("Logo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView3);
titleTextView.setText(object.getString("Name"));
//this is also where you can handle to onClick stuff
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return v;
}
}
Initialize your parse class: this is also where you can put your getters/setters.
#ParseClassName("YourClassNameInYourDBHere")
public class ResuaurantDetail(your class here) extends ParseObject
{
}
Related
I want to populate a ListView (with checkbox) with an ArrayList.
The ArrayList gets data from a server and contains my Shop's class objects, and it's filled in my function onPostExecute.
I'm trying to populate listview with shop name but not populating! Any suggestions?
onPostExecute
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (processDialog.isShowing()) {
processDialog.dismiss();
}
if (success == 1) {
if (null != restulJsonArray) {
for (int i = 0; i < restulJsonArray.length(); i++) {
try {
JSONObject jsonObject = restulJsonArray.getJSONObject(i);
favouriteShopsList.add(new Shop(jsonObject.getInt("id"),jsonObject.getString("azienda"), false));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
I checked and the list is not empty.
In my onCreate function, i execute the AsyncTask to get data from server and then i try to populate my ListView with a custom adapter. This is my onCreate function:
My Activity
public class NearestShopActivity extends AppCompatActivity{
private ProgressDialog processDialog;
private JSONArray restulJsonArray;
private int success = 0;
private Context context;
private ArrayList<Shop> favouriteShopsList = new ArrayList<Shop>();
private ArrayAdapter<Shop> listAdapter;
private ListView listView;
private CheckBox checkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearest_shop);
context = NearestShopActivity.this;
new ServiceStubAsyncTask(this, this).execute();
listView = (ListView) findViewById(R.id.activityNearestShopList);
listAdapter = new ShopArrayAdapter(this, favouriteShopsList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View item, int position, long id){
Shop shop = listAdapter.getItem(position);
shop.toggleChecked();
ShopViewHolder viewHolder = (ShopViewHolder) item.getTag();
viewHolder.getCheckBox().setChecked(shop.isChecked());
}
});
final Button continueBtn = (Button) findViewById(R.id.activityNearestShopContinueBtn);
continueBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
for(int i=0; i<listAdapter.getCount(); i++){
Shop shop = listAdapter.getItem(i);
if(shop.isChecked()){
Toast.makeText(context, "Checked: " + shop.getAzienda(), Toast.LENGTH_LONG).show();
}
}
}
});
}
private class ServiceStubAsyncTask extends AsyncTask<Void, Void, Void> {...}
}
I also post my ShopArrayAdapter class:
private static class ShopArrayAdapter extends ArrayAdapter<Shop>{
private LayoutInflater inflater;
public ShopArrayAdapter(Context context, List<Shop> favouriteShopsList) {
super(context, R.layout.activity_nearest_shop_item, R.id.activityNearestShopItem, favouriteShopsList);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
Shop shop = (Shop) this.getItem(position);
CheckBox checkBox;
TextView textView;
if(convertView == null){
convertView = inflater.inflate(R.layout.activity_nearest_shop_item, null);
textView = (TextView) convertView.findViewById(R.id.activityNearestShopItem);
checkBox = (CheckBox) convertView.findViewById(R.id.activityNearestShopCheckbox);
convertView.setTag(new ShopViewHolder(textView, checkBox));
checkBox.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
Shop shop = (Shop) cb.getTag();
shop.setChecked(cb.isChecked());
}
});
}else{
ShopViewHolder viewHolder = (ShopViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
checkBox.setTag(shop);
checkBox.setChecked(shop.isChecked());
textView.setText(shop.getAzienda());
return convertView;
}
}
Your adapter doesn't know you've added new data here:
favouriteShopsList.add(new Shop(jsonObject.getInt("id"),jsonObject.getString("azienda"), false));
try calling notifydatasetchanged() after you've added new data:
listAdapter.notifyDataSetChanged()
i have done custom listview
it has 6 textview and 1 image view in each row
whati did is when press on item it just goes to my second activity
but what it want is to pass the items of the row to the second activity
i don't know how to do it or where to add it in the list or in the adapter
also what to do in the second activity
here is my first activity
public class ImageListActivity extends AppCompatActivity {
private DatabaseReference mDatabaseRef;
private List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_list);
imgList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listViewImage);
//Show progress dialog during list image loading
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait loading list image...");
progressDialog.show();
mDatabaseRef = FirebaseDatabase.getInstance().getReference(ListActivity.FB_Database_Path);
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
progressDialog.dismiss();
//Fetch image data from firebase database
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//ImageUpload class require default constructor
ImageUpload img = snapshot.getValue(ImageUpload.class);
imgList.add(img);
}
//Init adapter
adapter = new ImageListAdapter(ImageListActivity.this, R.layout.image_item, imgList);
//Set adapter for listview
lv.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
startActivityForResult(myintent, 0);
}
});
}
}
and this is adapter
public class ImageListAdapter extends ArrayAdapter<ImageUpload> {
private Activity context;
private int resource;
private List<ImageUpload> listImage;
public ImageListAdapter(#NonNull Activity context, #LayoutRes int resource, #NonNull List<ImageUpload> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
listImage = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(resource, null);
TextView tvName = (TextView) v.findViewById(R.id.tvImageName);
TextView tvModel = (TextView) v.findViewById(R.id.tvImageModel);
TextView tvBrand = (TextView) v.findViewById(R.id.tvImageBrand);
TextView tvPrice = (TextView) v.findViewById(R.id.tvImagePrice);
TextView tvyear = (TextView) v.findViewById(R.id.tvImageYear);
TextView tvDesc = (TextView) v.findViewById(R.id.tvImageDesc);
ImageView img = (ImageView) v.findViewById(R.id.imgView);
tvModel.setText(listImage.get(position).getModel());
tvName.setText(listImage.get(position).getName());
tvBrand.setText(listImage.get(position).getBrand());
tvyear.setText(listImage.get(position).getYear());
tvDesc.setText(listImage.get(position).getDesc());
tvPrice.setText(listImage.get(position).getPrice());
Glide.with(context).load(listImage.get(position).getUrl()).into(img);
return v;
}
}
What I understand, you want to transfer the entire object namely ImageUpload of the list item you clicked on. For this you can create ImageUpload model class as Parcelable and and then pass selected ImageUpload object in your intent and retrieve it back from Intent using getParcelable().
on list item click:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
myintent.putExtra("selected_image_upload", ((ImageUpload) adapter.getItem(position)));
startActivityForResult(myintent, 0);
}
});
on Buy1Activity, to get the selected ImageUpload object in onCreate():
ImageUpload imgUploadObj = getIntent().getExtras().getParcelable("selected_image_upload");
You can get hint from here to create a class Parcelable.
Try this
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageUpload img = adapter.get(position);
Intent myintent = new Intent(view.getContext(), Buy1Activity.class);
myintent.putParcelableArrayListExtra("parcel_data", img);
startActivityForResult(myintent, 0);
}
});
In your Buy1Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageUpload img = (ImageUpload) getIntent().getParcelableArrayListExtra("parcel_data");
}
Hope it help!
I have a working list view with images from the drawables folder, I have working code which takes an image and uploads it to my server etc, I have the url to fetch the image from the database and I now am stuck in how to add it into my already existing list View by automatically adding a new image from this link into the list view.
This is the 'timeline' list view which displays the pictures we already have
/**
* Method which creates the list view on screen and displays images
*/
public class Timeline extends Activity implements OnItemClickListener {
//global variables
String[] pic_names;
TypedArray profile_pics;
List<RowItem> rowItems;
ListView mylistview;
ImageView btnTakePic;
String[] uploaded_pic_name;
TypedArray pic_url;
//Overridden method to create the main layout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timeline);
//set the global variables
//rowItems is now an arraylist
rowItems = new ArrayList<RowItem>();
//pic_names is set to the resource of pic_names
pic_names = getResources().getStringArray(R.array.pic_names);
uploaded_pic_name = getResources().getStringArray(R.array.uploaded_pic_name);
pic_url = getResources().obtainTypedArray(R.array.pic_url);
//profile_pics is now set to the resource of profile_pics
profile_pics = getResources().obtainTypedArray(R.array.profile_pics);
//gets the picture and name for each resource in the for loop array
for (int i = 0; i < pic_names.length; i++) {
RowItem item = new RowItem(pic_names[i], profile_pics.getResourceId(i, -1));
//adds items from the array
rowItems.add(item);
}
RowItem uploadedItem = new RowItem(uploaded_pic_name[0], pic_url.getResourceId(0, 0));
rowItems.add(uploadedItem);
//creates a new listview
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, rowItems);
mylistview.setAdapter(adapter);
//onclick listener on this main activity
mylistview.setOnItemClickListener(this);
btnTakePic = (ImageView) findViewById(R.id.btnTakePic);
// on click listener used to give function to the button when clicked.
btnTakePic.setOnClickListener(new View.OnClickListener() {
// onClick method defines what the function is
// Intent used to communicate to start
#Override
public void onClick(View v) {
Intent i = new Intent(Timeline.this, Camera.class);
startActivity(i);
}
});
}
//overridden method to show toast message on the picture
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String pic_name = rowItems.get(position).getPic_name();
Toast.makeText(getApplicationContext(), "" + pic_name,
Toast.LENGTH_SHORT).show();
}
}
This is the custom adapter class I had current for it
/**
* TODO
*/
public class CustomAdapter extends BaseAdapter {
//Instantiates getters for variables
Context context;
List<RowItem> rowItems;
//creates setters for variables
CustomAdapter(Context context, List<RowItem> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
//Overridden method to get the size of the rows
#Override
public int getCount() {
return rowItems.size();
}
//Overridden method to get the item position from rowItems array returning the position
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
//Overridden method to get the Item id return the position
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
/**
* private view holder class
*
*/
private class ViewHolder {
ImageView profile_pic;
TextView pic_name;
}
// Overriden method to insert image and its associated xml in the listview
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Instantiating local variables
ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//If the View is null create the layout
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
//set the textview and image view to required parameters
holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
holder.profile_pic = (ImageView) convertView.findViewById(profile_pic);
convertView.setTag(holder);
//create a new viewholder and get the tag from the view
} else {
holder = (ViewHolder) convertView.getTag();
}
//getter for the position of the row
RowItem row_pos = rowItems.get(position);
//sets the position of the row
holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
holder.pic_name.setText(row_pos.getPic_name());
//return the view
return convertView;
}
}
These are the getters and setter for the images
public class RowItem {
private String pic_name;
private int profile_pic_id;
public RowItem(String pic_name, int profile_pic_id) {
this.pic_name = pic_name;
this.profile_pic_id = profile_pic_id;
}
//getter for the pic name
public String getPic_name() {
return pic_name;
}
//setter for the pic name
public void setPic_name(String pic_name) {
this.pic_name = pic_name;
}
//getter for the profile pic
public int getProfile_pic_id() {
return profile_pic_id;
}
//setter for the profile pic
public void setProfile_pic_id(int profile_pic_id) {
this.profile_pic_id = profile_pic_id;
}
}
Any help is much appreciated
Kindly show the code which you want to implement.
I have working code which takes an image and uploads it to my server
etc, I have the url to fetch the image from the database and i now
and on which event to implement(onClick, onItemClick etc...)
I will edit this later
Do this in, RecyclerView. There implementation is not difficult.. Your mistake is in viewholder... Read Recycler view and there will not any questions.
I am building an android app where it READ from data Parse.com
I am using a CustomAdapter to display data like this
public class Dining extends Activity {
public Button but12;
public void init12() {
but12 = (Button) findViewById(R.id.button12);
but12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent toy3 = new Intent(Dining.this, Dining_select.class);
startActivity(toy3);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dining);
init12();
}
}
class CustomAdapter extends ParseQueryAdapter<ParseObject> {
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>(){
public ParseQuery create() {
ParseQuery query = new ParseQuery("RestaurantDetail");
/* query.whereEqualTo("Name", true);*/
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.dining, null);
}
super.getItemView(object, v, parent);
ParseImageView todoImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile imageFile = object.getParseFile("Logo");
if (imageFile != null) {
todoImage.setParseFile(imageFile);
todoImage.loadInBackground();
}
TextView titleTextView = (TextView) v.findViewById(R.id.textView3);
titleTextView.setText(object.getString("Name"));
return v;
}
}
Then i am also using another class to call the table and display
public class Dining1 extends Dining {
private CustomAdapter urgentTodosAdapter;
private ParseQueryAdapter<ParseObject> mainAdapter;
private ListView dininglist;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dining1);
mainAdapter = new ParseQueryAdapter<ParseObject>(this, "RestaurantDetail");
urgentTodosAdapter = new CustomAdapter(this);
dininglist = (ListView) findViewById(R.id.dininglistview1);
dininglist.setAdapter(urgentTodosAdapter);
urgentTodosAdapter.loadObjects();
}
}
problem is it does not display any information and i don't understand why.
please help
In my app, I have the goal of, when a user selects a list item, that ParseObject becomes associated with that user. I am using a ListAdapter to create a CustomList.
Here is my List Activity:
package android.bignerdranch.com.mobilemidwife;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQueryAdapter;
public class MidwifeResultList extends ListActivity {
private ParseQueryAdapter<ParseObject> mainAdapter;
private CustomMidwifeAdapter midwifeListAdapter;
//calls Adapter; Adapter manages data model
//adapts it to individual entries in widget
//inflates, assigns it to rows
ArrayAdapter<String> madapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//initialize main ParseQueryAdapter
mainAdapter = new CustomMidwifeAdapter(this);
//which keys in Midwife object
mainAdapter.setTextKey("practicename");
mainAdapter.setTextKey("education");
mainAdapter.setTextKey("yearsinpractice");
mainAdapter.setTextKey("practicephilosophy");
setListAdapter(mainAdapter);
mainAdapter.loadObjects();
}
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this, SelectedMidwife.class);
intent.putExtra("practicename", "practicename");
startActivity(intent);
}
}
I have this Adapter:
public class CustomMidwifeAdapter extends ParseQueryAdapter<ParseObject> {
public CustomMidwifeAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
// Here we can configure a ParseQuery to display
// midwives
ParseQuery query = new ParseQuery("midwifefirm");
query.whereEqualTo("userType", "midwife");
return query;
}
});
}
#Override
public View getItemView(ParseObject object, View view, ViewGroup parent) {
if (view == null) {
view = View.inflate(getContext(), R.layout.activity_midwife_result_list, null);
}
//use midwifefirm as item view/list
super.getItemView(object, view, parent);
// find in layout the practice name
TextView titleTextView = (TextView) view.findViewById(R.id.practicename);
//in the midwifefirm data model, call getPracticename
titleTextView.setText(object.getString("practicename"));
// Add education view
TextView EducationView = (TextView) view.findViewById(R.id.education);
EducationView.setText(object.getString("education"));
// Add yearsexperience view
TextView ExperienceView = (TextView) view.findViewById(R.id.yearsinpractice);
ExperienceView.setText(object.getString("yearsinpractice"));
//Add practice philosophy view
TextView PracticePhilosophyView = (TextView) view.findViewById(R.id.practicephilosophy);
PracticePhilosophyView.setText(object.getString("practicephilosophy"));
return view;
}
}
Ordinarily, I would setup a for loop, loop through the listitems..not sure where to start.
Thanks for any help.
Michael
Here is how I have handled custom adapters with listviews using parse. Hopefully this helps.
//setting up the listview
mYourListView = (ListView) findViewById(R.id.yourListView);
mYourCustomAdapter = new YourCustomAdapter(yourActivity.this, new ArrayList<whateverYourParseClassIs>());
mYourListView.setAdapter(mYourCustomAdapter);
YourCustomAdapter.java
public class YourCustomAdapter extends ArrayAdapter<whateverYourClassIs> {
private final Context mContext;
private List<whateverYourClassIs> mNumbers;
private TextView mNumber1;
private TextView mNumber2;
public YourCustomAdapter(Context context, List<whateverYourClassIs> objects) {
super(context, R.layout.your_layout_for_adapter, objects);
this.mContext = context;
this.mNumbers = objects;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.your_layout_for_adapter, null);
}
final whateverYourClassIs individualRecord = mNumbers.get(position);
mNumber1 = (TextView) convertView.findViewById(R.id.number1);
mNumber2 = (TextView) convertView.findViewById(R.id.number2);
mNumber1.setText(String.valueOf(individualRecord.get("whatever you want")));
mNumber2.setText(String.valueOf(individualRecord.get("whatever you want")));
return convertView;
}
}
YourQuery
public void updateYourData() {
ParseQuery<whateverYourClassIs> query = ParseQuery.getQuery(YourParseClass.class);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<whateverYourParseclass>() {
#Override
public void done(List<whateverYourParseclass> objects, ParseException error) {
if (objects != null) {
mYourCustomAdapter.add(objects.get(0));
mYourCustomAdapter.notifyDataSetChanged();
}
}
});
}
Or a query that has a loop
public void updateYourData() {
ParseQuery<whateverYourClassIs> query = ParseQuery.getQuery(YourParseClass.class);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<whateverYourParseclass>() {
#Override
public void done(List<whateverYourParseclass> objects, ParseException error) {
if (objects != null) {
mYourCustomAdapter.clear();
for (int i = 0; i < objects.size(); i++) {
mYourCustomAdapter.add(objects.get(i));
mYourCustomAdapter.notifyDataSetChanged();
}
}
In your adapter you simply get the objects (and it's position) passed into it by your query and query specific column names you want to get information from. Let me know if this helps or if you need any clarification.