There is a sample baseadapter as follows and it uses a layout xml file names adapter_layout. But I do not want to use the adapter_layout.xml. I want to identify with all components(TextView, EditText ...) in the class. How should the BaseAdapter coding programmatically without adapter_layout.xml?
public class LayoutAdapter extends BaseAdapter {
private List<String> objects = new ArrayList<String>();
private Context context;
private LayoutInflater layoutInflater;
public LayoutAdapter(Context context) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return objects.size();
}
#Override
public String getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.adapter_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.editText1 = (EditText) convertView.findViewById(R.id.editText1);
convertView.setTag(viewHolder);
}
return convertView;
}
protected class ViewHolder {
private TextView textView2;
private TextView textView1;
private EditText editText1;
}
}
Related
I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#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.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}
Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));
This is my BaseAdapater class in which I am using ViewHolder.
public class CategoryAdapter extends BaseAdapter {
private Context mContext;
public int[] mThumbIds = {R.drawable.shoes,R.drawable.dress,R.drawable.purse,
R.drawable.shoes,R.drawable.dress,R.drawable.purse ,
R.drawable.shoes,R.drawable.dress,R.drawable.purse };
public WishbookCategoryAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return (long)position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.wishbook_gridview_items, null);
holder.Category = (TextView)convertView.findViewById(R.id.wishbook_grid_category);
holder.items = (TextView)convertView.findViewById(R.id.wishbook_grid_category_number);
holder.iv = (ImageView)convertView.findViewById(R.id.wishbook_category_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.iv.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
holder.iv.setScaleType(ScaleType.FIT_XY);
holder.Category.setText("Category");
holder.items.setText("items");
return convertView;
}
private static class ViewHolder {
protected ImageView iv;
protected TextView Category;
protected TextView items;
}
}
i have fragment in which i have a listview , i was trying to show some data in the list . it is silly but its not working i have no idea what is wrong with it.
in my fragmnet in oncreateview im doing this
mContactsHomeAdapter = new ContactsHomeAdapter(getActivity());
mListView.setAdapter(mContactsHomeAdapter);
mContactsHomeAdapter.notifyDataSetChanged();
this is my adapter code
public class ContactsHomeAdapter extends BaseAdapter {
ExecutorService executorService;
private Context mContext;
LayoutInflater mInflater;
public ContactsHomeAdapter(Context pContext) {
mContext = pContext;
//executorService=Executors.newFixedThreadPool(10);
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return 100;
}
#Override
public Object getItem(int position) {
return null;
}
#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.contacts_adapter_item, null);
}
CheckBox lCheckbox = (CheckBox) convertView.findViewById(R.id.check_box);
TextView lContactTitle = (TextView) convertView.findViewById(R.id.contact_title);
TextView lContactEmail = (TextView) convertView.findViewById(R.id.contact_email);
ImageView lContactPhoto = (ImageView) convertView.findViewById(R.id.contact_photo);
TextView lContactNumber = (TextView) convertView.findViewById(R.id.contact_phonenumber);
lContactTitle.setText("hello");
// QueueItem lItem = new QueueItem() ;
// lItem.mId = ""+position;
// lItem.mName = lContactTitle;
// lItem.mNumber = lContactNumber;
// lItem.mEmail = lContactEmail;
// executorService.submit(new QueueRunner(lItem));
return convertView;
}
}
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();
}
};
}
I've got a custom Adapter like this:
public class DeviceAdapter extends BaseAdapter {
private static ArrayList<Device> availableDevices;
Context c;
private LayoutInflater mInflater;
/**
* Constructor for this class
* #param context Which context triggered this class
* #param devices Object of each devices
*/
public DeviceAdapter(Context context, ArrayList<Device> devices) {
this.availableDevices = devices;
this.c = context;
mInflater = LayoutInflater.from(c);
}
public int getCount() {
return availableDevices.size();
}
public Object getItem(int position) {
return availableDevices.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.device_list, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.address = (TextView) convertView.findViewById(R.id.address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText("Device name: "+availableDevices.get(position).getName());
holder.address.setText("Mac-address: "+ availableDevices.get(position).getAddress());
return convertView;
}
static class ViewHolder {
TextView name;
TextView address;
}
}
My question is: How can I access an objects variable name where the user presses?
use onItemClickListenerwith list http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
ist.setOnItemClickListener(new AdapterView.onItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Object listItem = list.getItemAtPosition(position);
or
TextView tv = (TextView) view.findViewById(R.id.name);
name = tv.getText();
}
});
You can get OnClick event for only name TextView click, by adding holder.name.setOnClickListener.......
See followin code
public class DeviceAdapter extends BaseAdapter {
private static ArrayList<Device> availableDevices;
Context c;
private LayoutInflater mInflater;
/**
* Constructor for this class
* #param context Which context triggered this class
* #param devices Object of each devices
*/
public DeviceAdapter(Context context, ArrayList<Device> devices) {
this.availableDevices = devices;
this.c = context;
mInflater = LayoutInflater.from(c);
}
public int getCount() {
return availableDevices.size();
}
public Object getItem(int position) {
return availableDevices.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.device_list, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.address = (TextView) convertView.findViewById(R.id.address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText("Device name: "+availableDevices.get(position).getName());
holder.address.setText("Mac-address: "+ availableDevices.get(position).getAddress());
// PUT FOLLOWIN ------------------
holder.name.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
put your dezired code here which runs OnClick of TextView NAME
}
});
// -----------------
return convertView;
}
static class ViewHolder {
TextView name;
TextView address;
}
}