Skip to content

Instantly share code, notes, and snippets.

@edutrul
Last active February 17, 2016 00:44
Show Gist options
  • Save edutrul/fe7c7dbc7b6554141a7d to your computer and use it in GitHub Desktop.
Save edutrul/fe7c7dbc7b6554141a7d to your computer and use it in GitHub Desktop.
CUSTOM ADAPTER: IMAGE + TEXTVIEW
package com.example.autonoma.myapplication;
/**
* Created by Autonoma on 16/02/2016.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result = prgmNameList;
context = mainActivity;
imageId = prgmImages;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.programa_listado, null);
holder.tv = (TextView) rowView.findViewById(R.id.textView);
holder.img = (ImageView) rowView.findViewById(R.id.imageView);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment