Compare Strings in List View in android - android

I am developing an Android app in which there are two list views. When user clicks on items of first list view that must be added to second list view. This is I have implemented correctly. But my problem is strings must not be repeated in second list view. Means if String A is there in list view 1 and I click on it, it moves to list view 2 and if again I click on String A, it should prompt message(Message will be handled by me). Here is my code. What am I missing?
ArrayList<String> arr2;
ArrayAdapter<String> adapter2;
arr2 = new ArrayList<String>();
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String selected = lv1.getItemAtPosition(arg2).toString();
arr2.add(selected);
adapter2 = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv2.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
});

probably this will work.
String selected = lv1.getItemAtPosition(arg2).toString();
if (!arr2.contains(selected)) {
//add item in second ListView
arr2.add(selected);
} else {
show popup
}

Finally, got it....
if (!arr2.contains(selected)) {
arr2.add(selected);
adapter2 = new ArrayAdapter < String > (getBaseContext(), android.R.layout.simple_list_item_1, arr2);
lv2.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else {
Toast.makeText(getBaseContext(), "Already Contains", 1000).show();
}

I would suggest you save an ArrayList (your arr2) and check if arr2 already contains the string selected.
if(arr2.contains(selected)){
//continue with something else
}else{
//add to list and refresh Fragment
}

Use Contains method and check whether particular value exists in arr2 if it returns false then only perform arr.add(selected)

Change this
arr2.add(selected);
To
if(!arr2.contains(selected)){
arr2.add(selected);
}

Related

Check all of adapter elements in ListView

I have CustomAdapter which I am using for populating ListView with some data.
Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
So I have tried this:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0; i<=items.size(); i++) {
SomeItem item = items.get(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
}
});
But all of my listview elements have values of the first element in those two variables.
So how can I do something like item.next(); for validating all of items in listview?
UPD:
Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.
I have one more adapter:
SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);
and one more listview:
listViewAnother.setAdapter(adapterPr);
First of all I understood, that first variable should be from first listview and the second variable from another listview.
In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100.
We can say, that items from the first listview also have "id".
So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.
I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.
Now, hope I provided full information and you will be able to help me improve my code.
Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.
In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.
Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeItem item = adapter.getItem(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
});

Dynamic array in spinner issue

Scenario : I have 2 spinners with one adapter and global array. When any one select an item from one spinner then that item will delete from global array.Problem is when an item suppose select with 0 index again after deleting that item from global array and another item at acquire position with 0 index . If I will select that 0 index changed item then it will not select other than all items will select.
So can any one explain or suggest what i want to do in this scenario.
List<String> spinnerArray = new ArrayList<String>();
HashMap<String, String> index_val = new HashMap<String, String>();
ArrayAdapter<String> adapter_left;
spinnerArray.add("None");
spinnerArray.add("Task");
spinnerArray.add("Priority");
spinnerArray.add("Deadline");
spinnerArray.add("Assigned to");
spinnerArray.add("Created by");
spinnerArray.add("Closed by");
spinnerArray.add("Category");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.spinner_selected_item, noneArray);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
adapter_left = new ArrayAdapter<String>(activity, R.layout.spinner_selected_item_left, spinnerArray);
adapter_left.setDropDownViewResource(R.layout.spinner_dropdown_item);
task_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(!index_val.get("1").equalsIgnoreCase("None")){
spinnerArray.add(index_val.get("1"));
}
current_selected_val = adapterView.getItemAtPosition(i).toString();
index_val.put("1", current_selected_val);
if(!current_selected_val.equalsIgnoreCase("none")) {
spinnerArray.remove(spinnerArray.indexOf(current_selected_val));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Your Array Adapter should be notify every time, when ever you modify an array list like adding, deleting or updating.
So notify your adapter.
I hope this might help you.
It's not possible, because spinner maintains their items on behalf of index instead of actual items.

Android ListView first element different color

I'm working on a project having two list views used to select different regions and cities. The thing is, I have an option that is "All Region" and "All Cities" and I'd like to have these two of different color (in my case, the main app color)
I've tried things such as getItemAtPosition on my ListView but nothing is working.. Here's the code related to that ListView:
mListView = (ListView) findViewById(R.id.list);
ArrayList<String> regions = getIntent().getExtras().getStringArrayList("regions_selection");
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String regionId;
if (position == 0) {
regionId = "";
} else {
regionId = OptionsManager.getInstance().getOptions().getRegion(position - 1).getId();
}
SearchInformations.getInstance().setRegionId(regionId);
startActivity(new Intent(PhoneRegionListActivity.this, PhoneClubListActivity.class));
}
});
mListView.setAdapter(new ArrayAdapter<>(this, R.layout.list_item, regions));
Before setting the adapter (starting with KITKAT anytime) you can add header views which get wrapped automatically.
View all = getLayoutInflater().inflate(R.layout.list_item, mListView, false);
// setup view for all regions/cities
mListView.addHeaderView(all);
mListView.setAdapter(new ArrayAdapter<>(this, R.layout.list_item, regions));
I've been able to set different colors to different element according to their position this way:
if(position == 0){
textView.setTextColor(ContextCompat.getColor(getContext(),R.color.orange));
}else{
textView.setTextColor(ContextCompat.getColor(getContext(),R.color.black));
}
Here, the else is important otherwise, when scrolling, some other elements might change color. Otherwise, another way to avoid this issue is to reinitialize the view everytime with LayoutInflater.

Spinner first item default set empty value?

Here in this code i have added pincode numbers from 600000 to 600113 in the spinner but i want the first position to be just empty. When the user clicks only then the item should be shown. Please see my code -
final String[] myarray=new String[114];
for(int i=0;i<114;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,myarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addnum_spinner.setAdapter(adapter);
lv.setOnItemClickListener(itemClickedListener);
//Spinner click
addnum_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String value = myarray[position];
//
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
add this property to your spinner in xml and try if that works
android:prompt=""
I don't know if this is the right way but what about just adding the empty string before the for loop as the first item and then you just have to handle the user clicking on the empty item in your Listener. Something like
final String[] myarray=new String[115];
myarray[0] = ""
for(int i=1;i<115;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
I know this question is old, but I'm adding a solution. I found this out on accident, as I DIDN'T want a blank default value, but there was one.
If using an ArrayAdapter, I found that attaching the adapter to an empty ArrayList, and THEN filling it left a blank initial value.
Filling the ArrayList first, then attaching the ArrayAdapter removed the blank default value.
Obviously, if not using an ArrayAdapter, using the prompt, or setting the first Spinner item as <item></item> with nothing in between in the XML should work.

How to add a click listener on auto generated Android ListViews

I added to my class MyActivity the following :
private void updateMyList(){
listing=new ArrayList<listing>();
for(int i =0;i<10;i++)
{
Users user=new Users();
user.setListingName("Name" + i);
user.setListingPhone("i" + i);
listing.add(user);
}
MyListAdapter lfa = new MyListAdapter(this, listing);
((ListView)findViewById(R.id.listFeed)).setAdapter(lfa);
}
This code generates 10 list views so I'd like to add a click listener, so when I click on one of the 10 lists, I get a message or sthing.
Thank you for your help.
I don't see why you couldn't just add an onItemClickListener in your loop to your ListView. In short, use your Adapter to create the list then just attach the listener:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, connections.toArray(new String[connections.size()])));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View item, int position, long id) {
String item = (String) lv.getItemAtPosition(position);
}
});
This is if you want to know which item in each list was clicked, there is an setOnClickListener method as well in case you just want to know whether a ListView has been clicked or not.

Categories

Resources