Paramaters with the same value - android - android

I have a listview that shows this paramaters. But 2 of the parameters returns the same value? How can I differentiate this two? Its email and voucher.
VoucherObj obj = new VoucherObj();
obj.customerID=item[0];
obj.type=item[1];
obj.name=item[2];
obj.searchStr=item[3]; <---- SAME PARAMETER
obj.searchStr=item[4]; <---- SAME PARAMETER
obj.branch=item[5];
obj.issued=item[6];
obj.expiration=item[7];
obj.status=item[8];
obj.vouchername=item[9];
obj.employeeid=item[10];
items.add(obj);
Voucher.class
public class VoucherObj {
public String customerID="";
public String type="";
public String name="";
public String email="";
public String voucher="";
public String branch="";
public String issued = "";
public String expiration="";
public String status = "";
public String vouchername = "";
public String employeeid = "";
public String searchStr;
}

If you'll have a static amount of items, you can declare the searchStr field of your VoucherObj as a static array with a fixed size. Assuming you're storing Strings, this would be:
String[] searchStr = new String[10];
If the number of items of that field is unknown, just use a more advanced data structure, for instance:
ArrayList<String> searchStr = new ArrayList<String>();
Afterwards you can use this method to add a value:
obj.searchStr.add("value1");
obj.searchStr.add("value2");
...

public class VoucherObj(){
int customerId;
..............
..............
ArrayList<String> searchStr;
..............
/*other parameters here*/
}
and in your main class use
obj.searchStr.add(item[3]);
obj.searchStr.add(item[4]);

Related

Android Volley Array in JSON

I have this kind of JSON response
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
And I am able to parse country, country_id, and currency without a problem, problem starts with the product list when I am trying to parse it! below the code
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<Tarif> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<Tarif>>(){}.getType());
new DtoneTarifs(country, country_id, currency, tarifs);
}
}
And here is my Tarif and Other Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<Tarif> tarifList;
public Tarifs (String country, int country_id, String currency, List<Tarif> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
I want to fill the product_list in Tarif class where only one parameter accept and show them in recycler_view
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
You can see that product_list is JSON Array of string values. But you are converting it into list of Tarif type. It should be converted into list of string type.
Either set values of Tarif as custom objects to JSON Array or change your list type to string.
It should be like this:
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<String> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<String>>(){}.getType());
Tarifs result = new Tarifs(country, country_id, currency, tarifs);
}
}
Tarifs Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<String> tarifList;
public Tarifs (String country, int country_id, String currency, List<String> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
Here you go!

Sub string cannot be resolved Android

I am getting array list from Global class and getting value with index , but i am getting error on sub-string is that (Sub-string cannot be resolved).
Glide.with(ChooseCategoryProductsActivity.this)
.load("file:///" + GlobalClass.IMAGES_PATH + "/" + GlobalClass.categoriesAr.get(GlobalClass.currentIndex)
.substring(categoriesAr.get(GlobalClass.currentIndex)
.lastIndexOf("/") + 1))
.placeholder(R.drawable.stub)
.into(categoryImage);
Category class :
public class Category {
public String catId = "";
public String catName = "";
public String catImage = "";
public String catDesc = "";
public String displayOrder = "";
public String createdDate = "";
public ArrayList<Product> productsAr = new ArrayList<Product>();
public Category(String catId, String catName, String catImage, String catDesc, String displayOrder, String createdDate) {
this.catId = catId;
this.catName = catName;
this.catImage = catImage;
this.catDesc = catDesc;
this.displayOrder = displayOrder;
this.createdDate = createdDate;
}
}
As you Declared
ArrayList<Category> categoriesAr = new ArrayList<Category>();
subString() is only applied on String not on custom object
There is no such method called subString() with glide as far as i know and hence the compile time error Sub string cannot be resolved. subString works with String
So finally i did just like following, but i don't know whether is it right or not :
GlobalClass.categoriesAr.get(GlobalClass.currentIndex).toString().substring(categoriesAr.get(GlobalClass.currentIndex)

Unable to retrieve value from other class method

i have a foll. method in class A
public String getContactNameFromNumber(String number) {
// define the columns I want the query to return
String[] projection = new String[]{
Phones.DISPLAY_NAME,
Phones.NUMBER};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(fname));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndex(Phones.DISPLAY_NAME));
return name;
}
// return the original number if no match was found
return number;
}
i want to retrive value from the variable fname and insert the value in listview of other class B
You can get the value from this method in any public static variable
And then in class B you can write ClassA.name_of_static_variable;
An example with static reference as a bridge communication between classes
//ClassA
public class ClassA {
public static String fName = "";
public void getContactNameFromNumber(String number) {
//calucate fName value
}
}
//ClassB
public class ClassB {
public static String fName = "";
private void fromSomeMethod() {
String fName = ClassA.fName;
}
}
Suppose you have two classes ClassA and ClassB.
Any method in ClassA that return value so you can get that value in public static variable.
public static String _varName = null; // get you return value in this variable.
And in ClassB you can just write the name of ClassA._varName;
then you can get the value of ClassA in ClassB.

Get String Array from Cursor

I have a SQLite Database with 45 different entries, each with:
public static final String TABLE = "Table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_HOUR = "hour";
public static final String COLUMN_WEEK = "week";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COLOUR = "colour";
public static final String COLUMN_ROOM = "room";
now I want to read out all. I Do this with following:
public Cursor fetchAllSubject(){
Cursor mCursor = database.query(true, TABLE, new String[] {
COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM},null
, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
In a other class I have this code to read all out:
dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();
Now I want to have for each entry an array with ID, Hour, week, ... but I have no idea how to do that.
My first try was following:
ArrayList<String> mo1h = new ArrayList<String>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR)));
mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)));
subjectList.moveToNext();
}
But everything is in mo1h, and I dont know how to devide it.
The best would be to have a String[] for each. Has anybody an Idea?
Thanks!
You can create on Bean class and then create one ArrayList (Collection class)
public class Bean
{
public Bean();
String id, hour, week, day, name, description, color, room;
}
now create list of Bean
ArrayList<Bean> mo1h = new ArrayList<Bean>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
Bean b = new Bean();
b.id = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
b.hour =subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
...
...
// all your column
mo1h.add(b);
}
Why not continue with your strategy, but instead use an ArrayList of String[]:
ArrayList<String[]> mo1h = new ArrayList<String[]>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
String[] toUse = new String[8];
toUse[0] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
toUse[1] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
toUse[2] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK));
toUse[3] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY));
toUse[4] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME));
toUse[5] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION));
toUse[6] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR));
toUse[7] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM));
mo1h.add(toUse);
subjectList.moveToNext();
}

Android : Sqlite Database insert?

How to insert a string array values to a table?
1,1,null,null,9876543210,null,1,-1,3,null,null,Testing message,null,0,0,0
The above is the value in a String[]. I want to insert this to my database table.
Here's my code : -
while((st = reader.readLine()) != null)
{
try
{
splitArray = st.split("\\n+");
}catch(PatternSyntaxException e ) { }
for(String result_array : splitArray)
{
Toast.makeText(context, result_array, Toast.LENGTH_SHORT).show();
}
What's the SQL statement to do this?
i have been trying to do the same thing and just worked it out. you
want to do something similar to this.
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
Code
public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
ContentValues values = new ContentValues();
values.put(SMSHelper.ADDRESS, "+61408219690");
values.put(SMSHelper.DATE, "1237080365055");
values.put(SMSHelper.READ, 1);
values.put(SMSHelper.STATUS, -1);
values.put(SMSHelper.TYPE, 2);
values.put(SMSHelper.BODY, "SMS inserting test");
Uri inserted = getContentResolver().insert(Uri.parse("content:// sms"), values);
If you have a table with one column of type 'TEXT' and all you wish is to enter
those string but in one commit: then as far as I know you have to make an INSERT statement
for each of them, there is no way to insert all the string in only 1 INSERT statement (as long as you want each string to be a row in the table)
Read more

Categories

Resources