I´d like to add an image in a row of a ListView, this is my code:
ListView listViewProducts = (ListView) findViewById(R.id.listViewProducts);
ImageView iconMeat = (ImageView) findViewById(R.id.iconMeat);
String[] products = getResources().getStringArray(R.array.products);
for (int itemPos = 0; itemPos < listViewProducts.getCount(); itemPos++)
{
if ( products[0].equals(listViewProducts.getItemAtPosition(itemPos)) )
{
//TODO
// In this case to add the image
}
}
Thanks
See these posts for using adapters in listview :-
https://stackoverflow.com/a/8267165/2035885
http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter
http://www.mkyong.com/android/android-listview-example/
Do following:-
Create a custom adapter for listview.
Create an array of drawables for populating the list.
In your getView method of adapter populate the list according to index.
Related
I am using a custom array adapter to display grades in a ListView, my question is how can I let a user set the number of rows displayed? I will take their selection in an editbox and then when they hit a button called "select" I will limit the display. I have searched for this elsewhere but couldn't find exactly how to perform this method. Thanks.
Just implement this code after onClick()
for (int i = 0; i < userInput; i++) {
yourList.add(data);
}
MyCustomAdapter dataAdapter = new MyCustomAdapter(yourCkass.this.getActivity(),
R.yourLayoutListItem, yourList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
Hi guys is it possible that everytime I add an item in the database it will notify and update the listview? For example I have two fragments in one activity then When I send a data in the database from the Fragment A the Listview in Fragment B will update also without using interface.
There is a function of ArrayAdapter called - notifyDataSetChanged()
//getting the list view
ListView userListView = (ListView) findViewById(R.id.users_ListView);
//creating an array to represnt what ever you want
ArrayList<String> users = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
//populate the array
String s = "user " + i;
users.add(s);
}
//setting up adapter to the array (this is 1 row with 3 arguments)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
users);
//connecting the list view to get the data to show from the adapter
userListView.setAdapter(adapter);
//changing the arrayList by adding or subtracting
String s = "another user";
users.add(s);
//update the adapter and list view with this command
adapter.notifyDataSetChanged();
Share your code if you are having any trouble.
good luck =)
This is my code where I want to get a specific child view of a ListView:
ListView list = (ListView) findViewById(R.id.lst);
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,
R.layout.text, R.id.txt1, data);
list.setAdapter(adap);
int num = list.getCount();
for (int j = 0; j < num; j++)
{
view1 = findViewById((int)list.getItemIdAtPosition(j));
text_view = list.getChildAt(j);
long lg = list.getItemIdAtPosition(j);
if (text_view != null) {
text_view.startAnimation(animation); // getting null in text_view .
}
if (view1 != null) {
view1.startAnimation(animation); // getting null in view1 .
}
}
The view that you get by using getChildAt is a row contained in your list view. It depends on how you have defined that row. If your row contains a layout and then a textView, the child view that you get is the layout. Then the textView is a child of that layout.
Let say your selected item position is in variable named pos.
pos=2; // selected item position
int nFirstPos = listNotification.getFirstVisiblePosition();
int nWantedPos = pos - nFirstPos;
if ((nWantedPos >= 0) && (nWantedPos <= listNotification.getChildCount()))
{
View view = listNotification.getChildAt(nWantedPos);
if (view!=null) {
//add your code here
TextView textData=(TextView) view.findViewById(R.id.txt1);
}
}
i think you can do that for only visible items, and you should know that all items of listview are not visible at once. Please check the below links, you will solve your problem:
Android: Access child views from a ListView
In an android ListView, how can I iterate/manipulte all the child views, not just the visible ones?
I have a listview , i would like to put some position preselected; in fact i have some values that i would like to put them selected. this is my code
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i)) {
selectedItems.add(adapter.getItem(position));
}
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
My selectedItems firstly is not empty; i would like to put its value as selected.
Pass your selectedItem list to your custom adapter and inside getView() method compare position. If it is available in seloectedItem list change the background of view.
Use a custom adapter and pass the list inside there and render it on getView() by inflating the item.xml layout and use a static class viewHolder to create a viewholder that transfers the data from your list into the appropriate widgets.
Make sure you check if the convertView has been created previously as you do not want to create the view every time android hits getView from your adapter as it will slow your listView UI.
Good luck
I am trying to count listView items. Im using this code:
int count=0;
ListView listView = (ListView) findViewById(R.id.listView1);
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++;
}
}
Toast.makeText(getApplicationContext(), String.valueOf(count), Toast.LENGTH_SHORT).show();
Why the COUNT variable value is always 0, when listView display some records?
If you are looking for count of all ListView items, you can use this call (make sure adapter is set):
listView.getCount();
If what you want is count of visible items, try this (works only for visible ListView):
listView.getLastVisiblePosition()-listView.getFirstVisiblePosition();
Let me explain the reason..
You've just get the listview like this
ListView listView = (ListView) findViewById(R.id.listView1);
so listview has no elements and then you are tring to get the last visible postion by using listView.getLastVisiblePosition() it always returns zero because your listview hasn't yet bind with any adapter, i.e your listview is empty at the time your are getting the last visible position try to place this code after binding Adapter to the listview
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++;
}
}
Use this .It helps mine
String CountListRowNo= String.valueOf(+ListviewObj.getAdapter().getCount());