I have list of numbers like this:
-153
-542
-153
-153
-783
-785
-975
-153
-478
as you see "153" is showen 4 times but i want to show this number one time only like this:
-153
-542
-783
-785
-975
-478
EDIT:
i need to get all messages numbers but show only one if similar numbers here is my method:
public static List<SMSData> getAllNumbers(Context context){
List<SMSData> smsList = new ArrayList<SMSData>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= context.getContentResolver().query(uri, null,null,null,null);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
SMSData sms = new SMSData();
sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
sms.setDate(c.getString(c.getColumnIndexOrThrow("date")).toString());
smsList.add(sms);
c.moveToNext();
}
}
return smsList;
}
i fond the solution:
https://stackoverflow.com/a/19305534/3522182
tnx all.
ArrayList yourList = new ArrayList();
/*
add your values to the list
*/
ArrayList simpleList = new ArrayList();
HashSet hashset = new HashSet();
hashset.addAll(yourList);
simpleList.addAll(hashset);// your list now has the unique numbers
NOTE: the order here is unexpected. if you want it orderd, use linkedHashSet for keeping the same order.
Related
This is my code for getting the contact names from my device. The problem I'm having is that in my listview, "elements" will display every name in contact list. Any ideas of how I can remove the names that have no SMS?
// converts contacts from cursor to arraylist
nameList = new ArrayList<String>();
cursor = getContacts();
while(cursor.moveToNext()){
nameList.add(cursor.getString(cursor.getColumnIndex(
ContactsContract.Data.DISPLAY_NAME )));
}
// convert arraylist to string array
name = new String[nameList.size()];
name = nameList.toArray(name);
// new arraylist for after contacts with no messages are removed
elements = new ArrayList<String>();
//convert back to an arraylist
for(int i = 0; i < name.length; i++){
elements.add(name[i]);
}
I would imagine that you need to retrieve the sms list and then do comparison to each contact number to find the actual number of SMS messages per contact. Once you have that you can remove the contacts from the list who have 0 messages.
This is a good link regarding working with SMS
I'm trying to take an entire column from a table in my DB and store all the entries into a String array Below you have my code:
public Cursor fetchAllBarcodeEntries(){
return db.query(BARCODE_TABLE, new String[] {KEY_BARCODE_ID, KEY_BARCODE_NUMBER, KEY_BARCODE_PRODUCT, KEY_BARCODE_PRODUCT_DESC}, null, null, null, null, null);
}
I'm calling and using the function like this:
Cursor c = dbAdapter.fetchAllBarcodeEntries();
for(int i = 0; i < c.getCount(); i++){
stringArray[i] = c.getString(c.getColumnIndex(dbAdapter.KEY_BARCODE_PRODUCT);
}
This is part of the error log that I'm getting
01-26 23:15:02.434: E/AndroidRuntime(787): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
Any help would this would be greatly appreciated...
The answer is here
ArrayList<String> columnArray1 = new ArrayList<String>();
ArrayList<String> columnArray2 = new ArrayList<String>();
ArrayList<String> columnArray3 = new ArrayList<String>();
if(mCursor.moveToFirst()!=null){
do{
columnArray1.add(mCursor.getString(COLUM_INDEX1));
columnArray2.add(mCursor.getString(COLUM_INDEX2));
columnArray3.add(mCursor.getString(COLUM_INDEX3));
} while (mCursor.moveToNext()!=null);
}
Afterwards you can convert the ArrayList into a String array:
String[] colStrArr1 = (String[]) columnArray1.toArray(new String[columnArray1.size()]);
You should check the return value of c.getColumnIndex(). It is probably returning -1 because the given column (dbAdapter.KEY_BARCODE_PRODUCT) does not exist in that table, possibly due to an error on your database.
Also, what's Toast.LENGTH_SHORT doing there?
It seems something is missing from the code you posted...
You may try something like this below:
Cursor c = dbAdapter.fetchAllBarcodeEntries();
int columnCnt = c.getColumnCount ();
String[] stringArray = new String[columnCnt];
c.moveTofirst();
for(int i = 0; i < columnCnt ; i++){
stringArray[i] = c.getString(i);
}
Note: I don't have IDE handy, there may be syntax errors.
I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my code:
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist = new ArrayList<String>();
stringList3 = new ArrayList<ArrayList<String>>();stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
I am getting the stringList1 array values from Mypage and accessing that in new page. In the new page I am trying to add the optionlist array to stringList1 by giving stringList1.add(optionlist) but the data is not adding. Where I went wrong? Please help me regarding this... Thanks in Advance
Use addAll() It appends all of the elements in the specified ArrayList to the end of this list, in the order that they are returned by the specified Collection's Iterator.
use stringList1.addAll(optionlist); instead of stringList1.add(optionlist);
After seeing your code I can guess that you have problem that elements of arrayList is not copy in another arrayList.If this is your issue then change your code like below
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist;
stringList3 = new ArrayList<ArrayList<String>>();
stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
}
The problem seems that you are initializing your
ArrayList<String> optionlist;
only once that is outside the loop. So, in that case only the first ArrayList gets stored. So, initialized the ArrayList<String> optionlist inside the loop.
ArrayList<String> optionlist;
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
stringList1.add(optionlist);
}
I've trying to display random data from the database example in my database i have total of 20 question. i want to display randomly 10 qns every time i access the app. And for the second time that i access it, it will display the other 10 qns that has not been display before.
my code:
ArrayList<Integer> randList = new ArrayList<Integer>();
Random randGenerator = new Random();
ArrayList<String> list2 = new ArrayList<String>(db.selectData("MCQ",new String [] {"_id"}, "testId=0", null, null, null, null));
//random 10 n.o
for(int i=0;i<10;i++)
{
int randNum = randGenerator.nextInt(list2.size());
while (randList.contains(randNum))
{
randNum = randGenerator.nextInt(list2.size());
}
randList.add(randNum);
Log.d("rand no:"+i,String.valueOf(randNum));
}
ArrayList<String> idList = new ArrayList<String>();
//get the 10 _id that are chosen
for (int i=0; i<10;i++)
{
idList.add(list2.get(Integer.valueOf(randList.get(i))));
Log.d("id:"+i,String.valueOf((list2.get(Integer.valueOf(randList.get(i))))));
}
ArrayList<String> list3 = new ArrayList<String>(db.selectData("MCQ",new String [] {"Question"}, null, null, null, null, null));
Log.d("size",String.valueOf(list3.size()));
ArrayList<String> qnsList = new ArrayList<String>();
// == set question in arrangement of randList == //
for(int i=0;i<=list3.size();i++)
{
qnsList.add(list3.get(i));
Log.d("_id:"+i,String.valueOf(list3.get(i)));
}
i have tested it again and again and realized that weirdly, it could not read the data at _id=20
this is what i get in my log cat:
E/AndroidRuntime(1284): java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20
E/AndroidRuntime(1284): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
there are more, but i think it is useless as i have tried running with just _id=19 and it works but once i change to 20 it crashed.
Change this line:
for(int i=0;i<=list3.size();i++)
to be:
for(int i=0;i<list3.size();i++)
You were iterating through the loop 1 too many times (on the last iteration, when i = list3.size() you would get an IndexOutOfBoundsException when you do list3.get(i) since list indexes are 0-based so the twentieth and last element is actually at index 19).
Just keep in mind that SQLite's _id is not 0-based.
I try to convert my Cursor data to a arraylist. But at the end all the data in the arraylist is overwrited with the last row. What do i do wrong?
Cursor c = myDbHelper.getLvl1Cata();
String[] data = new String[3];
c.moveToFirst();
while(!c.isAfterLast()) {
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
c.moveToNext();
}
Try this
Cursor c = myDbHelper.getLvl1Cata();
String[] data;
if (c != null) {
while(c.moveToNext()) {
data = new String[3]; // Note this addition
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
}
c.close();
}
data is an array of strings. In the original code, you added the same array to your catalogueData structure several times. You changed the value of the array's contents each time, but it was still the same array object. So you ended up with catalogueData holding several references to a single array, and that array can only have one value for data[0]: the last thing you set it to.
This answer fixes that by using a new and different array for each row in the cursor.
Try this:
if(mycursor!=null){
do{
TextView name = (TextView)view.findViewById(R.id.contact_name);
name.setText(cursor.getString(cursor.getColumnIndex
(Displayname)));
mycursor.moveToNext();
}while (mycursor.isLast());
}
Put String[] data = new String[3]; into the while loop. You're overwriting the array object with each iteration.