I have this function in the bottom, In this function I fill the listview in the MainActivity, I can fill with any text I want, but I do not know how to hide, I tried the thing in the comment part to hide it, but did not work.
I need to check a certain condition and if true hide R.id.dire_win, if false hide R.id.radiant_win
Also I need to fill R.id.team01_pic in each row of the list view with an .png/.jpg url.
Thanks for your help in advance.
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SingleContactActivity.this, matchList,
R.layout.list_match, new String[] { TAG_team01_name, TAG_team02_name , TAG_RESULT }, new int[] {
R.id.team01_name, R.id.team02_name , R.id.result});
//ImageView imgView = (ImageView) findViewById(R.id.radiant_win);
//imgView.setVisibility(View.INVISIBLE);
//TextView textview =(TextView )findViewById(R.id.text);
setListAdapter(adapter);
}
Update: I tried to create this function as one of the answers said, but did not work .
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_match, parent, false);
//TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.radiant_win);
//textView.setText(values[position]);
// change the icon for Windows and iPhone
//String s = values[position];
imageView.setImageResource(R.drawable.team_navi);
//imageView.setVisibility(View.INVISIBLE);
return rowView;
}
Solution is to use Custom adapter instead of Simple adapter. Then you have to create a custom xml with all widgets you want (imageView,TextField, etc.). In your custom adapter class, inside the getView() method, that you have previously overridden, you can specify the conditions of imageView visibility. This example should help you:
http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html
Also read this (http://developer.android.com/reference/android/widget/Adapter.html)
Related
Overview:
I have a ListView with a custom adapter/layout, every time a user adds a new row (which contains a number), I check if that number is the smallest in the list. If so, an image within that row must be set as visible while setting all other row's images as invisible.
Problem:
My ListView does not set any row's image as visible, even though I have the index of the smallest element.
How I'm doing it:
//In MainActivity
private void addProduct(float price) { //User adds product
priceList.add(price); //Add to Float list
adapter.notifyDataSetChanged();
updateView(findMinIndex(priceList)); //Find smallest val indx
}
private void updateView(int index){
View v = listView.getChildAt(index -
listView.getFirstVisiblePosition());
if(v == null)
return;
ImageView checkMark = (ImageView) v.findViewById(R.id.check_mark);
checkMark.setVisibility(View.VISIBLE); //Initially set Invisible
}
Edit, CustomAdapter:
public CustomList(Activity context,
ArrayList<Float> priceList) {
super(context, R.layout.list_single, priceList);
this.context = context;
priceList = priceList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView price = (TextView) rowView.findViewById(R.id.new_price);
ImageView cheapest = (ImageView) rowView.findViewById(R.id.check_mark);
price.setText(priceList.get(position) + "");
return rowView;
}
Thank you
It is your priceList binded with the adapter?
First of all i would put a breakpoint to see if you are getting the right view in the updateView method.
try this way;
Create a Pojo class with imageview and it's state(Visibility) initially set all to invisible
Add your items to the ArrayList of Pojo Class type.
when user enters a new row based on your requirement set visibility state to true or false(visible or invisible) and call notifyDataSetChanged() to the adapter.
Doing this way you can have a easy track of the items.
I got it working :).
Problem is that adapter.notifyDataSetChanged(); is async, so while it's doing that, updateView(findMinIndex(priceList)); runs but doesn't find the new row as it should. Therefore, I add a runnable to the ListView object as so:
adapter.notifyDataSetChanged();
listView.post( new Runnable() {
#Override
public void run() {
updateView(findMinIdx(priceList));
}
});
Now it works perfectly!
As Adapter for ListView, I am using custom adapter. On Adapter's getView method, I am trying to change listitem's background image using setBackgroundDrawable.
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
CurrencyModel currency = new CurrencyModel();
currency = currencyList.get(position);
if (convertView == null) {
vi = inflater.inflate(R.layout.listitem_currency, null);
}
TextView tvSymbol = (TextView) vi.findViewById(R.id.tvSymbol);
TextView tvSize = (TextView) vi.findViewById(R.id.tvSize);
TextView tvName = (TextView) vi.findViewById(R.id.tvName);
TextView tvRate = (TextView) vi.findViewById(R.id.tvRate);
tvSymbol.setText(currency.getSymbol());
tvSize.setText(currency.getSize());
tvName.setText(currency.getName());
tvRate.setText(currency.getRate());
if (currency.getSymbol().equals("AUD")) {
vi.setBackgroundDrawable(context.getResources().getDrawable(
R.drawable.bg_exch_high_cell));
}
return vi;
}
When activity starts, everything is working correctly - listitem with AUD has different background. All mystery starts when I scroll the listview - other listitems also get "special" background. The more scroll, the more listitems changed. I do not have idea why this is happening. How to solve this problem?
if (currency.getSymbol().equals("AUD")) {
vi.setBackgroundDrawable(context.getResources().getDrawable(
R.drawable.bg_exch_high_cell));
}
else {//restore default background}
This happens because cell views are reused by listview. You should restore default background for other items.
More about list view performance
TextView tvSymbol = (TextView) vi.findViewById(R.id.tvSymbol);
Before this line add the default background the you put to the item. The reason in your code why it happens is that android try using the the already created view(vi). Since if it is assigned bg_exch_high_cell, it will retain it. So reset it in the beginning.
vi.setBackgroundDrawable(context.getResources().getDrawable(
//default background drawable here ));
TextView tvSymbol = (TextView) vi.findViewById(R.id.tvSymbol);
I created an application for chat between two person
first of all i have to fetch all data from server by Jsonparser
there is a custom listview each row contains "shop,painter,datetime,comment,id"
if yourname is not empty means comment is yours and viseversa
i want to put a bubble background for comment that indicate the painter or shop
i dont know how to use getview when i have a custom listview with more than one textview and because my resource data that is contain all information comes from server and store it to the hashmap array... hashmap doesnt have position as it is in getview method...
.........some code .....
// adding HashList to ArrayList
AllCommentsList.add(map);
adapter = new SimpleAdapter(getApplicationContext(),
AllCommentsList, R.layout.list_row_order_comments,
new String[] { TAG_COMMENT_ID, TAG_SHOP, TAG_PAINTER,TAG_COMMENT, TAG_DATETIME },
new int[] { R.id.tvIdComments, R.id.tvShopSender,R.id.tvPainterSender, R.id.tvComment,R.id.tvDateTimeComments });
// updating listview
listViewComment.setAdapter(adapter);
this is my code but i want to dynamically change the background of Comment textview
how to put some code like this????
if (strPainter.equals("null")) {
tvComment.setBackgroundResource(R.drawable.bubble_green);
}
if (strShop.equals("null")) {
tvComment.setBackgroundResource(R.drawable.bubble_yellow);
}
Don't use SimpleAdapter. Create a custom adapter that overrides getView and make whatever manipulations that you want to the layout, background, etc based on the current item.
See /samples/android-8/ApiDemos/src/com/example/android/apis/view/List5.java in your Android SDK folder (download appropriate samples as necessary) for a simple example:
private class MyListAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(
android.R.layout.simple_expandable_list_item_1, parent, false);
} else {
tv = (TextView) convertView;
}
tv.setText(mStrings[position]);
return tv;
}
...
}
I use even and odd rows to set backgrond to my listview rows. In my efficientAdapter I set the row background as follows:
public View getView(int position, View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.ecran_multiple_row, null);
holder = new ViewHolder();
holder.txIndex = (TextView) vi.findViewById(R.id.txIndex);
holder.txSTitle = (TextView) vi.findViewById(R.id.txSTitle);
holder.btOnOFF = (ImageView) vi.findViewById(R.id.btOnOFF);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
/*
* CHANGE ROW COLOR 0 WHITE 1 GRAY
*/
if ( position % 2 == 0) //0 even 1 odd..
vi.setBackgroundResource(R.drawable.listview_selector_odd);
else
vi.setBackgroundResource(R.drawable.listview_selector_even);
/*
* ONE ITEM IN ARRAY
*/
if (data.toArray().length==1){
holder.btOnOFF.setBackgroundResource(R.drawable.air_radio_button_rouge);
}else {
holder.btOnOFF.setBackgroundResource(R.drawable.air_deezer_check);
}
return vi;
}
and in my MainActivity.Class. I select an item using on itemclicklistener() as shown below:
**lvRMultiple.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imgview = (ImageView) view.findViewById(R.id.btOnOFF);
//And change its background here
imgview.setBackgroundResource(R.drawable.air_radio_button_rouge);
}
});**
When i clicked on an item btnOff image change successfully but when i scroll down it change to default background. Secondly when i click on one item after the other both becomes the new image but i want only the row clicked by the user to change to new image and the previous image are set to default.
All row view of a ListView created by the getView() method of BaseAdpter class. When ever we scroll the ListView all, new viable row create by getView() using recycle. So getView() called again and again when new row is viable on scroll.
There are two solution of your question:-\
You can save the status of ListView
// Save ListView state
Parcelable state = listView.onSaveInstanceState();
// Set new items
listView.setAdapter(adapter);
// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state)
And other solution is create RowView at runtime and add it on a Parent Layout by using addView() method.
LayoutInflater inflater = LayoutInflater.from(context);
// You should use the LinerLayout instead of the listview, and parent Layout should be inside of the ScrollView
parentView = (LinerLayout)this.findViewById(R.id.parentView);
for(int i = 0; i<=numberOfRow;i++){
LinearLayout rowView = (LinerLayout)inflater.inflate(R.layout.rowView);
ImageView rowImageView = (ImageView)rowView.findViewById(R.id.rowImage);
rowImageView.setOnClickListener(new View.onClickListListener(){
#Override
public void onClick(){
rowImageView.setImageBitmap(onClickBitmapImage);
}
});
parentView.addView(rowView);
}
Please check this answer Maintain/Save/Restore scroll position when returning to a ListView
More Reference
http://developer.android.com/reference/android/widget/Adapter.html#getView(int,android.view.View, android.view.ViewGroup)
The item changes back to the default background because the view gets recycled. This is the same problems of checkboxes losing their checked state
Check out this answer too see how to handle it:
CheckBox gets unchecked on scroll in a custom listview
As for your second problem, I believe it's already answered here:
highlighting the selected item in the listview in android
Hope it helps
i need a some help , executing fallowing task. can any one help to sort out this ?
I have list view . each row contains
Title (Textview)
sub title(TextView)
Progress bar
progress count(TextView)
Download/ Cancel(ToggleButton)
while loading adapter i passed List . from where am displaying title , subtitle for each row.
here is myadapter code , generating views for each row.
My problem is that . In run time when i entered download button, its goes to downloadQueue to download, and updates respective progress bar . But Whwn am scrolling down the listview., progress bar visible for other row instead current row where i started Downloading. Can some one tell me t, how to bind view and state object so that it wont misplace progressbar.
`
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos= position;
final StateViewHolder viewHolder;
final State state = stateList.get(pos) ;
if (convertView == null) {
inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.adapter_view, null);
TextView title = (TextView) convertView.findViewById(R.id.text);
TextView subTitle = (TextView) convertView.findViewById(R.id.subText);
TextView progresstext = (TextView) convertView.findViewById(R.id.progressText);
ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
ImageView cancel = (ImageView) convertView.findViewById(R.id.cancel);
ImageView download = (ImageView) convertView.findViewById(R.id.download);
viewHolder = new StateViewHolder(title, subTitle, progresstext, download, cancel, progressBar);
hashMap.put(state, viewHolder);
convertView.setTag(viewHolder);
Log.i("DownloadAdapter", "InGetView");
}else{
viewHolder = (StateViewHolder)convertView.getTag();
}
state.setViewHolder(viewHolder);
viewHolder.getDownload().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.getDownload().setVisibility(View.GONE);
viewHolder.getCancel().setVisibility(View.VISIBLE);
Log.i("DownloadAdapter", ""+state.getName());
DownloadQueue.addToQueue(state);
}
});
viewHolder.getCancel().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.getCancel().setVisibility(View.GONE);
viewHolder.getDownload().setVisibility(View.VISIBLE);
DownloadQueue.RemoveFromQueue(state);
}
});
viewHolder.getTitle().setText(state.getName());
viewHolder.getSubTitle().setText(state.getEffective());
return convertView;
}
}
Thanking you,
Srinivas
`
When you use a view holder to reuse your row view, then you need to explicitly set all views everytime. In other words, you need to set every element in that row including your Title, Subtitle, Progress count text view, progress bar, and download Image view toggle.
What you are seeing is when one row sets the progress bar as visible, another row may be reusing that (thus you would see the progress bar on that row too). So you need to explicitly set the progress bar to not be visible for the rows that aren't downloading.
So at the bottom of your getView method, make sure you have logic to set every view:
viewHolder.getTitle().setText(state.getName());
viewHolder.getSubTitle().setText(state.getEffective());
viewHolder.getProgressBar().setVisibility(isImageDownloading);
viewHolder.getProgressCount().setText("example");