I'm having an issue while i'm looking to sort my Variables to fees Menues, i mean i have an ArrayList with data as "WITH PEPERONI,1," or "CHAMPAGNE,1,2," where ,1, or ,1,2, means the menu of the variable so when i press on Menu 1 i have to see only variables that had ,1, or ,1,2, or ,1,2,3, (where there is 1) in their array.
And actually what i've done works but only with variables that has multiple menues i mean if a variable is in menu 1,2,3,4 and i press on 2 that will be visible but if the variable is just in one menu as 1 so ,1, in array that will be not visualized and i can't get why.
here is my code where i filter the variables and set them in a new Array:
public void FilterVariable() {
filteredVariable = new ArrayList<>();
for (VariantiConstructor varianti : variantiConstructors) {
String data = varianti.getMenu();
String[] items = data.split("," + positionMenu + ",");
try {
if (items[0].equals(data)) {
//
} else {
filteredVariable.add(varianti);
}
} catch (Exception e) {
//
}
}
}
While here is a screen from where i was debugging and where there was a ,2, and it skipped it insteam of adding in the ArrayList:
You need to use the following code for the shorting the ArrayList
ArrayList<String> YOUR_ARRAYLIST = new ArrayList<>();
private void searchDataFromList(String serachString) {
ArrayList<String> SEARCH_ARRAYLIST = new ArrayList<>();
for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {
if (serachString.contains(YOUR_ARRAYLIST.get(i))) {
SEARCH_ARRAYLIST.add(YOUR_ARRAYLIST.get(i));
}
}
}
On your click listener, u need to call this searchDataFromList() method as following
YOUR_CLICK.setOnClickListener(view -> {
String searchString ="WITH PEPERONI,1,";
String YOUR_SEARCH_STRING ="";
List<String> YOUR_SELECETD_LIST = Arrays.asList(searchString.split(","));
for (int i = 0; i <YOUR_SELECETD_LIST.size(); i++) {
if (YOUR_SELECETD_LIST.get(i).length()==1)
{
YOUR_SEARCH_STRING = YOUR_SELECETD_LIST.get(i);
System.out.println("VALUE IS ==>>>>> "+YOUR_SEARCH_STRING);
}
}
if (!YOUR_SEARCH_STRING.isEmpty())
{
searchDataFromList(YOUR_SEARCH_STRING);
}
Related
I get a JSON Array from the server that consists of some of the phone contacts on the phone of the person who is using my app. I want these phone numbers to be displayed to the user in a ListView as 'Already a contact'.
The JSON Array, called JsonArrayMatchingContacts, might be, for example:
[{"contact_phonenumber":"+12345678"},{"contact_phonenumber":"+23456789},
{"contact_phonenumber":"+34567890"}]
Here's my code, but it's not working. It works for an individual value - for example if I have if (phoneNumberofContact.equals("+12345678")) etc.. it comes up with Already a Contact but I need to have it working for my JSON Array. Can you help?
SelectPhoneContact selectContact = new SelectPhoneContact();
ArrayList<String> MatchingContacts = new ArrayList<String>();
try {
JSONArray Object = new JSONArray(JsonArrayMatchingContacts);
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
MatchingContacts.add(obj.getString("contact_phonenumber"));
}
} catch(Exception e) {
e.printStackTrace();
}
if (phoneNumberofContact.equals(MatchingContacts))
{
phoneNumberofContact= "Already a contact";
selectPhoneContacts.add(selectContact);
} else {
selectPhoneContacts.add(selectContact);
}
selectContact.setName(name);
selectContact.setPhone(phoneNumberofContact);
You are comparing a string ( phoneNumberofContact ) with a list ( MatchingContacts ).
You should check if the string is contained in the list.
if (MatchingContacts.contains( phoneNumberofContact )) {
put your if condition in loop..you will get it.try this
SelectPhoneContact selectContact = new SelectPhoneContact();
ArrayList<String> MatchingContacts = new ArrayList<String>();
try {
JSONArray Object = new JSONArray(JsonArrayMatchingContacts);
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
if (phoneNumberofContact.equals(MatchingContacts))
{
phoneNumberofContact= "Already a contact";
selectPhoneContacts.add(selectContact);
} else {
selectPhoneContacts.add(selectContact);
}
}
} catch(Exception e) {
e.printStackTrace();
}
selectContact.setName(name);
selectContact.setPhone(phoneNumberofContact);
int size = mcq.size();
String arr[] = null;
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr[i] = tv1.getText().toString();
// Log.e("Array",arr[i]);
} else if (op2.isPressed()) {
arr[i] = tv2.getText().toString();
//Log.e("Array",arr[i]);
} else if (op3.isPressed()) {
arr[i] = tv3.getText().toString();
// Log.e("Array",arr[i]);
} else if (op4.isPressed()) {
arr[i] = tv4.getText().toString();
//Log.e("Array",arr[i]);
}
I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.
here , Arrays in Java have a size so u cannot do like this. Instead of this
use list,
ArrayList<String> arr = new ArrayList<String>();
Inorder to do using String array :
String[] arr = new String[SIZEDEFNE HERE];
For ur answer :
ArrayList<String> arr = new ArrayList<String>();
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr.add(tv1.getText().toString());
} else if (op2.isPressed()) {
arr.add(tv2.getText().toString());
} else if (op3.isPressed()) {
arr.add(tv3.getText().toString());
} else if (op4.isPressed()) {
arr.add(tv4.getText().toString());
}
Retrive value using
String s = arr.get(0);
This is because your string array is null. Declare it with the size as
String[] arr = new String[size];
Try Array List. use the following code in your main java
final ArrayList<String> list = new ArrayList<String>();
Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(editText.getText().toString());
}
});
To avoid duplication in arraylist .You can use arraylist for faster then String array
ArrayList<String> list = new ArrayList<String>();
list.add("Krishna");
list.add("Krishna");
list.add("Kishan");
list.add("Krishn");
list.add("Aryan");
list.add("Harm");
System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);
I have a grid in my application and when I select an item it load the items prospectively. I want to allow to select the check box only when isMayoBaseAvailable returns true.
This is the code I used, when I used this code and debug it work correctly, but when I run the application and check it always go inside the false (In all the items). Why does it happens?
I have shown my logcat as well.
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> crust = new ArrayList<String>();
List<String> descriptionHalf = new ArrayList<String>();
final List<String> description = new ArrayList<String>();
List<String> extraDescription = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
String isMayoBaseAvailable = object
.getString("IsMayoBaseAvailable");
mayoBaseCB = (CheckBox) findViewById(R.id.ch_mayobase);
if (isMayoBaseAvailable.contains("true")) {
mayoBaseCB.setEnabled(true);
} else {
mayoBaseCB.setEnabled(false);
}
Logcat shows some items has isMayoBaseAvailable as true and some has as false.
I am having a problem with the above task in my android application. I am accepting user input from the EditText widget in the form of String. I accepting numbers from the user so I have to parse them to integers so they can be compared with another array of integers. I have the line:
String message = editText.getText().toString()
then to try and parse the String to an int I have the code line:
int userNumbers = Integer.parseInt(message).
However when I attempt to compare the array userArray with the array numbers I am getting the error that "Incompatible operand types String and Integer.
Can anyone see where my problem is or how I can solve it? Here's my code:
Thanks in advance.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = ".com.example.lotterychecker.MESSAGE";
static boolean bonus = false;
static boolean jackpot = false;
static int lottCount = 0;
Button check;
Integer [] numbers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//link to the intended web site and get the lottery numbers while the app is opening
try {
Document doc = Jsoup.connect("http://www.national-lottery.co.uk/player/p/drawHistory.do").userAgent("Mozilla").get();
Elements elements = doc.getElementsByClass("drawhistory");
Element table = elements.first();
Element tbody = table.getElementsByTag("tbody").first();
Element firstLottoRow = tbody.getElementsByClass("lottorow").first();
Element dateElement = firstLottoRow.child(0);
System.out.println(dateElement.text());
Element gameElement = firstLottoRow.child(1);
System.out.println(gameElement.text());
Element noElement = firstLottoRow.child(2);
System.out.println(noElement.text());
String [] split = noElement.text().split(" - ");
// set up an array to store numbers from the latest draw on the lottery web page
Integer [] numbers = new Integer [split.length];
int i = 0;
for (String strNo : split) {
numbers [i] = Integer.valueOf(strNo);
i++;
}
for (Integer no : numbers) {
System.out.println(no);
}
Element bonusElement = firstLottoRow.child(3);
Integer bonusBall = Integer.valueOf(bonusElement.text());
System.out.println("Bonus ball: " + bonusBall);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//called when the user clicks the send button
public void checkNumbers(View view) {
final int SIZE =6;
String [] userArray = new String[SIZE];
//create an intent to display the numbers
Intent intent = new Intent(this, DisplayNumbersActivity.class);
EditText editText = (EditText) findViewById(R.id.enter_numbers);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message );
startActivity(intent);
//parse string message to an int for user numbers
try{
int userNumbers = Integer.parseInt(message); //is this right?
}//try
catch (NumberFormatException e)
{
System.out.println("Not a number" + e.getMessage());
}
Toast.makeText(MainActivity.this, "Here are your numbers", Toast.LENGTH_LONG).show();
for (int count =0; count < SIZE; count ++)
{
if (check.isPressed())
{
userArray[count] = editText.getText().toString();
}
}//for
//compare the two arrays of integers
for (int loop = 0; loop < userArray.length; loop++)
{
for (int loopOther = 0; loopOther < numbers.length; loopOther++)
{
if (userArray[loop] == numbers[loopOther]) //how do I parse this?
{
lottCount++;
}else if (userArray[loop] == bonus)
{
bonus = true;
}
}//for
}//for main
You have this
Integer [] numbers; // numbers is an integer array
You have string array
String [] userArray = new String[SIZE]; // userArray is a string array
You compare like below
if (userArray[loop] == numbers[loopOther])
So you get the error Incompatible operand types String and Integer.
try
if (Integer.parseInt(userArray[loop]) == numbers[loopOther])
Enclosing the above with try catch block
String message = editText.getText().toString();
try{
int userNumbers = Integer.parseInt(message);
//is this right? yes
}
catch (NumberFormatException e)
{
e.printStacktrace();
}
Change String to Int here:
for (int loop = 0; loop < userArray.length; loop++)
{
for (int loopOther = 0; loopOther < numbers.length; loopOther++)
{
if (Integer.valueOf(userArray[loop]) == numbers[loopOther]) //how do I parse this?
{
lottCount++;
}else if (Integer.valueOf(userArray[loop]) == bonus)
{
bonus = true;
}
}//for
}//for main
Parse Like this :
for (int loop = 0; loop < userArray.length; loop++)
{
for (int loopOther = 0; loopOther < numbers.length; loopOther++)
{
if (Integer.parseInt(userArray[loop]) == numbers[loopOther])
{
lottCount++;
}else if (userArray[loop] == bonus)
{
bonus = true;
}
}
}
I am displaying running tasks in a ListView(simple_list_item_multiple_choice) and removing checked tasks from the list using button's onClick event as we can not kill the tasks completely in android so i have set the Timer to reload these tasks after every 5 seconds.
I am using one simple CheckBox widget to check all these CheckBox of Listview at once.
So my problem is that i want new items to be checked at the time of reload if my CheckBox widget is set to checked but i am unable to do that.
Here is my code :-
public void reloadTasks()
{
int listInitSize = list.size();
try
{
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
int numOfTasks = tasks.size();
for(int i = 0; i < numOfTasks; i++)
{
ActivityManager.RunningAppProcessInfo task = tasks.get(i);
boolean doAdd = true;
HashMap<String, String> item = new HashMap<String, String>();
item.put("Process", task.processName);
try
{
PackageInfo myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
item.put("Name", myPInfo.applicationInfo.loadLabel(getPackageManager()).toString());
}
catch (PackageManager.NameNotFoundException ne)
{
}
if(!list.isEmpty())
{
if(list.contains(item))
{
doAdd = false;
}
else
{
doAdd = true;
}
}
if(filter == true)
{
int size = SystemProcessList.length;
for (int j = 0; j < size; j++)
{
if(task.processName.indexOf(SystemProcessList[j]) > -1)
{
doAdd = false;
}
}
}
if(doAdd==true)
{
addItem(item);
}
}
}
catch (SecurityException se)
{
}
notes.notifyDataSetChanged();
int size = list.size();
int numOfLoop = list.size() - listInitSize;
for(int i = 1; i >= numOfLoop; i++)
{
if(cb.isChecked())
{
lv.setItemChecked(size-i, true);
}
}
}
I tried something to check the items at reload but it's not working and this attempt is not letting my app to get load on emulator(not force close just a blank screen).
You should use a listadapter and create a class that extends ArrayAdapter and then in your getView function you should implement each row. It is not so much easy, you can see a working example here
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/