ArrayList arReceipient = new ArrayList(); is declared globally. The arraylist is populated as follows
arReceipient.add(new MyItem(data.get(i).getId(),data.get(i).getNickName()));
Resulting in
sId 1000002327 sName htc1
sId 1000002208 sName htcandroid
sId 1000002208 sName htcandroid
sId 1000002242 sName htcandroid1
sId 1000000721 sName bachan
sId 1000000721 sName bachan
sId 1000000810 sName bachan2
How can i remove duplicates entries such that result is
sId 1000002327 sName htc1
sId 1000002208 sName htcandroid
sId 1000002242 sName htcandroid1
sId 1000000721 sName bachan
sId 1000000810 sName bachan2
Here is MyItem class
public class MyItem {
public String sId;
public String sName;
public MyItem(String sid, String sname){
this.sId=sid;
this.sName=sname;
}
}
instead of list use set.
LinkedHashSet<MyItem> arReceipient =new LinkedHashSet<MyItem>();
and add equals method in MyItem
public class MyItem {
public String sId;
public String sName;
public MyItem(String sid, String sname){
this.sId=sid;
this.sName=sname;
}
public boolean equals(Object o){
if(!(o instanceof MyItem)) return false;
return sId==((MyItem)o).sId;
}
}
ArrayList list = new ArrayList();
HashSet hashmap = new HashSet();
hashmap.addAll(list);
list.clear();
list.addAll(hashmap);
Related
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!
I'm trying to pass objects from a SQLite database to a fragment, but they're getting nulled somewhere along the way.
The Log.i line in DatabaseHelper outputs as expected, but the Log.i line in CollectionsFragment does not -- it returns the correct number of values, but all of them are null.
I feel like logging collectionsList in DatabaseHelper would be useful, but I'm not sure how to do that with an ArrayList.
Snippet from DatabaseHelper:
public List<Book> getAllCollections(int authorID) {
List<Book> collectionsList = new ArrayList<>();
// Select all query
String selectQuery = "SELECT DISTINCT collection FROM " + BOOKS + " WHERE author_id = '" + authorID + "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Book book = new Book();
book.setCollection(cursor.getString(0));
Log.i("stories", cursor.getString(0)); // returns with correct values
collectionsList.add(book);
} while (cursor.moveToNext());
}
return collectionsList;
// not sure how to log collectionsList here
}
Snippet from CollectionsFragment:
// link ListView object with XML ListView
collectionsListView = (ListView) view.findViewById(R.id.collections_list_view);
// create new instance of DatabaseHelper
DatabaseHelper db = new DatabaseHelper(getActivity());
// return view;
// create list of collections through getAllCollections method
List<Book> collectionsList = db.getAllCollections(authorID);
Log.i("testing", collectionsList.toString()); // returns [null, null]
// create new ArrayAdapter
ArrayAdapter<Book> arrayAdapter =
new ArrayAdapter<Book>(getActivity(), android.R.layout.simple_list_item_1, collectionsList);
// link ListView and ArrayAdapter
collectionsListView.setAdapter(arrayAdapter);
Book Class:
public class Book {
int id;
String title;
int author_id;
String collection;
String body;
#Override
public String toString() {
return title;
}
public Book() {
}
public Book(int id, String title, int author_id, String collection, String body) {
this.id = id;
this.title = title;
this.author_id = author_id;
this.collection = collection;
this.body = body;
}
// getters
public int getStoryID() {
return this.id;
}
public String getTitle() {
return this.title;
}
public int getAuthorID() {
return this.author_id;
}
public String getCollection() {
return this.collection;
}
public String getBody() {
return this.body;
}
// setters
public void setStoryID(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthorID(int author_id) {
this.author_id = author_id;
}
public void setCollection(String collection) {
this.collection = collection;
}
public void setBody(String body) {
this.body = body;
}
}
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)
I am trying to implement search on user input. The serach results will be shown after searching the relevant option from database.
I have made this method to display the results
public Cursor getBooksBySearch(String query) {
// TODO Auto-generated method stub
String[] args={query};
return(getReadableDatabase().rawQuery("SELECT _id,chapter FROM chapters WHERE chapter LIKE '%" + query + "%", args));
}
Here the query is coming from an activity SearchResultAcitvity.java
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
ListView myListView = (ListView)findViewById(R.id.txt_query);
dbBookHelper = new BooklistHelper(this);
ourCursor = dbBookHelper.getBooksBySearch(query);
startManagingCursor(ourCursor);
adapter = new BookAdapter(ourCursor);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(onListClick);
}
I want to match this coming query string to get the results from my chapter table.
Can I just match the query string m getting in String query = intent.getStringExtra(SearchManager.QUERY) with the data in my database.
Using list view to display.
Please help me in this.
Let me know if you want more information
Thanks in Advance :)
This will return list of data search by keyword
public ArrayList<ObjectType> getSearchData(String keyword)
{
ArrayList<ObjectType> objectTypeList = new ArrayList<ObjectType>();
SQLiteDatabase db = getWritableDatabase();
String checkEntry="SELECT id,firstname,lastname FROM abc_table WHERE firstname like '%"+ keyword +"%'" +" or "+ "p.lastname like '%"+ keyword +"%'";
Cursor cursor = db.rawQuery(checkEntry, null);
try
{
//If entry exist then update
if (cursor.moveToFirst())
{
do
{
ObjectType objectType = new ObjectType();
int PID=cursor.getInt(0);
String fname = cursor.getString(1);
String LastName = cursor.getString(2);
objectType.setFirstName(fname);
objectType.setLastName(LastName);
objectTypeList.add(objectType);
}
while(cursor.moveToNext());
}
}
finally
{
if(cursor != null)
cursor.close();
}
return objectTypeList;
}
ObjectType is class
public class ObjectType {
int id;
String firstName;
String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Hope this ll help you.. :)
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]);