Add differents items to custom list adapter - android

I have a List view with a custom adapter (imageviews), in the same activity I have a header and footer, the listView is between those.
What I want is to add a button as the last item of the list view so when you arrive to the last item it appear, i cant add the button outside because it wont scroll
Sorry about my english
Regards

use ListView.addFooterView() to add view as a footer which is visible only in the end of the list:
http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

If you already have a custom adapter class implemented, the solution is rather simple. Based on an xml layout implemented for your list-view rows which contains both a Button and an ImageView, you can hide/display them in the adapter's getView() method based on the index. This is a code sample, which I currently don't have the chance to test and might not be the most efficient solution, but it should give you an idea:
class CustomAdapter extends SimpleAdapter {
[...]
#Override
public int getCount() {
// number of images to be displayed + 1 for the button
return images.length + 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = inflater.inflate(R.layout.row, parent, false);
final ImageView imageView = (ImageView) row.findViewById(R.id.image);
final Button button = (Button) row.findViewById(R.id.button);
if (position == getCount() - 1) {
// The last element
imageView.setVisibility(View.GONE);
// set an OnClickListener on the button or whatever...
} else {
button.setVisibility(View.GONE);
// do your regular ImageView handling...
}
return row;
}
}

Related

How to set gridview within listview item and click on each grid item with uniq posion and click evant

I want to foll list view with image grid date wise.First get Date list of image and then after get image path list of particular date wise image list.and finally I fill list and grid of particular date wise image(No of images and date not fixed),have a look screen shot.
Date wise images display in Grid View
And now my problem is, When I set click listener on image and add check mark image on click then image add in two position, Why I don't know.and second is when I scroll down list view then check mark visible gone.
I want to select multiple image and add check mark image and perform operation like "Delete image" and "Share image"
Like this
I call grid adapter of each list item , look my code tell me what's wrong
Custom List View adapter : get View method
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder viewHolder = new ViewHolder();
DateSortedImageItem item = getItem(position);
if (row == null) {
row = mInflator.inflate(R.layout.row_date_sorted_image_list, parent, false);
viewHolder.tv_date = (TextView) row.findViewById(R.id.tv_row_date);
viewHolder.rowGridView = (GridView) row.findViewById(R.id.gv_row_item);
viewHolder.tv_date.setText(item.getDate());
row.setTag(viewHolder);
gridAdapter = new GridAdapter(context, R.layout.row_sorted_image_gridview_item,
item.getDateSortedImageList(), position);
viewHolder.rowGridView.setAdapter(gridAdapter);
} else {
row.getTag();
}
return row;
}
private class ViewHolder {
TextView tv_date;
GridView rowGridView;
}
and my grid adapter code is :
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = ((Activity) getContext()).getLayoutInflater();
convertView = mInflater.inflate(R.layout.row_sorted_image_gridview_item, parent, false);
}
view = convertView;
final SquareImageView imageView = (SquareImageView) view.findViewById(R.id.iv_test_row_grid_img);
final ImageView iv_checked = (ImageView) view.findViewById(R.id.iv_checked);
final ImageView iv_unChecked = (ImageView) view.findViewById(R.id.iv_unchecked);
view.setTag(arrList.get(position).toString());
File f = new File(arrList.get(position).toString());
System.out.println("arrList size >>" + arrList.size());
Picasso.with(context).load(Uri.fromFile(f)).resize(100, 100).centerCrop().placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_close_black).into(imageView);
I just set Grid View image. And now i set imageview click ivent and add check mark then add check mark event two places and scroll down then visible gone also.
What's wrong hear and how to set click event of image view unique.
Finally I want to design Look like Google "Photos" app.
Suggest me any example or library:
First you should create a POJO as below
public class AlbumItem {
private String headerStr;
private List<ChildItem> children;
//getter - setter....
public class ChildItem {
private String childName;
private boolean isSelected;
//getter - setter.....
}
}
Now use this POJO inside your main adapter.
and in the second adapter handle the selection logic means if isSelected is true then show iv_checked otherwise show iv_unchecked.
Please let me know if you need further explanation.
Hope it will help you.

Android listview adapter ...no redraw

I have the following code inside my adapter:
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.box_afisare, null);
}
final TextView titlu = (TextView) convertView.findViewById(R.id.titlu);
titlu.setText(Html.fromHtml(textt.get(position)));
titlu.setTextSize(TypedValue.COMPLEX_UNIT_SP,font);
final Integer pos = position;
titlu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (main_contextt.selectie.contains(id_post.get(pos)))
{
Toast.makeText(mContext," REMOVE ",Toast.LENGTH_LONG).show();
titlu.setBackgroundColor(Color.parseColor("#0077CC"));
}
else
{
main_contextt.selectie.add(id_post.get(pos));
titlu.setBackgroundColor(Color.parseColor("#404040"));
}
}
});
return convertView;
}
I manage to colorate the selected line or lines. But when i scroll the listview and those selected lines are no longer in view range of the phone....the background color disapear.
It disapear only if that line/lines is out of view. I think the adapter is redrawing?
What to do to remain the color set on the line/lines even after i scroll the listview?
thanks
ListViews recycle their child views. So if you have 20 items in your ListView but only enough room on the screen to show 4 at a time, the ListView will only have 4 children which it will recycle to show all 20 items. When one view scrolls out of view, it is recycled for the next view coming into view. See this answer for a great explanation.
What you need to do is tie the color to some underlying data. Then you would use code like.
titlu.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// set data.get(pos).color
}
});
titlu.titlu.setBackgroundColor(data.get(pos).color);
...something like that.

ListView Odd/Even Rows Refresh Automatically When Scrolling Down Or Selecting AnotherItem

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

How to change the image resource of a item clicked in a list view

I have a custom listview which has an ImageView on the left, followed by a EditText and on the right I have another ImageView which, at first, has not any source.
Then I have an onItemClickListener on this View. I would like to change the source of the ImageView on the right (the one that initially hasn't any source) on the item click.
For this I have implemented this code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView icone2 = (ImageView) view.findViewById(R.id.icone2_lugar);
icone2.setImageResource(R.drawable.flag_origem);
}
The ImageView is succefully inserted on the item clicked. However, when I scroll down the list, I realized that all the other elements which were at the same position of the one clicked before I scroll the list, also changed the image resource.
Why is it happening? I guess Android loads new views as scroll the list instead of loading all elements views at the same moment, so I guess I can't change the resource of a element in a list by taking the View parameter of onItemClick.
So how could I change the resource of the clicked element in a list view?
EDIT:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View linha = convertView;
ArmazenadorLugar armazenador = null;
if (linha == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linha = inflater.inflate(R.layout.linha_lugar, parent, false);
armazenador = new ArmazenadorLugar(linha);
linha.setTag(armazenador);
} else {
armazenador = (ArmazenadorLugar) linha.getTag();
}
armazenador.popularFormulario(mListaLugares.get(position), mContext);
return linha;
}
public LugarAndroid getItem(int position) {
return mListaLugares.get(position);
}
I ended up doing it in a different way. Don't know if it is the better but it works.
I created a variable (imgPosition) in my adapter which will hold the element position which has to set the image resource. Then, whenever I click onto an element, the listener calls
adapter.setImgPosition(position);
adapter.getView(position, view, parent);
I called the getView method in the listener to refresh the list, because getView calls a method (I'll explain it next) to update the list.
In the getView method of the adapter class I have the following logic (the position element comes from getView parameter):
holder.setValues(mListaLugares.get(position), mContext, imgPosition == position);
In the holder:
static class Holder{
private TextView txt1= null;
private ImageView img1 = null;
private ImageView img2 = null;
Holder(View linha) {
txt1= (TextView) linha.findViewById(R.id.txt1);
img1 = (ImageView) linha.findViewById(R.id.img1 );
img2 = (ImageView) linha.findViewById(R.id.img2);
}
void setValues(LugarAndroid lugar, Context mContext, boolean setImg) {
img2.setImageResource(0);
if(setImg) img2.setImageResource(R.drawable.flag);
// Logic to fill up the others views
}
This way I will only set the image resource of the element clicked when the boolean passed is true
If you are using an adapter with a List of Object, you can use :
getItem(position) and then change the image of the imageview of this specific object

Android ListView: getTag() returns null

Hallo all,
I have a ListView which contains a Button in each line. The following code is part of the getView() Method
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView tv;
Button saveA_button;
EditText edittext;
FITB_ViewWrapper wrapper;
if (row == null) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Training")) {
row = li.inflate(R.layout.exercise_for_training_fitb,parent, false);
}else {
row = li.inflate(R.layout.exercise_for_exam_fitb,parent, false);
}
wrapper=new FITB_ViewWrapper(row);
row.setTag(wrapper);
if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Exam")) {
saveA_button=wrapper.getSaveAnswer_Button();
OnClickListener l=new OnClickListener() {
#Override
public void onClick(View v) {
Integer mp=(Integer)v.getTag();
Log.i("mp","my Position is: "+mp);
}
};
saveA_button.setOnClickListener(l);
}
}else {
wrapper=(FITB_ViewWrapper) row.getTag();
}
For my App i need to known to which item the Button belongs to, so i try to detect it. The code
Log.i("mp","my Position is: "+mp);
puts out a message: mp myPosition is: null
I can't understand, why do i get a "null" but not an Integer? How can i find out the Position of an Item in a ListView?
Thanks a lot.
Log.i("mp","my Position is: "+position);
you have the position already !
public View getView(final int position, View convertView, ViewGroup parent) {
The Views in a ListView are reused as you scroll up and down. Thus, setting values in getView often has unintented consequences, like the image that you meant to set for item number 5 appearing in item number 10, 15 and 20 also.
To avoid this, if you want to set properties in getView you need to make sure you set or unset them for each view.
I'm not sure what exactly you are trying to accomplish with your code, but it might help to move the setTag outside of the if statement, to make sure you are setting it each time that a view is used.

Categories

Resources