I want to add single row to my ListView. When I run the code below, there are 2 lines added, one with the text I want to add, and an empty one. Why is the empty one added? (lines[0] is a file with (for now) 1 line in it)
listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.listviewrow, R.id.txt_listviewItem, lines){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View myView = convertView;
if (myView == null) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = li.inflate(R.layout.listviewrow, null);
} else {
TextView txt_listviewItem = (TextView)myView.findViewById(R.id.txt_listviewItem);
txt_listviewItem.setText(lines[0]);
}
return myView;
};
...
listView.setAdapter(listAdapter);
When I debug, listview.setAdapter(listAdapter) is called before AND after the getView code runs. Why? I only call it once.
you have use Viewholder to avoid duplication.
Just a wild guess, but does your text file include a "new line" character on the second row? Or, should I say, does your text file have, indeed, one line or two lines out of which the second is just a \n character?
Try this
listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.listviewrow, R.id.txt_listviewItem, lines){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View myView = convertView;
if(myView==null){
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = li.inflate(R.layout.listviewrow, null);
TextView txt_listviewItem = (TextView)myView.findViewById(R.id.txt_listviewItem);
txt_listviewItem.setText(lines[0]);
}
return myView;
};
listView.setAdapter(listAdapter);
Related
I am using ListView with alternate rows colored. But when the ListView exceeds the length of the screen, on scrolling, the alternate color disappears.
Below is my code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.problem_list_row, null);
if(position%2!=0) {
convertView.setBackgroundColor(Color.parseColor("#ebebeb"));
}
}
if(position%2!=0){
convertView.setBackgroundColor(Color.parseColor("#ebebeb"));
}
/* some code */
return convertView;
}
Attached below, the image of the ListView.
Try this code
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.problem_list_row, null);
}
if(position%2==0)
{
// SET EVEN POSITION COLOR
convertView.setBackgroundColor(Color.parseColor("#ebebeb"));
}
else
{
//SET ODD POSITION COLOR
}
/* some code */
return convertView;
}
I hope this helps you.
convertView is null if the entire ListView fits on the screen. However once you start scrolling, Views that move off the screen get passed to the getView() method as convertView. Sometimes the convertView received will be grey even though you want the View to be white. Your code doesn't deal with this case because it only colours Views grey, it never clears the background colour when it is no longer wanted. To solve this problem you need to set the colour both for odd and even positions.
This should work.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.problem_list_row, null);
}
convertView.setBackgroundColor(Color.parseColor(position%2!=0 ? "#ebebeb" : "#ffffff"));
/* some code */
return convertView;
}
I would recommend you to use multiple views, so two .xml file in your case.
I did exactly the same thing you want in an article on my website: http://raverat.github.io/android-listview-multi-views/
Hope this will help!
I need to customize this listview, I wonder how I could do to put the buttons and an identifier of different colors to distinguish them.
Anyone know how it could be the xml? and how it could change colors ?? is there any component in android? or I could use an imageview to the colors?
I appreciate your help.
You should use a custom class, to create the Adapter for the List, and that class has to extend the BaseAdapter class. In this class, you have to implement the following methods:
public int getCount()
public Object getItem(int position)
public long getItemId(int position)
public View getView(int position, View convertView, ViewGroup parent)
In the last one, you can get a LayoutInflater, and load a view from .xml. In that .xml, you can define the layout of one row. Here's an example from one of my projects:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// menuItems is an ArrayList of Strings
final String menu = menuItems.get(position);
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_mainmenu_row, null);
TextView menuText = (TextView) view.findViewById(R.id.menuListRow_menuItem);
menuText.setText(menu);
return view;
}
I am using a default array adapter to display my listview.
How do I highlight specific rows from my listview without touching anything i.e. without using onTouch() or onItemClick() (Just by using a code!) ?
I guess the way you should do it is use custom adapter and in getVew function highlight the row you needed
e.g.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final Holder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
...
if(position==thePositionYouNeed)
row.setBackgroundColor(color)
So in my listview I have a bunch of strings, but I'd like to display the values with £ in front of the strings, but NOT actually edit the values themselves in the ArrayList.
Anyone know how I can do this?
I'm already overwriting the view anyway, I'm just not sure how to alter the display of the items values themselves.
Some Code:
adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 != 0)
view.setBackgroundColor(Color.rgb(135,206,250)); // Pale blue
return view;
}
});
foodListView = (ListView) findViewById(R.id.foodListView);
foodListView.setAdapter(adapter);
In you getView method you can use:
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("£" + listItems.get(position));
I have a listview that contains items that have listviews. I am trying to populate item's listviews from inside the getView of the custom adapter that populates the "parent" listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
System.out.println("session adapter: here1");
if (v == null) {
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_session, null);
}
SessionObject i = sessions.get(position);
if (i != null) {
tvTrackName = (TextView) v.findViewById(R.id.textViewTrackName);
tvTrackName.setText(i.trackName);
tvSessionName = (TextView) v.findViewById(R.id.textViewSessionName);
tvSessionName.setText(i.sessionName);
tvSessionModerator = (TextView) v.findViewById(R.id.textViewModeratorName);
tvSessionModerator.setText("Moderator: "+i.sessionModerator);
listAbstracts = i.abstractList;
lvAbstracts = (ListView) v.findViewById(R.id.listViewAbstracts);
AbstractObjectAdapter adapter = new AbstractObjectAdapter(act, R.id.listViewAbstracts, listAbstracts);
lvAbstracts.setAdapter(adapter);
}
return v;
}
The "interior" adapter seems to only be calling its getView function once, regardless of the number of items in its list. If there are 2 items, only the first gets put into the listview. Is this the wrong way to do this? Am I missing something?
Any help is appreciated. Thanks.
Just create your item view dynamically, adding textviews to a linearlayout. You'll just have to be careful about handling the convertView, initially it will be easiest to always ingore it but you'll take a bit of a performance hit.
Optimise later.