how to remove java.lang.numberformatexception: invalid double: "" - android

I have a method to get all the items in the database, but I don't know what is the problem, it always say java.lang.numberformatexception: invalid double: "". Any help would be highly appreciated.
My code:
#Override
public void processFinish(String s) {
productList = new JsonConverter<Products>().toArrayList(s, Products.class);
BindDictionary dic = new BindDictionary();
dic.addStringField(R.id.tvName, new StringExtractor<Products>() {
#Override
public String getStringValue(Products item, int position) {
return item.name;
}
});
dic.addStringField(R.id.tvDesc, new StringExtractor<Products>() {
#Override
public String getStringValue(Products item, int position) {
return item.description;
}
}).visibilityIfNull(View.GONE);
dic.addStringField(R.id.tvPrice, new StringExtractor<Products>(){
#Override
public String getStringValue(Products item, int position) {
return ""+item.price;
//return String.valueOf(item.price);
}
});
dic.addDynamicImageField(R.id.ivImage, new StringExtractor<Products>() {
#Override
public String getStringValue(Products item, int position) {
return item.img_url;
}
}, new DynamicImageLoader() {
#Override
public void loadImage(String url, ImageView img) {
//Set image
ImageLoader.getInstance().displayImage(url, img);
}
});
}
Products.java
public class Products implements Serializable {
#SerializedName("itemID")
public int id;
#SerializedName("productName")
public String name;
#SerializedName("descrt")
public String description;
#SerializedName("price")
public double price;
#SerializedName("my_img")
public String img_url;
}
Thanks in advance.

This error because of your Products.java
There is a double type property in your Products.java that called price,
When you convert the json object to java object the both types must be same. In your case,
you get a
{
...
price:"",
....
}
in your json object. But the java side, it waits for double value. You could send price:0.0 instead of price:"" or write your own converter.

first see the datatype for price which you want to send.i also faced same situation long back i resolved it by passing data what is expected or before sending data parse the data to required type.
#Override
public double getStringValue(Products item, int position) {
return ""+item.price;
//return String.valueOf(item.price);
}

Related

Passing ArrayList value to a Intent.putExtra for Intent.ACTION_SEND

How to pass arraylist value (samplename, samplequote ) to the intent.putExtra.. for sharing the text..
In MainActivity.java
list = new ArrayList<>();
//loading list view item with this function
loadRecyclerViewItem();
}
private void loadRecyclerViewItem() {
list.add(new MyQuote("sample name","sample quote"));
In MyRecyclerViewAdapter.java
public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, int position) {
final MyQuote myQuote = myQuoteList.get(position);
myholder.tv_author.setText(myQuote.getAuthor());
myholder.tv_quote.setText(myQuote.getQuotedesc());
myholder.im_favlike.setImageResource(R.drawable.fav_border);
myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("text/plain");
view.getContext().startActivity(Intent.createChooser(intent,"send to"));
}
});
}
EDIT
after implementing Parcelable in MyQuote class.. like this when i use intent.putParcelableArrayListExtra("mani" , MyQuote);....
i'm getting "expression expected ...in MyRecyclerViewAdapter.java"
public class MyQuote implements Parcelable{
private String author;
private String quotedesc;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(quotedesc);
parcel.writeString(author);
}
private MyQuote(Parcel parcel){
quotedesc = parcel.readString();
author = parcel.readString();
}
public static final Parcelable.Creator<MyQuote> CREATOR = new Parcelable.Creator<MyQuote>(){
#Override
public MyQuote createFromParcel(Parcel parcel) {
return new MyQuote(parcel);
}
public MyQuote[] newArray(int size){
return new MyQuote[size];
}
};
//constructor initializing values
public MyQuote(String author, String quotedesc) {
this.quotedesc = quotedesc;
this.author = author;
}
//getters
public String getAuthor() {
return author;
}
public String getQuotedesc() {
return quotedesc;
}
}
I have a recycler cardview which contains quotes,and authors.. as textviews..and a sharebutton.. in each card.. when user want to share the quote, author of a particular card.. he can share the quote(string) along with author(string) to apps like messeges, whatsapp ..etc..
how can i solve this ? is implementing parcelable is correct process..for this purpose or not..if it is correct what code should i use in onclick of a sharebutton
#Override
public void onBindViewHolder(final MyRecycleViewAdapter.ViewHolder myholder, int position) {
final MyQuote myQuote = myQuoteList.get(position);
myholder.tv_author.setText(myQuote.getAuthor());
myholder.tv_quote.setText(myQuote.getQuotedesc());
myholder.im_favlike.setImageResource(R.drawable.fav_border);
myholder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (Intent.ACTION_SEND);
intent.putParcelableArrayListExtra("mani" , MyQuote);
intent.setType("text/plain");
view.getContext().startActivity(Intent.createChooser(intent,"send to"));
}
});
}
You have to implement Parcelable interface in MyQuote Class and then you can send it through intent like this :-
intent.putParcelableArrayListExtra(key_name,your_list);
The class of your array elements should implement Parcelable interface.
After doing so, you can use Intent.putParcelableArrayListExtra(ARRAY_LIST_KEY,fooArrayList) to send the array list and then Intent.getStringArrayListExtra(ARRAY_LIST_KEY) to retrieve the list
Hi check out this plugin Parcelable Code Generator.
It will help you to auto generate parcelable boilerplate for a POJO class and then you can directly use it as suggested by Abhishek
intent.putParcelableArrayListExtra(key_name, list);

Android Retrofit 2 issue in adapter

I have problem with adapter. I'm using retrofit 2 to parse JSON data. JSON example, models and adapter code are below.
JSON example:
[{
"id":1,
"type":"TEMPERATURE",
"measurements":[
{
"value":"22.58",
"time":"2017-01-11T12:20:44.701"
}]
},{
"id":2,
"type":"HUMIDITY",
"measurements":[
{
"value":"52.366",
"time":"2017-01-11T12:20:44.731"
}]
},{
"id":3,
"type":"LUMINOSITY",
"measurements":[
{
"value":"1.0",
"time":"2017-01-11T12:20:44.742"
}]
}]
Model Senzori:
public class Senzori {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("type")
#Expose
private String type;
#SerializedName("measurements")
#Expose
private List<Measurement> measurements = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Measurement> getMeasurements() {
return measurements;
}
public void setMeasurements(List<Measurement> measurements) {
this.measurements = measurements;
}
}
Model Measurement:
public class Measurement {
#SerializedName("value")
#Expose
private String value;
#SerializedName("time")
#Expose
private String time;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
Adapter:
public class SenzoriAdapter extends RecyclerView.Adapter<SenzoriAdapter.Holder>{
private List<Senzori> mSenzori;
public SenzoriAdapter(){
mSenzori = new ArrayList<>();
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false);
return new Holder(row);
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Senzori curr = mSenzori.get(position);
holder.senzoriType.setText(curr.getType());
holder.senzoriLastAlive.setText(curr.getMeasurements().getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().getValue());
}
#Override
public int getItemCount() {
return mSenzori.size();
}
public void addSenzori(Senzori senzori) {
mSenzori.add(senzori);
notifyDataSetChanged();
}
public class Holder extends RecyclerView.ViewHolder {
private TextView senzoriType, senzoriLastAlive, senzoriMeasurement;
public Holder(View itemView) {
super(itemView);
senzoriType = (TextView) itemView.findViewById(R.id.senzoriType);
senzoriLastAlive = (TextView) itemView.findViewById(R.id.senzoriLastAlive);
senzoriMeasurement = (TextView) itemView.findViewById(R.id.senzoriMeasurement);
}
}
}
I'm getting "Cannot resolve method" on getTime() and getValue().
Since getMeasurements() returns a List and the JSON you have shown contains only one item. Hence you should call it like this:
holder.senzoriLastAlive.setText(curr.getMeasurements().get(0).getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue());
I would suggest adding a check to see if getMeasurements() is not empty, else you will get an Exception.
The problem is you haven't given any information on what item from the List that you wish to find the value or time of.
holder.senzoriMeasurement.setText(curr.getMeasurements().get(<item you wish to get>).getValue;
If you want to get the first item in the List
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue;

android passing arguments are correct in first activity , wrong in the second

I am passing arguments from first activity to the second , in the first activity i can see all the arguments , but in the second activity one of the argument is being always 0 , the rest I can see them. I passing id , url, likes the likes argument is always null can you help indentify the issue ?
I am passing data like this , in systemoutprint I am getting value 2
Listitem listitem = new Listitem(item.getOrder(), item.getId(), item.getUrl(), item.getUserName(), item.getLikes());
Intent intent = new Intent(mcontext,SingleObjectActivity.class);
System.out.println(item.getUrl()); //i can see correct value
System.out.println(item.getLikes());//i can see correct value
intent.putExtra("Object_list", (Parcelable) listitem);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);
here is second activity the url is correct , the likes getting always 0
Listitem li = ((Listitem) getIntent().getParcelableExtra("Object_list"));
calc= li.getLikes();
id=li.getId();
System.out.println(li.getLikes());
here is the logcat
09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/System.out: http://justedhak.com/old-files/images/uploaded_images57d985894b416.png
09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/System.out: 2
09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/Timeline: Timeline: Activity_launch_request id:com.justedhak.www.i time:16396709
09-14 20:21:00.199 8874-8874/com.justedhak.www.i I/System.out: 0
here is the listitem , I though might problem with the writeToParcel
public Listitem(int order, String id, String url, String userName, int likes) {
this.id = id;
this.url = url;
this.userName = userName;
this.order = order;
this.likes = likes;
}
#Override
public int describeContents() {
return 0;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
// dest.writeString(this.name);
dest.writeString(this.url);
dest.writeInt(this.likes);
}
public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() {
public Listitem createFromParcel(Parcel in) {
return new Listitem(in);
}
public Listitem[] newArray(int size) {
return new Listitem[size];
}
};
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
Your intent code seems to be correct.
You didn't show your read from parcel code, so it might be there too.
Regardless, parcelable code is a pain in the ass to do, so I suggest you to use a library that makes it automatically. This one github.com/johncarl81/parceler all you have to do is put the #Parcel annotation and use the Parcels.wrap and Parcels.unwrap static methods
example:
intent.putExtra("Object_list", Parcels.wrap(listitem));
// then
Listitem li = (Listitem) Parcels.unwrap(getIntent().getParcelableExtra("Object_list"));

Save list of objects in onSaveInstanceState

I would like to not reload RecyclerView after screen rotation. I found that I need to store/restore List from Adapter. Isn't it ?
But there is problem:
Found java.util.List<Department> requried java.util.ArrayList<?extends android.os.Parcelable>
When I try to put list in the bundle:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("key", mAdapter.getList());
}
mAdapter.getList() return the List<Department> from the Adapter of RecyclerView.
But I had implemented Parcelable inside Department model class:
#Parcel
public class Department implements UrlInterface,Parcelable {
#SerializedName("department_id")
#Expose
String departmentId;
#SerializedName("short_name")
#Expose
String shortName;
#Expose
String url;
#Expose
HashMap<Integer, Category> categories;
public Department() {}
protected Department(android.os.Parcel in) {
departmentId = in.readString();
shortName = in.readString();
url = in.readString();
}
public static final Creator<Department> CREATOR = new Creator<Department>() {
#Override
public Department createFromParcel(android.os.Parcel in) {
return new Department(in);
}
#Override
public Department[] newArray(int size) {
return new Department[size];
}
};
public String getDepartmentId() { return departmentId; }
public void setDepartmentId(String departmentId) { this.departmentId = departmentId; }
public String getShortName() { return shortName; }
public void setShortName(String shortName) { this.shortName = shortName; }
#Override
public String getUrl() { return url; }
#Override
public String getFullName() { return shortName; }
public void setUrl(String url) { this.url = url; }
public HashMap<Integer, Category> getCategories() { return categories; }
#Override
public String toString() { return shortName; }
#Override
public String getImageUrl() { return null; }
#Override
public int describeContents() { return 0; }
#Override
public void writeToParcel(android.os.Parcel dest, int flags) {
dest.writeString(departmentId);
dest.writeString(shortName);
dest.writeString(url);
}
}
What is wrong ?
ArrayList is a subclass of List, casting from List to ArrayList....bad idea...and it will create problems, especially with parcelable stuffs.
So the quick solution is to do:
outState.putParcelableArrayList("key", new ArrayList<Department>(mAdapter.getList()));.
The above solution did not work for me and continued to throw error. The below solution, provided as hind by Android Studio worked for me-
outState.putParcelableArrayList("message_list", (ArrayList<? extends Parcelable>) messageList);
Just in case someone else also tumbles upon this thread searching for similar solution!

Display a single object in layout from JSON

I feel silly asking this, but how do I display a single object in an android layout after obtaining it's data from JSON? I know how to display data in a listview, but haven't learned how to display a single object. Do I need any kind of adapter?
Here's how I parse the data from Volley (I'm aware of libraries like Jackson and GSON):
public void parseDeal(JSONObject response) {
Deal dealItem = new Deal();
try {
dealItem.setId(response.getInt("id"));
dealItem.setHeadline(response.getString("headline"));
dealItem.setTimeStamp(response.getString("created_at"));
dealItem.setImge(response.getString("image_url"));
JSONObject mVendor = response.getJSONObject("vendor");
dealItem.setVendorPic(mVendor.getString("image_url"));
// how to adapt to the view?
} catch (JSONException e) {
e.printStackTrace();
}
}
There are certain JSON attributes in the object that may come back null, so I want to be able to handle those by changing their visibility in the view conditionally. I have done that in a list adapter before like this, but not sure if I need an adapter in this case:
if (!TextUtils.isEmpty(item.getHeadline())) {
headlineMsg.setText(item.getHeadline());
headlineMsg.setVisibility(View.VISIBLE);
} else {
// status is empty, remove from view
headlineMsg.setVisibility(View.GONE);
}
UPDATE
Here is my Deal object class.
public class Deal {
private int id;
private String headline, image, vendorPic, timeStamp;
public Deal() {
}
public Deal(int id, String image, String headline,
String vendorPic, String timeStamp) {
super();
this.id = id;
this.image = image;
this.headline = headline;
this.vendorPic = vendorPic;
this.timeStamp = timeStamp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImge() {
return image;
}
public void setImge(String image) {
this.image = image;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getVendorPic() {
return vendorPic;
}
public void setVendorPic(String vendorPic) {
this.vendorPic = vendorPic;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
}
I think i can follow you're logic here. You should work on implementing a rendering class. This is called upon the loading of the visual component that holds all this information. The renderer should turn on and off the views that are not null. The rendering class is the actual code that takes the data you have pulled out of the json, checks it to make sure there is data actually there (not null) and sets the required view to the desired value.
if (dealItem.getTimeStamp() != null && dealItem.getTimeStamp() != "") {
LayoutView.setText(dealItem.getTimeStamp()));
LayoutView.setVisibility(View.VISIBLE);
} else {
// status is empty, remove from view
LayoutView.setVisibility(View.GONE);
}
Why don't you simply create a Fragment or activity with TextViews (or whatever view you prefer...) ?
You don't need an adapter for that. Then you simply populate it on onCreate (activity) or fragment (onViewCreated) with the values from the object !
Hope it helps !

Categories

Resources