Modify a textview value in a custom listview after clicking - android

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

Related

Because textview product counter is repeated in grid view using BaseAdapter

Because it repeats the product counter which is a TextView; I have the following scenario:
The user selects the product, and applies the quantity.
From the Activity and not from the Adapter I show the quantity of the selected product as seen in the image.
Everything works perfect, until under the screen and the counter magically passes to another product, I have implemented these methods by suggestion:
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return position;
}
and still I still get this error. I have also tried to modify the counter from the Adapter but the same thing happens. I want that counter to stay fixed and not update when under the screen.
Adapter:
public class ProductoAdapter extends BaseAdapter
{
private Activity activity;
private LayoutInflater inflater;
private ArrayList<Producto> listadoProductos;
private ArrayList<Producto> productosPedidos;
Producto producto;
Empresas pedido;
public final int TYPE_NOTICIA=0;
public final int TYPE_LOAD=1;
private gestionSharedPreferences sharedPreferences;
private Context context;
/*
OnLoadMoreListener loadMoreListener;
*/
boolean isLoading=false, isMoreDataAvailable=true;
vars vars;
public static int contador;
public static int i;
public static int contadorGeneral;
private NumberFormat numberFormat;
ImageLoader imageLoader = ControllerSingleton.getInstance().getImageLoader();
public ProductoAdapter(Activity activity, ArrayList<Producto> listadoProductos)
{
this.activity=activity;
this.listadoProductos=listadoProductos;
productosPedidos=new ArrayList<Producto>();
vars=new vars();
sharedPreferences=new gestionSharedPreferences(this.activity);
contador=0;
contadorGeneral=0;
producto=new Producto();
/* LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
new IntentFilter("custom-message"));*/
}
/* public BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Get extra data included in the Intent
producto = (Producto) intent.getSerializableExtra("producto");
Toast.makeText(context, producto.getNombreProducto()+producto.getNumeroDeProducto() ,Toast.LENGTH_SHORT).show();
}
};*/
#Override
public int getCount()
{
return listadoProductos.size();
}
#Override
public Producto getItem(int position)
{
return listadoProductos.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
numberFormat = NumberFormat.getNumberInstance(Locale.GERMAN);
if (view == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);
Log.i("martin","null");
}
else
{
Log.i("martin","llenoooooooooo");
}
final Producto producto = getItem(position);
ImageView imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);
TextView nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
TextView cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
TextView precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
TextView precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
TextView ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);
if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
{
imagenProducto.setImageResource(R.drawable.ic_not_image_found);
}
else
{
Glide.with(activity).
load(producto.getImagenProducto().toString()).
thumbnail(0.5f).into(imagenProducto);
}
nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());
//cantidadProducto.setText(producto.getCantidadProducto());
precioGeneralProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioGeneralProducto())));
precioGeneralProducto.setPaintFlags(precioGeneralProducto.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
precioPideProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioPideProducto())));
int valorahorro=(Integer.parseInt(producto.getPrecioGeneralProducto()))-(Integer.parseInt(producto.getPrecioPideProducto()));
ahorroProducto.setText(""+"$"+numberFormat.format(Double.parseDouble(""+valorahorro)));
return view;
}
}
Activity:
I use a TextView as a counter which is initially invisible, but when it is selected, it is visible with the quantity of product selected.
From the activity I use this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Producto producto = (Producto) parent.getItemAtPosition(position);
final View contadorImagenProductoBolsa = Pedidos.this.findViewById(R.id.contadorProductoImagenBolsa);
TextView test1TextView = (TextView) view.findViewById(R.id.contadorProductoImagenBolsa);
mostrarDialogoDetalle(test1TextView,position,producto.getIdProducto(),producto.getImagenProducto(), producto.getNombreProducto(),
producto.getCantidadProducto(),producto.getUnidadProducto(), producto.getPrecioGeneralProducto(),producto.getPrecioPideProducto(),
producto.getAhorroProducto());
}
Has anyone had this happen? how did they solve it?
Here it is perfect, but when I go under the screen and I go back and up, observe how the counter happens to another product inexplicably:
I want an effective help and a possible snippet if it is the case of an incorrect adapter or activity.
Thank you.
I think You have to add ViewHolder For ProductoAdapter Like:
class ViewHolder
{
TextView nombreProducto,cantidadProducto
,precioGeneralProducto,precioPideProducto,ahorroProducto;
ImageView imagenProducto;
public ViewHolder(View view){
imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);
nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);
}
}
Use setTag and getTag For ViewHolder Like:
ViewHolder holder;
if (view == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);
holder = new ViewHolder();
Log.i("martin","null");
}
else
{ holder = (ViewHolder) view.getTag();
Log.i("martin","llenoooooooooo");
}
final Producto producto = getItem(position);
if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
{
holder.imagenProducto.setImageResource(R.drawable.ic_not_image_found);
}
else
{
Glide.with(activity).
load(producto.getImagenProducto().toString()).
thumbnail(0.5f).into(holder.imagenProducto);
}
holder.nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());
.
.
.
view.setTag(holder);
return view;
i hope this is help you

ListView setOnItemClicklistener NullPointerException

public class CustomAdapter extends ArrayAdapter<Plan> implements View.OnClickListener {
private ArrayList<Plan> planArrayList;
int position;
Context context;
private static class ViewHolder{
TextView workType;
TextView zone;
TextView division;
TextView station;
TextView Msg;
}
public CustomAdapter(ArrayList<Plan> plan,Context context){
super(context,R.layout.plan_list,plan);
this.planArrayList=plan;
this.context=context;
}
#Override
public void onClick(View v) {
}
private int lastPosition = -1;
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Plan plans = getItem(position);
ViewHolder viewHolder;
final View result;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
viewHolder = new ViewHolder();
//LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.plan_list,parent,false);
viewHolder.workType = (TextView) convertView.findViewById(R.id.plan_worktype);
viewHolder.zone = (TextView) convertView.findViewById(R.id.plan_zone);
viewHolder.division = (TextView) convertView.findViewById(R.id.plan_division);
viewHolder.station = (TextView) convertView.findViewById(R.id.plan_station);
viewHolder.Msg = (TextView) convertView.findViewById(R.id.)
result=convertView;
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
Plan plan = planArrayList.get(position);
lastPosition = position;
viewHolder.workType.setText(plans.getWorkType());
viewHolder.zone.setText(plans.getZone());
viewHolder.division.setText(plans.getDiv());
viewHolder.station.setText(plans.getSTA());
viewHolder.workType.setText(plan.getWorkType());
viewHolder.zone.setText(plan.getZone());
viewHolder.division.setText(plan.getDiv());
viewHolder.station.setText(plan.getSTA());
if (this.position == position){
View view2 = inflater.inflate(R.layout.list_dialog,null);
TextView worktype= (TextView) view2.findViewById(R.id.dlg_worktype);
TextView zone = (TextView) view2.findViewById(R.id.dlg_zone);
TextView div = (TextView) view2.findViewById(R.id.dlg_division);
TextView sta = (TextView) view2.findViewById(R.id.dlg_station);
TextView msg = (TextView) view2.findViewById(R.id.dlg_msg);
worktype.setText(plan.getWorkType());
zone.setText(plan.getZone());
div.setText(plan.getDiv());
sta.setText(plan.getSTA());
msg.setText(plan.getMsg());
return view2;
}
return convertView;
}
public void selectedItem(int position){
this.position=position;
}
}
When I called setOnItemClickListener on listView item those item values need to display in dialog box.
By the above code I'm getting java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.example.nsurekha.entry_ex.CustomAdapter$ViewHolder.workType' on a null object reference
If you want to use onClick on listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String title=adapter.getItem(arg2).getTitle();
String description=adapter.getItem(arg2).getDescription();
String addedby=adapter.getItem(arg2).getAddedby();
}
});
Now you can easily show dialogbox with the desired data. Hope it works for you.

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

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

Getting object out of a ListView with a CustomAdapter 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;
}
}

Categories

Resources