Android ListView: don't show all strings - android

I show a listview from the following string:
String[] values = new String[] { test1, test2, test3 };
The variables:
private String test1 = "test";
private String test2 = "test";
private String test3 = "test3";
Now I don't want to show the strings that contain "test" in my listview.
Like this:
if (String == "test") {
*don't show in ListView*;}
And I want it to test all Strings at once if they contain "test"
How is it possible?
EDIT: Here the adapter code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);

Yup...
if (values[i].equals("test")) // i being the iterator of your for-loop
OR
if (values[i].contains("test"))
contains is a slow(er) string function though.
You might want to use an ArrayList though... that way you can just add all those objects to the array list, then iterate through it... and remove them as you go...
// Do something to add all items to your array list
...
// Iterate through the list, removing what doesn't need to be there
for (int i = 0; i < arrayList.size(); i++){
if (arrayList.get(i).contains("test"))
arrayList.remove(i);
}
...Then set 'arrayList' (or whatever you've called it) as the string list for your adapter. If you use the same constructor it'll look like this...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList.toArray());

all the time, if you want compare 2 strings use the equals methode
read this about The equals() method

You should be using a filter on your ListView. Look at these examples:
(Simple iterating) http://androidsearchfilterlistview.blogspot.com/2011/06/android-custom-list-view-filter.html
(getFilter()) Filtering ListView with custom (object) adapter
Like others have posted, you compare String objects using .equals(), but if you are trying to only display certain items in your ListView you should use the getFilter() method like described in the link I posted.
edit: I found you a nice SO example.

Related

adding static data to existing listView where data is populated from sqlite

I have the following codes to add data from sqlite to the listView. (The code works) However, I want to have static data on the listView and also have the function on adding from sqlite. Meaning that, I want to do a listView with data "A", "B","C" as default. So when user clicks on the "add" function user is able to add the data into sqlite and display into the same listView where the "A","B","C" data is.
Please help.
step 1
declare one empty arraylist then add static data like in your case A,B,C and fill adapter with this data.
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
MyAdapter myAdapter=new MyAdapter(this);
listview.setAdapter(myAdapter);
myAdapter.addItem(arrayList);
in Adapter add this function
public void addItem(ArrayList<String> arraylist){
this.arraylist = arraylist;
notifyDataSetChanged();
}
step 2
in your add item function do 2 things
1 add those data in your database.
2 add those data into arraylist that you have created in step 1, and add it to adapter like below code.
databaseHelper.addData("D");
arrayList.add("D");
myAdapter.addItem(arrayList);
step 3
check after this when you come second time in screen check whether database has data init if yes then modify step one like below
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
MyAdapter myAdapter=new MyAdapter(this);
listview.setAdapter(myAdapter);
if(databaseHelper.getDataList()!=null &databaseHelper.getDataList().size()>0{
arrayList.addAll(databaseHelper.getDataList());
myAdapter.addItem(arrayList);
}else{
myAdapter.addItem(arrayList);
}
you used imageViewToByte() for change ImageView to byte, but R.mipmap.newsgd is not ImageView, it resource
All you do is :-
//get all data
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD");
list.clear();
while (cursor.moveToNext())
{
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte [] image = cursor.getBlob(3);
list.add(new Food(id,name,price,image));
}
// Adding your three items for the list
list.add(new food(your_id_for_A,your_name_for_A,your_price_for_A,your_image_for_A));
list.add(new food(your_id_for_B,your_name_for_B,your_price_for_B,your_image_for_B));
list.add(new food(your_id_for_C,your_name_for_C,your_price_for_C,your_image_for_C));
adapter.notifyDataSetChanged();
Noting that you may have to be a little careful with what id you provide and how you subsequently handle that id as you may have to distinguish between a row from the database as opposed to an added row.
Just add A, B, C after listView.setAdapter(adapter); -> list.add(A), list.add(B), list.add(C)
Ex:
list.add(new Food(someId, "Singapore","4.772",R.mipmap.newsgd, ...));
Hope this help!

Getting a spinner's item list

I can retrieve a list of all items of a spinner by:
List<String> list = new ArrayList<String>();
for (int i =0; i<spinner.getCount(); ++i)
{
String item = String.valueOf(spinner.getItemAtPosition(i));
list.add(item);
}
Or storing the item list globally...
Is there any more elegant way, something like .getItemList()?
My concern is the iteration (linear complexity), I would prefer to directly get the list from the adapter (possibly constant complexity?)
See http://developer.android.com/reference/android/widget/Adapter.html and http://developer.android.com/reference/android/widget/SpinnerAdapter.html (which inherits from Adapter)
You will find that the method you're looking for doesn't exist
Your solution is about as elegant as it gets

Creating a custom hashmapadapter

If I have a hashmap containing the following:
Hashmap contains (String, String)
How can I instantiate a custom adapter? The custom adapter should extend baseadapter.
I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.
Instantiation should look something like this:
MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);
setListAdapter(adapter)
I am lost here so code would be a big help.
THANKS
graham
The following list is using as its datasource a string array called items.
public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items));
however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap
so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm
I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter.
For you example you can do:
HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
for(int i=0; i<=10;i){
hm.put(i,("num"+i));
}
for (Entry<Integer, String> e : hm.entrySet()) {
String newString=e.toString();
elements.add(newString);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
list.setAdapter(adapter);

android ArrayAdapter items update

I have ArrayAdapter with this items structure:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
<TextView
android:id="#+id/itemTextView"
... />
</RelativeLayout>
And add this adapter so:
mAdapter = new ArrayAdapter<String>(this, R.layout.item,
R.id.itemTextView, itemsText);
All is fine but I want to update text in adapter's items. I found a solution
mAdapter.notifyDataSetChanged();
but do not understand how to use it. Help please.
upd
My code:
String[] itemsText = {"123", "345", "567"};
ArrayAdapter<String> mAdapter;
onCreate
mAdapter = new ArrayAdapter<String>(this, R.layout.roomitem,
R.id.itemTextView, itemsText);
setListAdapter(mAdapter);
itemsText = {"789", "910", "1011"};
onClick
mAdapter.notifyDataSetChanged();
//it's dont work
I think something like this
public void updatedData(List itemsArrayList) {
mAdapter.clear();
if (itemsArrayList != null){
for (Object object : itemsArrayList) {
mAdapter.insert(object, mAdapter.getCount());
}
}
mAdapter.notifyDataSetChanged();
}
Your problem is a typical Java error with pointers.
In a first step you are creating an array and passing this array to the adapter.
In the second step you are creating a new array (so new pointer is created) with new information but the adapter is still pointing to the original array.
// init itemsText var and pass to the adapter
String[] itemsText = {"123", "345", "567"};
mAdapter = new ArrayAdapter<String>(..., itemsText);
//ERROR HERE: itemsText variable will point to a new array instance
itemsText = {"789", "910", "1011"};
So, you can do two things, one, update the array contents instead of creating a new one:
//This will work for your example
items[0]="123";
items[1]="345";
items[2]="567";
... or what I would do, use a List, something like:
List<String> items= new ArrayList<String>(3);
boundedDevices.add("123");
boundedDevices.add("456");
boundedDevices.add("789");
And in the update:
boundedDevices.set("789");
boundedDevices.set("910");
boundedDevices.set("1011");
To add more information, in a real application normally you update the contents of the list adapter with information from a service or content provider, so normally to update the items you would do something like:
//clear the actual results
items.clear()
//add the results coming from a service
items.addAll(serviceResults);
With this you will clear the old results and load the new ones (think that the new results should have a different number of items).
And off course after update the data the call to notifyDataSetChanged();
If you have any doubt don't hesitate to comment.
Assuming itemTexts as String array or String ArrayList,where you are adding new items into itemsTextat that time after that you can call
mAdapter.notifyDataSetChanged();
If you did not get answer then please put some code.
I did something like this. And it works correctly.
Add method to the Adapter class:
public void updateList(ArrayList<ITEM> itemList){
this.itemList.clear();
this.adapterList = new ArrayList<ITEM>();
this.adapterList .addAll(itemList);
notifyDataSetChanged();
}
Call the method in the class you use the adapter:
itemList.add(item);
adapter.updateList(itemList);

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources