android Button inside custom listview - android

I have developed an Android application using custom ListView. My custom ListView consists of Textview, ImageView and 2 ImageButtons. I want to change my ImageButton's resource when I click it. (example: from play to stop). More clearly, I attach my design below.
How can I control my ImageButton from Activity?
public class MyCustomBaseAdapterSurat extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
ViewHolder holder;
private StreamingMediaPlayer audioStreamer;
public MyCustomBaseAdapterSurat(Context context,
ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return searchArrayList.size();
}
#Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customlistsurat, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.play = (ImageButton) convertView
.findViewById(R.id.imageButton1);
holder.rep = (ImageButton) convertView
.findViewById(R.id.imageButton2);
holder.play.setOnClickListener(klikplay);
// holder.rep.setOnClickListener(klikrep);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.rep.setId(searchArrayList.get(position).getIdRep());
holder.play.setId(searchArrayList.get(position).getIdPlay());
holder.txtName.setText(searchArrayList.get(position).getName());
holder.iv.setBackgroundResource(searchArrayList.get(position)
.getDrawable());
return convertView;
}
static class ViewHolder {
TextView txtName;
ImageView iv;
ImageButton play;
ImageButton rep;
}
private OnClickListener klikplay = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.play.setBackgroundResource(searchArrayList.get(v.getId())
.getDrawable());
//startStreamingAudio();
}
};
}

Related

My images changing during scroll in listview android

In last week, i just try to resolve this problem. see every similar questions and i don't know why, solutions don't work for me.
I have a list view and each item has an image view. when clicked on image view , i want to change image resource. well. all done. but when scrolling, image resources change.
my full code is:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<String> items;
public CustomBaseAdapter(Context context,ArrayList<String> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
}
});
return vi;
}
}
where i have mistake?
you have to use one boolean value in holder to check whether your background image is set or not for particular row.
holder
private class ViewHolder{
TextView titr;
ImageView image;
boolean isSet;
}
set image
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
holder.isSet = true
}
});
if(holder.isSet)
{
holder.image.setBackgroundResource(R.drawable.fav_ic);
}
else{
holder.image.setBackgroundResource(no image or other image);
}
hope this will help.
try this,
public class CustomBaseAdapter extends BaseAdapter {
Context context;
ArrayList<Items> items;
public CustomBaseAdapter(Context context,ArrayList<Items> items){
this.context = context;
this.items = items;
}
private class ViewHolder{
TextView titr;
ImageView image;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Items getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.hashCode();
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position).getStrTxt());
holder.image.setImageResource(items.get(position).getDrawableImage());
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
items.set(position,new Items(R.drawable.fav_ic));
notifyDataSetChanged();
}
});
return vi;
}
}
Add Model Class :
public class Items {
int drawableImage;
String strTxt;
public Items(int drawableImage) {
this.drawableImage = drawableImage;
}
public int getDrawableImage() {
return drawableImage;
}
public void setDrawableImage(int drawableImage) {
this.drawableImage = drawableImage;
}
public String getStrTxt() {
return strTxt;
}
public void setStrTxt(String strTxt) {
this.strTxt = strTxt;
}
}
This is because when you scroll listview android system always create new row and destroy non-visible rows. You need to modify the getView() as below then everything will be fine :
#Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
final ViewHolder holder ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(view==null){
vi = inflater.inflate(R.layout.customlistitem,null);
holder = new ViewHolder();
holder.titr = (TextView) vi.findViewById(R.id.listtext);
holder.image = (ImageView) vi.findViewById(R.id.listimg);
holder.image.setTag(position);
vi.setTag(holder);
}
else{
holder = (ViewHolder) vi.getTag();
}
holder.titr.setText(items.get(position));
if(sp.getBoolean("position="+position, false)){
v.setBackgroundResource(R.drawable.fav_ic);
}
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.fav_ic);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("position="+position, true);
edit.commit();
}
});
return vi;
}
You should define layoutInflater in the constructor because getView() call for each row so if you define layoutInflater in getView() then LayoutInflater define again and again for each row.
Yes, this problem appears always in ListView because android always destroy and recreate rows in it.
You have two choices:
First, Modify your Adapter to save status for each row in the ListView and check this status before displaying the row.
Second one, I recommended this one and use it in my code, you can use RecyclerView, this problem will not happen with it.

How to change row view in listview when clicking on item

Is it possible to change row view when I'm clicking on it? For example I have listView with rows where some short information shown, and when after clicking on row detailed view will show instead of old row.
I tried to use addView in my listView, but "listview addView(View, int) is not supported in AdapterView" error appeared.
try something like that:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<UserPojo> mList;
public MyAdapter(Context context, List<UserPojo> users) {
this.mContext = context;
this.mList = users;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public UserPojo getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final UserPojo user = mList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_ad, null);
holder = new ViewHolder();
holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
holder.name = (TextView) convertView.findViewById(R.id.name_textView);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
if (user.isClicked) {
holder.pic.setVisibility(View.VISIBLE);
holder.name.setVisibility(View.GONE);
} else {
holder.pic.setVisibility(View.GONE);
holder.name.setVisibility(View.VISIBLE);
}
holder.name.setText("text");
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.setClicked(true);
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
CircleImageView pic;
TextView name;
}
}

ListView Button positions repeating

Well i have a listview populated with BaseAdapter. In listview i have 2 textviews and 2 buttons that i want to be clickable and i did set it class and works fine but problem is when i click one textview, 4th, 8th list item below is clicked too and i just find out that they have same position by printing position with toast.
How can i make that positions go ++ and every of them be uniqe?
Adapter Class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
protected ListView feedListView;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
}
public void addItem(final FeedItem item) {
listData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount()
{
return listData.size();
}
#Override
public Object getItem(int position)
{
return listData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public View getView( final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View row=convertView;
if (row == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
holder.approve.setTag(v);
holder.approve.setText("Approved");
}
});
return convertView;
}
static class ViewHolder
{
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
}
Listview recycles views.
How ListView's recycling mechanism works
Change getView
public View getView( final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
As you are reusing the view by
else{
holder = (ViewHolder) convertView.getTag();
}
so multiple textView using same listener. So Take your onclickListener outside of the if condition.
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
Add this code in your list adapter :
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return questionsClassArrayList_.size();
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}

Button onClick in ListView

Hi i have dynamic ListView with next form:
----------------------------
[TextView][Button]
----------------------------
I want to be able to receive OnClick Button; How can I do it:
Following method doesn't helped:
lv = (ListView)findViewById(R.id.listViewmain);
...
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("click on button", "click on button");
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
this one doesn't help too (moreover app is crashed):
Button b = (Button)findViewById(R.id.buttonTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
How can I do it?
If u have a button inside a listview and you need to capture onclick even on that button. you have to override base adapter.
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView button;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.button= (Button) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
// Tty this one
1. Add this class to your adapter.
static class ViewHolder {
TextView textview;
Button button;
}
2. Your getView() look like
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(yourActivityName.this).inflate(R.layout.yourListItemXml, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.textview);
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText("textview value");
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// write your start another activity code here
}
});
}
Create a custome adapter as follows in your activity
static class ViewHolder {
public TextView text;
public Button button;
}
private class myAdapter extends BaseAdapter {
LayoutInflater inflater;
public ApproversListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myarrayList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text = (TextView) convertView
.findViewById(R.id.listitem_text);
holder.button = (Button) convertView
.findViewById(R.id.listitem_button);
holder.text.setText("Your String Here");
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//your code to be executed on button click
}
});
return convertView;
}
}
Now create a an object of myAdapter and use that as your list adapter.
Hope it helped

Modify a textview value in a custom listview after clicking

This listview has an adapter that is linked to a layout for each row (itemrow.xml)
The price value (500-250 300) is a textview
How can we access to it, to modify it, once clicked on a button ?
lllllllllllllllllllllllllllllllll
Android
Price: 500
lllllllllllllllllllllllllllllllll
Php
Price: 250
lllllllllllllllllllllllllllllllll
C++
Price: 300
lllllllllllllllllllllllllllllllll
public class CommandeAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<Commande> listCommande;
public CommandeAdapter(Context context, List<Commande> listCommande) {
this.context = context;
this.listCommande = listCommande;
}
public int getCount() {
return listCommande.size();
}
public Object getItem(int position) {
return listCommande.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
Commande entry = listCommande.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_row, null);
}
TextView tvNom = (TextView) convertView.findViewById(R.id.textView1);
tvNom.setText(entry.getNom());
TextView tvPrixU = (TextView) convertView.findViewById(R.id.textView2);
tvPrixU.setText(entry.getPrixU());
TextView tvqte = (TextView) convertView.findViewById(R.id.textView4);
tvqte.setText(entry.getQte());
TextView tvSomme = (TextView) convertView.findViewById(R.id.textView5);
tvSomme.setText(String.format("%.03f", entry.getSomme()));
return convertView;
}
#Override
public void onClick(View view) {
Commande entry = (Commande) view.getTag();
listCommande.remove(entry);
// listCommande.remove(view.getId());
notifyDataSetChanged();
}
private void showDialog(Commande entry) {
// Create and show your dialog
// Depending on the Dialogs button clicks delete it or do nothing
}
}

Categories

Resources