for choosing favorite contact ,i load all contact inside a list view and declare my adapter like below :
ArrayAdapter<String> cnct_List_Adapter =new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_multiple_choice,info);
and for show result i use this codes :
try
{
SparseBooleanArray a = LvPopupContacts.getCheckedItemPositions();
String my_sel_items="";
for(int i = 0; i < LvPopupContacts.getCount() ; i++)
{
if (a.valueAt(i))
{
/*
Long val = lView.getAdapter().getItemId(a.keyAt(i));
Log.v("MyData", "index=" + val.toString()
+ "item value="+lView.getAdapter().getItem(i));
list.add(lView.getAdapter().getItemId((a.keyAt(i))));
*/
my_sel_items = my_sel_items + ","
+ (String) LvPopupContacts.getAdapter().getItem(i);
}
}
Toast.makeText(getActivity(),String.valueOf(a.size()), 0).show();
Log.v("Contacts :" ,my_sel_items);
} catch (Exception e) {
Log.v("e Error" ,e.toString());
}
now , when i use this in emulator it work rightly , but when it test in Device return e Exception
Exception: java.lang.ArrayindexOutOfBoundsException length=13;index=13
remember , my Device have about 220 concacts.
your bug is here:
for(int i = 0; i < LvPopupContacts.getCount() ; i++)
"a" array contains all of checked items but you loop over all LvPopupContacts so change it to
for(int i = 0; i < a.size() ; i++)
Related
I searched for my problem on StackOverflow. But This problem is not the same as them. I have two arrays. The first array has too many words. The second array has custom words that user inputs. My purpose is; I want to search for similar values but when I find the similar value, It passes to next value of the second array. How can ı do it?
Example ;
first array elements; all words of one language
second array elements ; " cat ", "dog"
result array; " category", "dogde"
Here is my code ;
for (String s: second_array
) {
for (int i = 0; i < first_array.size(); i++) {
if (first_array.get(i).toString().contains(s)){
result_array.add(first_array.get(i).toString());
}
}
}
for (int j = 0; j <result_array.size() ; j++) {
Log.i(TAG, "onCreate: " + result_array.get(j) + " "+ result_array.size());
}
if This code runs , I get too many values.
You need to break the for of first_array, but this only get the first similar word:
for (int i = 0; i < first_array.size(); i++) {
if (first_array.get(i).toString().contains(s)){
result_array.add(first_array.get(i).toString());
//Add this line
break;
}
}
EDIT: To put the "There is no such word" if not found a similar word, your code it should look like this:
for (String s: second_array) {
boolean isFound = false;
for (int i = 0; i < first_array.size(); i++) {
if (first_array.get(i).toString().contains(s)){
result_array.add(first_array.get(i).toString());
isFound = true;
break;
}
}
if(!isFound) result_array.add("There is no such word");
}
for (int j = 0; j <result_array.size() ; j++) {
Log.i(TAG, "onCreate: " + result_array.get(j) + " "+ result_array.size());
}
I have a ArrayList that has value like [Value,Sum3,121,data8input,in:21::7,7.00,9.01] and I want to extract only number as the output should be like this [3,121,8,21,7,7.00,9.01] and then have to rearrange ascending and then get the index of last two number as result will be [21,121].
My tried code below,
for (int i = 0; i < arrayString.size(); i++) {
Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?");
List<String> numbers = new ArrayList<String>();
Matcher m = p.matcher(arrayString.get(i).getvalue);
numbers.addAll(m);
for (int j = 0; j < numbers.size(); j++) {
Log.d("REMEMBERFILTER", allCollection.get(i).getTextValue());
}
}
}
do something like this, though it is not exactly memory efficient as I am using another list.
ArrayList<String> tempList = new ArrayList<>();
for (int i = 0; i < yourArrayList.size(); i++) {
tempList.add(yourArrayList.get(i).replaceAll("[^0-9]", ""));
}
//Arrange in ascending order
Collections.sort(tempList);
//Also try to remove those indexes which has only letters with
tempList.removeAll(Arrays.asList("", null));
for (int i = 0; i < tempList.size(); i++) {
Log.d("+++++++++", "" + tempList.get(i));
}
//You can get the last two or any element by get method of list by //list.size()-1 and list.size()-2 so on
This is a way to do it, finalArray has the 2 numbers you want:
String[] str = new String[] {"Value", "Sum3", "121", "data8input", "in:21::7", "7.00,9.01"};
StringBuilder longStringBuilder = new StringBuilder();
for (String s : str) {
longStringBuilder.append(s).append(" ");
}
String longString = longStringBuilder.toString();
String onlyNumbers = " " + longString.replaceAll("[^0-9.]", " ") + " ";
onlyNumbers = onlyNumbers.replaceAll(" \\. ", "").trim();
while (onlyNumbers.indexOf(" ") > 0) {
onlyNumbers = onlyNumbers.replaceAll(" ", " ");
}
String[] array = onlyNumbers.split(" ");
Double[] doubleArray = new Double[array.length];
for (int i = 0; i < array.length; i++) {
try {
doubleArray[i] = Double.parseDouble(array[i]);
} catch (NumberFormatException e) {
e.printStackTrace();
doubleArray[i] = 0.0;
}
}
Arrays.sort(doubleArray);
int numbersCount = doubleArray.length;
Double[] finalArray;
if (numbersCount >= 2) {
finalArray = new Double[]{doubleArray[numbersCount - 2], doubleArray[numbersCount - 1]};
} else if (numbersCount == 1) {
finalArray = new Double[]{ doubleArray[0]};
} else {
finalArray = new Double[]{};
}
for (Double number : finalArray) {
System.out.println(number);
}
I hope you can help me.
I make a game like 4Pics1Word.
I want to load the Level Randomly, I want a loop which generate a Random number from 0 to 10, and then check if the generated number is the first time loaded.
If yes write it in an array and end the loop.
If the number is not the first time loaded, generate a new random number and check it again until it is not used.
For example this is my code (don´t work right):
Boolean usedImageSet = false;
for (int t = 0; t <= usedImages.length; t++) {
if (usedImageSet == false) {
StringBuilder sb = new StringBuilder();
sb.append(currentQuestion);
String used = sb.toString();
if (usedImages[t] != null) {
System.out.println("usedImage" + t
+ " = not Null, it is" + usedImages[t]);
if (usedImages[t].equals(used)) {
System.out.println("String: "
+ used
+ " found it here: [" + t + "]");
currentQuestion = (int) (Math.random() * 10);
}else {
System.out.println("String: "
+ used + " not found");
}
}
if (usedImages[t] == null) {
usedImages[t] = used;
System.out.println("useddImage[" + t + "]: "
+ usedImages[t]);
System.out.println("usedImage" + t + " is Null, change to"
+ usedImages[t]);
usedImageSet = true;
}
}
}
PS:
Thank you all, I think the solution from Oren is the best
// naive implementation
ArrayList<Integer> list = new ArrayList();
for(int i=0; i<10; i++)
{
list.add(i);
}
Collections.shuffle(list);
// output the generated list
for(int i=0; i<10; i++)
{
System.out.print(list.get(i));
}
But how can I save the list if I close the game?
You would probably be much better off creating a List of the numbers you want and then calling Collections.shuffle() on that list.
// naive implementation
ArrayList<Integer> list = new ArrayList();
for(int i=0; i<10; i++)
{
list.add(i);
}
Collections.shuffle(list);
// output the generated list
for(int i=0; i<10; i++)
{
System.out.print(list.get(i));
}
int oldnumber = 5;
int newnumber = new Random().nextInt(10);
while (newnumber == oldnumber){
newnumber = new Random().nextInt(10);
}
My listview is from a layout where its id is #android:id/list. It is part of the android.R.id.list. I used a SimpleCursorAdapter to setAdapter to listview. The listview contains checkboxes with contact names such as Emily Andrews, Susan John... I wanted to know the names of the contacts which were selected by the user. When I use the code below, it prints an object android.content.ContentResolver.cursorWrapper... I am not sure how to interpret this and know which contact name was selected. Is there a way I can make the output of listview.getItemAtPosition() readable?
Code:
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).
.toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
When listview adapter is a cursor adapter then listview.getItemAtPosition() return a cursor pointed to that row. So you have to get data from that cursor like this:
Cursor cursor = listview.getItemAtPosition();
String name = cursor.getString(0);
I just assume that 0 is the index of first name column in that database which cursor is representing, It could be 2 , 3 ...
My listview is from a layout where its id is #android:id/list. It is part of the android.R.id.list. I used a SimpleCursorAdapter to setAdapter to listview. The listview contains checkboxes with contact names such as Emily Andrews, Susan John... I wanted to know the names of the contacts which were selected by the user. When using the code below, for some reason, it gives me random contact names in my unchecked list. For instance though I have Emily checked, and Matt Jones unchecked. It has both of them in the unchecked list. Also, no matter what i check and uncheck in the listview, it always gives me the first two contacts in my unchecked list and doesn't print anything for my checked list. Is there any way I can solve this?
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.valueAt(i) == true)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
checked.add(name);
}
else if(sparseBooleanArray.valueAt(i) == false)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
unchecked.add(name);
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
try this and let me know if i am wrong
public void blockCheckedItems(View view) {
ArrayList<String> checked = new ArrayList<String>();
ArrayList<String> unchecked = new ArrayList<String>();
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
Cursor cursor = (Cursor) listView.getItemAtPosition(i);
String name = cursor.getString(1);
if (sparseBooleanArray.get(i)) {
checked.add(name);
} else {
unchecked.add(name);
}
}
for (int i = 0; i < checked.size(); i++) {
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for (int i = 0; i < unchecked.size(); i++) {
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}