Efficient way to display the List View in Android - android

I have attached the screen shot of my custom list view. I want to display it efficiently. What is the best possible way to do it?
thanks
Sneha
i have uploaded another list view screen display . But alignment is not proper in it. How to resolve it??

With ListView there is a very standard method to improve its performance.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.text_item, parent, false);
}
((TextView) convertView).setText(getItem(position));
return convertView;
}
Basically, you dont need to inflate the convertview all time, you can inflate it once and use it the next time on. The above is a very standard practice, read about it on http://android.cyrilmottier.com/?p=422

If you are talking about how to make the look more aesthetically pleasant then I would suggest you to use Table layout inside each row to organize all the information

Related

Listview duplicates value

I have dynamic listview in which textview and edittext are two columns. The textview comes with proper data but when i insert the value in edittext in first row then i scroll down the list and in third row the edittext is fill with the first row edittext value. I don't know why this happening. Please help me.
Please provide some code so we can better understand the problem. From what i understand this is whats happening.
The adapter generates as many views as can be accomodated on the screen. When you scroll down the list, a new item needs to be generated. This can be done in two ways inside the getView() method shown below.
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
//inflater is the layout inflater for the custom view
convertView=inflater.inflate(R.layout.list_row, null);
return convertView;
}
else{
return convertView; //recycled view
}
}
convertView is the view that just went off the screen and is available for recycling. In your case its the convertView which was supposed to be destroyed but is reused again. So the edit text values reappear in the view below.
I guess it's because you're inflating a layout for items with have concrete IDs (manually set) and android may think that all listview's items are the same
But if you want real help, paste some code and layouts here... We're not fortune-tellers

Cause onClick of a ListView row to offer editing options

I'd like to have the effect of clicking on a row in a ListView and offering buttons to do basic editing with what I'm displaying in said row.
Now I don't mean to change a textview into an editable-textview exactly, rather something that just offers buttons like "edit" and "delete" for example. This could be done with a popup but I'm trying to avoid this, I want these action buttons to be replacing the displayed content of that row.
At first I figured it could just have two layouts for inflating into rows. One "active" and one normal. On click it would just return the different layout for the row clicked and have a marker to indicate which row was the currently selected one. First ran into issues I didn't expect with the inflated views being recycled as a listview is scrolled up & down. Fine, ok, so I made an extension of BaseAdapter so I could do my own thing with getView(). Well I managed to get it to correctly allow recycling of views (and not reusing the "active" one where it wasn't suppose to be) however I see no way to make it refresh / reload the alternate layout except when scrolling said row off screen and then back on. Seems there's no way to force getView() to actually happen unless a row leaves the screen and comes back.
So it's a two headed question. One is there a way to make a single row swap out inflated views while it's being displayed? And two maybe my method of doing this is a backwards way to accomplish what I want and is there a better way?
Thank you for your time!
public View getView(int position, View convertView, ViewGroup parent) {
View v;
Boolean activeExists = false;
if(convertView!=null && convertView.getTag()==(String) "active"){
activeExists=true; }
if (((position==activeFlag && activeExists==true) || (position!=activeFlag && activeExists==false)) && convertView!=null) {
v = convertView;
} else if(position==activeFlag && activeExists==false){
v = inflater.inflate(rowlayoutActive, parent, false);
v.setTag((String) "active");
} else if (position!=activeFlag && activeExists==true) {
v = inflater.inflate(rowlayout, parent, false);
} else {
v = inflater.inflate(rowlayout, parent, false);
}
bindView(position, v);
return v;
}
(Outside of this getView I have (int) activeFlag to remember which one is the current "selected" and I have my own version of bindView which doesn't really differ that much from normal. The boolean is a quick mark for already inflated views to keep the wrong one from going to the wrong row when recycled.)
Great question, and part of the answer might be that you consider the MVC model. Strictly speaking, you should modify the underlying data to cause a change in the UI which suggests adding perhaps a boolean to your data "isEditing" or similar, set it with the click then calling notifyDataSetChanged. In getView, you would test that boolean to use the appropriate layout.

what's exactly happening in getView

I see this a lot in Adapter extensions override of getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.whatever, null);
}
I get semantically what's happening - "if convertView is null then inflate it", but I don't really understand why - what circumstance would convertView be null, and in what circumstance would it already be a View?
Also (and I know "1 question per post"), but as it relates to the above - what's exactly happens during the inflate method? I know what it does generally ("inflates" a view resource to be parsed and populated), but don't fully grok it...
TYIA
AFAIK, this is how getView works.
Suppose you have a list with 50 items and you can only see 5 items at a time. The getView will be called 5 times initially and the convertView will be null for each row and a new row should be inflated.
As you scroll through the list the getView will be called again as the next rows get visible on the screen. Now since 5 rows are already rendered for the list, these rows will be recycled by updating existing rows with new values to show new rows. At this case the convertView will not be null.
Say your list adapter has 1000 objects. Each object is represented by a view. On the phone screen there's place only for maybe some 10 such views. You scroll the list wanting to see more items. Some items go out of sight because you're scrolling. It makes no sense for the Framework to create more views as they will be exactly the same as those which have just gone out of sight. Framework thus can reuse some views it created before and so it offers to you such a possibility by offering you a non-null convertView.
During the inflate method a View object gets created out of some XML resource. The XML resource has a description of a View sufficient for that View to be created and so the inflate method does that creation. So no view - you create a new one by inflating, there is a view already - you can reuse it (you don't have to but you should)
for full information i recommend you to watch this video
it will tell you alot of information include what is happening in getView()

How does scrolling work in the Google+ Android app?

I've noticed, while playing around with it, that if you scroll up and down in the stream from the Google+ Android app, the scroll bar changes size depending on the vertical size(s) of the currently visible post(s). For example, if you scroll into a long posting, the bar shrinks in size, and if you scroll into a short post, it lengthens. How is this implemented?
Now, I don't particularly need this feature, but it's just something that has piqued my curiosity.
It's a side effect of using a recycling ListView. As new posts are scrolled in to view, the rest of the items are virtualized - basically Android guesses as to how much space the rest of the list will take up, but it doesn't actually render them so it can't be sure. As you scroll on to a big post, it assumes the rest of the list has big posts in it and therefore the list is longer.
You can get the same functionality by using the convertView parameter of getView like so:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if(convertView == null)
{
view = // set your view here
}
else
{
view = convertView
}
// set all your properties on the view here.
return view;
}

How to color code individual rows in ListView as is done in MESSAGING app?

How can I color code individual rows in a ListView exactly like it is done in the native MESSAGING app? I do NOT want to simply do alternating rows, but I want to copy the format used in the MESSAGING app, where message rows have a different background color based on the username of the message.
FYI:
I am currently extending SimpleAdapter and overriding getView(int position, View convertView, ViewGroup parent). In this method, I am trying to calculate setting the background color based on the position as compared to a list of 'positions to highlight' that I am maintaining each time I update the list, but so far this is only working the first time the list is updated.
In class that overrides SimpleAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if(highlightPositions.contains(new Integer(position))){
view.setBackgroundColor(highlightColor);
}
else{
view.setBackgroundColor(normalColor);
}
return view;
}
Thank you for any guidance you can offer!
I think you need to call notifyDataSetChanged() on your ListAdapter. This forces the list to refresh, calling your overridden getView() method.
Thanks for the help. Actually, the code I listed does in fact work. The problem turned out to be a small mistake in my keeping track of the list positions to highlight in the highlightPositions list. Once I corrected that, and in fact highlightPositions list did indeed contain the correct positions to highlight, then it worked properly.

Categories

Resources