Custom ListView Displaying Last value - android

I have a custom listView with 200 values coming from Cloud DB. All the values are received properly and added in ArrayList(hotelList). When I am passing to the ArrayList(hotelList) to Custom Adapter class, showing all the values as last element. Many stackOverflow questions found here related to this I tried them too. None worked for me. How should I fix this?
The code is,
#Override
protected void onPostExecute(String result) {
try {
Log.d("Inside Post", "message");
root = new JSONObject(result);
validation = root.getInt("response");
message = root.getString("message");
JSONArray data=root.getJSONArray("data");
data.length();
if(validation==1) {
hgs=new HotelGetSetter();//Getting and setting class
for(int i=0;i<data.length();i++){
hgs.setName(data.getJSONObject(i).getString("hotel_name"));
hgs.setPlace(data.getJSONObject(i).getString("city"));
hotelList.add(hgs);//While Debugging, all the 200 values from db received and added in the ArrayList(hotelList) values also there correctly
}
//But after the loop all the values here changed to last element value(debugged).
mainMethod();
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void mainMethod(){
HotelCustomAdapter hca=new HotelCustomAdapter(HotelMaster.this,hotelList);
list_hotel.setAdapter(hca);
//list_hotel.setAdapter(new HotelCustomAdapter(this, hotel_id1,hotel_name1,hotel_place1));
}

you initialize variable hgs=new HotelGetSetter(); out of for loop and then you are always editing the same instance

You are changing the values for the same object hsg through the loop instead of creating different objects for each set of values. Therefore the 200 same objects.
Just move the hgs=new HotelGetSetter(); into the loop and it should work.

Related

Firebase recyclerAdapter duplicate objects into arrayList

final FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter(Service.class, R.layout.browse_service_detail, ServiceHolder.class, mReference){
#Override
protected void populateViewHolder(ServiceHolder serviceHolder, Service service, int position) {
serviceHolder.setServiceName(service.getName());
serviceHolder.setInfo("От " + service.getPrice1());
service.setQuantitySelected(service.getQuantityEnabled());
if (Order.getInstance().getServices() != null) {
for (Service serviceFromSingleton : Order.getInstance().getServices()) {
if (serviceFromSingleton.getName() == serviceHolder.getServiceName().getText().toString()) {
serviceHolder.getServiceName().setSelected(true);
serviceHolder.getServiceName().setTextColor(getResources().getColor(R.color.yellow));
}
}
}
//add item to array
servicesList.add(service);
}
}
};
When I run this activity, it records the visible list objects to an array, but when I scroll down and go back up, it duplicates the first elements again into the array. How to fix it? For an item to be added only once.
I don't think there is any issue in RecyclerAdapter..I think the list only inserting same data multiple times.
why not you check whether the list is empty or not before adding data into it and clear the data if its not empty and then add new.
if(servicesList.isEmpty())
servicesList.add(service);
//else clear and add data
else{
servicesList.clear();
servicesList.add(service);
}
To handle data duplicacy, you can use a Set which will ignore duplicate inserts on scrolling.
servicesList.add(service);
Set<Service> mSet= new HashSet<Service>();
mSet.addAll(servicesList);
servicesList.clear();
servicesList.addAll(mSet);
OR use Set other than ArrayList
little clumsy but will work for you.

Need a Structure to maintain Data in Android

I have a structure made from ListView - Customised as seen in Image Below
I can add New Products on Click of AddProduct Button
A new Category gets added with EditText of Product and Quantity
On that I have a (+) and (-) button which adds subcategory for Enter Dealer and Quantity with remove button on it .
The structure has been good .
But the data maintenance on it difficult . Like when I scroll , the focus gets lost and the data goes else where on ListView .
Kindly suggest some code for this .
I suggest to you to change the navigation structure.
You can keep the delete feature on the listView maybe implementing the swipe as it happens for example in GMail.
I would remove the add button and use FAB (Floating action Button) as Material guide lines suggest.
Hope it will help you!
I have implemented this thing with the following structure.
First of all I have created Model class for maintain data of Product.
I am saving data in JSONArray with collection of JSONObject of quantity added. (I have used JSON because I can save easily as a String in Preference.)
In onCreate of Activity I am creating SparseArray of Saved Products from JSONArray like:
try {
JSONArray savedProdJSONArray = new JSONArray(productSharedPref.getString("productArray", new JSONArray().toString()));
Log.i(TAG, "Saved ARRAY Count : " + productSharedPref.getInt("count", -1));
for (int i = 0; i < savedProdJSONArray.length(); i++) {
ProductItems tempPItems = new ProductItems();
tempPItems.setProdId(savedProdJSONArray.getJSONObject(i).getInt("prod_id"));
tempPItems.setProdQty(savedProdJSONArray.getJSONObject(i).getDouble("qty"));
tempPItems.setProdName(savedProdJSONArray.getJSONObject(i).getString("name"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Json Err variant:"+e.getLocalizedMessage());
}
Now in getView() of Adapter I am checking with current ProductId and Saved ProductId is matched. If it is then fill up EditText with saved data like:
/** First check whether value of Saved product Array is >0 or not..*/
ProductItems savedProdTemp = prodItemsSavedList.get(holder.prodId, null);
if(savedProdTemp != null)
{
productQtyValue = savedProdTemp.getProdQty();
holder.prodQtyView.setText(""+productQtyValue);
} else {
holder.prodQtyView.setText("");
}
Here you also have to addTextChangeListner for EditText to save data in ArrayList and then you have to Convert in JSONArray when Activity is Stop or Pause
I think it is little bit confusing but I thought it is only helps you. I tried many times to find out solution but didn't better than this.
Hope it will help you.
Welcomes you if you have any question. (Sorry If you found English Mistake :D)

Parse| can't save retrieved objects after findInBackground is done

I'm trying to retrieve String objects from Parse. I do able to get the objects back - but only inside the 'done' method. I wish to send the data i retrieve to another function\class. I tried to do this with some different public function in the same class or even to save the String in public field, but in the moment the callback is done, the data remains blank.
what can i do in order to keep the retrieved data?
this is my code:
public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
if (e == null) {
'myFieldArray' = new String[parseObjects.size()][3];
String text = "";
int index =0;
for(ParseObject po : parseObjects){
text = po.getString("Fact");
Toast.makeText(context,theFact, Toast.LENGTH_SHORT).show();
'myFieldArray'[index][0] = text;
index++;
}
}
Well,
after few tries, i realized that the Parse query probably goes inside the block twice - and ones when there are no objects returned - thats mean the array will be initialized again - and thats the reason its remains null.
the part i should have added is
if (e == null && parseObject.size()>0)

Android using JSON Objects

I'm trying to populate a ListView with various objects obtained from a RESTApi.
To test if the connection, and the objects are actually recieved, I invoke this method:
protected void onPostExecute(String result) {
try {
JSONArray users = new JSONArray(result);
JSONObject user = users.getJSONObject(1);
Toast.makeText(MainActivity.this, user.getString("name"), Toast.LENGTH_LONG).show();
jsonList = users;
} catch (JSONException e) {
e.printStackTrace();
}
}
The Toast does show me the name of the current object, but whenever I try to populate my ListView, I get a NullPointerException.
I made a TextView to test if the JSON is actually there aswell, but here I get another NullPointerException:
textView.setText(user.getString("name"));
Why does it give me a NullPointer here, when the JSONObject works fine in the Toast?
As far as your code seems
You are accessing the first element of users object
You have to specify list view's first index as well,
you are accessing 1st element and passing to the whole listview
Please follow the basic programming of Listview
http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/
Create a Bean class with getters and setters then populate the Bean with JSON data and use it as the data source to your listView adapter!

Prevent duplicate entries parse.com

I'm using Parse.com as my backend and while there seems to be a method, saveInBackgroundWithBlock, to prevent duplicate entries. It doesn't appear to exist on Android. I'd like to upload only unique entries but can't figure out a way to do so.
The only thing I can think of is to query then insert if the entry doesn't exist, but that's doing twice as many network calls and I feel like it needs to.
Thanks
As I had mentioned in the comment earlier, I had faced the same problem. Ended up writing a query to find the existing objects and then save only the non-existing ones. Like below.
//Say you have a list of ParseObjects..this list contains the existing as well as the new objects.
List<ParseObject> allObjects = new ArrayList<ParseObject>();
allObjects.add(object); //this contains the entire list of objects.
You want to find out the existing ones by using the field say ids.
//First, form a query
ParseQuery<ParseObject> query = ParseQuery.getQuery("Class");
query.whereContainedIn("ids", allIds); //allIds is the list of ids
List<ParseObject> Objects = query.find(); //get the list of the parseobjects..findInBackground(Callback) whichever is suitable
for (int i = 0; i < Objects.size(); i++)
existingIds.add(Objects.get(i).getString("ids"));
List<String> idsNotPresent = new ArrayList<String>(allIds);
idsNotPresent.removeAll(existingIds);
//Use a list of Array objects to store the non-existing objects
List<ParseObject> newObjects = new ArrayList<ParseObject>();
for (int i = 0; i < selectedFriends.size(); i++) {
if (idsNotPresent.contains(allObjects.get(i).getString(
"ids"))) {
newObjects.add(allObjects.get(i)); //new Objects will contain the list of only the ParseObjects which are new and are not existing.
}
}
//Then use saveAllInBackground to store this objects
ParseObject.saveAllInBackground(newObjects, new SaveCallback() {
#Override
public void done(ParseException e) {
// TODO Auto-generated method stub
//do something
}
});
I had also tried using beforeSave method on ParseCloud. As you may know, before saving the objects this method is called on the ParseCloud and is ideal to make any validation required. But, it didn't quite run well. Let me know if you need something from the ParseCloud code.
Hope this helps!
I'm not sure I understand your question, but you can get the same functionality as saveInBackgroundWithBlock in Android like this:
myObject.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
myObjectSavedSuccessfully();
} else {
myObjectSaveDidNotSucceed();
}
}
});

Categories

Resources