Getting object out of a ListView with a CustomAdapter android - android

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

Related

DIsplay first letter of string to another textview in Android

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

How to set visibility true of one button onclick of another button in listView - android

I want to set the visibility to true of btnProductDetailMinus to true on click of btnProductDetailAddToCart. This is my code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
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.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utility.displayToast("wonderful");
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}
});
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}
But its giving error "Variable holder is accessed from within inner class. needs to declared final"
This is the line giving error.
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
How to fix this?
Assign holder tot a final variabele:
final ViewHolder fHolder = holder;
Then in onClick:
fHolder.btnProductDetailMinus.setVisibility(View.VISIBLE);
Move this line out of the if (convertView == null)
holder = new ViewHolder();
You can declare final ViewHolder holder = null; instead of ViewHolder holder = null;
You can just make it a Global variable.
set public static class ViewHolder instead of private class ViewHolder
One of the approach that I follow is create a Map or List in Adapter class and onClick set value and that collection, and in adapter getView method check for the collection value. .
Checkout the below code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
private Map<Integer, Boolean> visibilityMap = new HashMap<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
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.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (int)v.getTag();
Utility.displayToast("wonderful");
visibilityMap.put(position, true);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnProductDetailAddToCart.setTag(position);
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
if(visibilityMap.containsKey(position) && visibilityMap.get(position)){
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}else{
holder.btnProductDetailMinus.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}

How should the BaseAdapter coding programmatically without a layout file?

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

android fragment baseadapter getcount is getting called but not getview

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

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