Button onClick in ListView - android

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

Related

Android custom listadapter always deleting the last item

Good day,
I am trying to refresh my knowledge in android programming
by creating a simple memo app with custom adapter list.
My problem is, even I call "NotifyDataSetChanged();",
the value deleted in my list is always the last one.
Though when I refresh it, it will display the correct one.
Here is my code :
public class CustomAdapter extends BaseAdapter{
List<Memo> memos;
Context context;
int [] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
}
return convertView;
}
}
Hoping someone can lighten me up why this code is not working.
Thank you!
Try this code.
public class CustomAdapter extends BaseAdapter {
List<Memo> memos;
Context context;
int[] imageId;
private LayoutInflater inflater = null;
public CustomAdapter(Context mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return memos.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
convertView = inflater.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (Holder) convertView.getTag();
}
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}

Gridview custom adapter button onclick

My gridview custom item is a image button.I want to change image button image when the image button is clicked.You can see below my custom adapter class getView method.But image button image does not change.
ImageButton btn1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myItem, null);
btn1 = (ImageButton) view.findViewById(R.id.btn1);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn1.setImageResource(R.id.image1);
}
});
return view;
}
Try this way,hope this will help you to solve your problem.
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Integer>> imagesList;
public CustomGridAdapter(Context context,ArrayList<HashMap<String,Integer>> imagesLis) {
this.context =context;
this.imagesList =imagesLis;
}
#Override
public int getCount() {
return imagesList.size();
}
#Override
public Object getItem(int position) {
return imagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.myItem, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setImageResource(imagesList.get(position).get("normalImage"));
holder.btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ImageButton)v).setImageResource(imagesList.get(position).get("selectedImage"));
}
});
return convertView;
}
static class ViewHolder {
ImageButton btn1;
}
}

how to redirect to different Activity in android on item click

Here is My code for item Click and print data in list view .
listView = (ListView) layout.findViewById(R.id.listView1);
Notifcationadapter adapter = new Notifcationadapter(this, notifies);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View parentView = (View) view.getParent();
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
String value = value1.getText().toString();
if (value.equals("Post"))
Toast.makeText(this, "Post", 1000).show();
if (value.equals("Request For travelling"))
Toast.makeText(this, "Request for travelling", 1000).show();
}
Here is my adapter :
public class Notifcationadapter extends BaseAdapter {
private static ArrayList<Notify> values;
Context context;
// List<RowItem> rowItems;
ViewHolder holder = null;
LinearLayout linear2;
DataBaseManager dbManager = new DataBaseManager(context);
ArrayList<Notify> notifies;
public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
super();
this.context = context;
this.notifies = notifies;
}
/* private view holder class */
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
ImageView yesimage;
ImageView noimage;
TextView revresetime;
LinearLayout linear2;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.notifcationlistitem, null);
holder = new ViewHolder();
holder.linear2 = (LinearLayout) convertView.findViewById(R.id.linear2);
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.yesimage = (ImageView) convertView.findViewById(R.id.imageView1);
holder.noimage = (ImageView) convertView.findViewById(R.id.imageView2);
holder.revresetime = (TextView) convertView.findViewById(R.id.revresetime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.txtTitle.setText("Title");
String statustype = notifies.get(position).getNotificationType();
if (statustype.equals("1")) {
holder.txtDesc.setText(notifies.get(position).getNotificationDescrpiton());
holder.revresetime.setText(Functions.setLastSeenNotifcationTime(notifies.get(position).getNotificationDatetime()));
holder.txtTitle.setText("Request For travelling");
holder.yesimage.setVisibility(View.VISIBLE);
holder.noimage.setVisibility(View.VISIBLE);
} else {
holder.txtDesc.setText(notifies.get(position).getNotificationDescrpiton());
holder.txtTitle.setText("Post");
holder.revresetime.setText(Functions.setLastSeenNotifcationTime(notifies.get(position).getNotificationDatetime()));
holder.yesimage.setVisibility(View.GONE);
holder.noimage.setVisibility(View.GONE);
}
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
} else {
convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
}
holder.yesimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(context, "Yes", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
holder.noimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "NO", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
return convertView;
}
#Override
public int getCount() {
return notifies.size();
}
#Override
public Object getItem(int position) {
return notifies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
I am displaying data in List view i wan i want to apply on item click function so that if text view list display post in list then its item click it should Print post if it Request for traveling then it should show Request for travel but i am unable to do this please help where am doing wrong .
you are getting the same value because of this line
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
to get the String value in your adapter or arraylist you can do something like this.
i assume you have arraylist of objects
//you arraylist contains object/class
String value1 = notifies.get(position).getTitle();
and using the adapter
String value1 = adapter.getItem(position);
then #Override the getItem() function in your adapter
#Override
public String getItem(int position){
return notifies.get(position).getTitle();
}
then to move to other activity according to your if statement
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View parentView = (View) view.getParent();
String value = notifies.get(position).getTitle();
Intent i = null;
if (value.equals("Post")){
i = new Intent(context,PostActivity.class);
}
else if (value.equals("Request For travelling")){
i = new Intent(context,RequestActivity.class);
}
if(i !=null){
startActivity(i);
}
}
I think you should use int position of onItemClick.
and then use if(notifies.get(position).getNotificationType() == 1)
it will be faster and should effectively work also.
First remove the onitemclick listener from activity. and use this code -
listView.setOnItemClickListener(this); // remove this line
as well as remove onitem click-
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
View parentView = (View) view.getParent();
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
String value = value1.getText().toString();
if (value.equals("Post")) {
Toast.makeText(this, "Post", 1000).show();
}
if (value.equals("Request For travelling")) {
Toast.makeText(this, "Request for travelling", 1000).show();
}
}
**just add convertview click listener**
public class Notifcationadapter extends BaseAdapter {
private static ArrayList<Notify> values;
Context context;
// List<RowItem> rowItems;
ViewHolder holder = null;
LinearLayout linear2;
DataBaseManager dbManager = new DataBaseManager(context);
ArrayList<Notify> notifies;
public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
super();
this.context = context;
this.notifies = notifies;
}
/* private view holder class */
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
ImageView yesimage;
ImageView noimage;
TextView revresetime;
LinearLayout linear2;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.notifcationlistitem, null);
holder = new ViewHolder();
holder.linear2 = (LinearLayout) convertView
.findViewById(R.id.linear2);
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.yesimage = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.noimage = (ImageView) convertView
.findViewById(R.id.imageView2);
holder.revresetime = (TextView) convertView
.findViewById(R.id.revresetime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.txtTitle.setText("Title");
String statustype = notifies.get(position).getNotificationType();
if (statustype.equals("1")) {
holder.txtDesc.setText(notifies.get(position)
.getNotificationDescrpiton());
holder.revresetime.setText(Functions
.setLastSeenNotifcationTime(notifies.get(position)
.getNotificationDatetime()));
holder.txtTitle.setText("Request For travelling");
holder.yesimage.setVisibility(View.VISIBLE);
holder.noimage.setVisibility(View.VISIBLE);
}
else {
holder.txtDesc.setText(notifies.get(position)
.getNotificationDescrpiton());
holder.txtTitle.setText("Post");
holder.revresetime.setText(Functions
.setLastSeenNotifcationTime(notifies.get(position)
.getNotificationDatetime()));
holder.yesimage.setVisibility(View.GONE);
holder.noimage.setVisibility(View.GONE);
}
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
}
else {
convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
}
holder.yesimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "Yes", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
holder.noimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "NO", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// write your code to redirect to another activity like this way
Intent i = new Intent(context,
Youractivity.class);
context.startActivity(i);
}
});
return convertView;
}
#Override
public int getCount() {
return notifies.size();
}
#Override
public Object getItem(int position) {
return notifies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}

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;
}

android Button inside custom listview

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();
}
};
}

Categories

Resources