Making Object from getItem() and accessing the fields - java

I have a list listview in my code that contains 2 buttons and 2 TextViews. I want to access the text of each textView by Clicking on buttons. here is the code of the adapter...
public class TracksAdapter extends SimpleAdapter {
public TracksAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TracksAdapter proxy = this;
final View trackView = super.getView(position, convertView, parent);
Button dl = (Button) trackView.findViewById(R.id.dl);
dl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something with data : proxy.getItem(position)
TextView tv = (TextView) trackView.findViewById(R.id.track);
String track = tv.getText().toString();
Toast.makeText(getApplicationContext() , track , Toast.LENGTH_LONG);
}
});
Button play = (Button) trackView.findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something with data : proxy.getItem(position)
Toast.makeText(getApplicationContext() , String.valueOf(proxy.getItem(position)).indexOf("file=") , Toast.LENGTH_LONG);
}
});
return trackView;
}
}
the result of proxy.getItem(position) is something like {id=4 , name=foo , track=fofoo}
what should I do to access 'foo' ????

Try his code to get the text from onClick:
TextView tv = (TextView) parent.findViewById(R.id.your_textView_position_id_from_xml);
String s = tv.getText().toString();

Related

How to make 2 buttons for multiple listview elements

I'm creating an app for ordering food, and I created a Dish class and a Dish array adapter.
Currently you can add quantity for a specific dish by clicking on the view.
However, I wish to have 2 buttons to add or sub the quantity.
How can I make the 2 buttons for each dish, without the need to write for each element its ows button code? is there a way to make an "add" and "sub" methods and that the listview will know on which view it was clicked and by that update its quantity?
public Dish(String dishName, int dishPrice, int Image, int quantity) {
mdishName = dishName;
mdishPrice = dishPrice;
mdishPic = Image;
mquantity = quantity;
}
public class DishAdapter extends ArrayAdapter<Dish> {
public DishAdapter(Activity context, ArrayList<Dish> dishes){
super(context, 0, dishes);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_layout, parent, false);
}
final Dish currenDish = getItem(position);
TextView dishName = (TextView) listItemView.findViewById(R.id.dishName);
dishName.setText(currenDish.getDishName());
TextView dishPrice = (TextView) listItemView.findViewById(R.id.dishPrice);
dishPrice.setText(String.valueOf(currenDish.getDishPrice()));
ImageView image = (ImageView) listItemView.findViewById(R.id.dishPic);
image.setImageResource(currenDish.getDishPic());
image.setVisibility(View.VISIBLE);
TextView quantity = (TextView) listItemView.findViewById(R.id.quantity);
quantity.setText(String.valueOf(currenDish.getQuantity()));
return listItemView;
}
public class DesertsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deserts_activity);
final ArrayList<Dish> dishes = new ArrayList<Dish>();
dishes.add(new Dish("Number Cake",180, R.drawable.cake_number, 0));
dishes.add(new Dish("Ear of Haman", 40, R.drawable.ozen_haman, 0));
dishes.add(new Dish("Alphachores", 35, R.drawable.alphachores, 0));
dishes.add(new Dish("Snow Cookies", 35, R.drawable.snow_cookies, 0));
DishAdapter adapter = new DishAdapter(this, dishes);
final ListView listView = (ListView) findViewById(R.id.deserts_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
dishes.set(position, new Dish(dishes.get(position).getDishName(),
dishes.get(position).getDishPrice(), dishes.get(position).getDishPic(),
dishes.get(position).getQuantity()+1));
TextView quantity = (TextView) view.findViewById(R.id.quantity);
quantity.setText(String.valueOf(dishes.get(position).getQuantity()));
}
});
}
}
You have to create a separate Adaptor Class and then apply button click listeners on the buttons. I am attaching the image for the reference.
and My product Adaptor Is
public class ProductAdaptor extends RecyclerView.Adapter<ProductAdaptor.ViewHolder> {
List<ProductsItem> productsItems;
Context context;
public ProductAdaptor(Context context, List<ProductsItem> productsItems)
{
this.context = context;
this.productsItems = productsItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.single_product_design,viewGroup, false);
return new ProductAdaptor.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
final ProductsItem productsItem = productsItems.get(i);
viewHolder.productname.setText(productsItem.getProductName());
Glide.with(context).load(productsItem.getProductImageUrl()).into(viewHolder.productImage);
viewHolder.stock.setText(productsItem.getStock());
viewHolder.farmername.setText(productsItem.getFarmerName());
// add listener on single Item
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
viewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// apply code to increment the number
// first of all get the value form text counter and increment after that bind on the UI
}
});
viewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// first of all get the value form text counter and decrement after that bind on the UI
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return productsItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView productname;
ImageView productImage;
TextView stock, farmername, specility,qty;
Button btnPlus, btnMinus;
LinearLayout btnAdd;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
productname = itemView.findViewById(R.id.productname);
productImage = itemView.findViewById(R.id.productimage);
stock = itemView.findViewById(R.id.stcokValue);
farmername = itemView.findViewById(R.id.farmerName);
qty = itemView.findViewById(R.id.qty);
specility = itemView.findViewById(R.id.specility);
btnPlus = itemView.findViewById(R.id.plusbutton);
btnMinus = itemView.findViewById(R.id.minusbtn);
btnAdd = itemView.findViewById(R.id.add_button);
}
}
}

switch state is not stable in listview when scrolling

I'am new to android and i use sample for my listview. because of that i can't change the code. i have one switch button in each item of my Listview. when i scroll the Listview switches change state randomly. how can i make switch state stable?
my listview class adapter:
public class MyCustomCursorAdapter extends CursorAdapter {
LIGHTS calling_activity;
private DatabaseHelper dbHelper;
public MyCustomCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
this.calling_activity = (LIGHTS) context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.adapter,parent,false);
}
#Override
public void bindView(final View view, Context context, Cursor cursor) {
((TextView)view.findViewById(R.id.id)).setText(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
((TextView)view.findViewById(R.id.KEYCODE)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.TITLE)));
((TextView)view.findViewById(R.id.NAME)).setText(cursor.getString(cursor.getColumnIndex(dbHelper.DESC)));
ImageView option = view.findViewById(R.id.itemoption);
Switch thisswitch = view.findViewById(R.id.onoff);
thisswitch.setTag(cursor.getString(cursor.getColumnIndex(dbHelper._ID)));
thisswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { calling_activity.myOnCheckedChangedHandler((String)buttonView.getTag(),isChecked);
}
});
option.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView itemID = (TextView) view.findViewById(R.id.id);
TextView itemTitle = (TextView) view.findViewById(R.id.KEYCODE);
TextView itemDesc = (TextView) view.findViewById(R.id.NAME);
String myId = itemID.getText().toString();
String myTitle = itemTitle.getText().toString();
String myDesc = itemDesc.getText().toString();
Intent intent = new Intent(calling_activity, ModifyActivity.class);
intent.putExtra("Id", myId);
intent.putExtra("Title", myTitle);
intent.putExtra("Desc", myDesc);
calling_activity.startActivity(intent);
}
});
}
}
and in my Lights activity :
#Override
public void myOnCheckedChangedHandler(String id, boolean check_status) {
Toast.makeText(
this,
"You changed the status for the row with an id of " + id +
" the status is now " + new Boolean(check_status).toString(),
Toast.LENGTH_SHORT).show();
String name = cursor.getString(cursor.getColumnIndex(dbHelper.DESC));
if(new Boolean(check_status).toString().equals("true")){
Toast.makeText(this,name +" is ON", Toast.LENGTH_SHORT).show();
}
}
Please manage through if/else case inside bindView at adapter, You need to check the switch condition like below :
// set the code into bind
if(switchCondition1 == 1)
{
//set the code as per condition wise
}else{
//
}
After change the state you also need to change the state into your list item and use notifydatasetChanged() method to refresh the list items.

update the text of a listView row

I want my listView to be updated after clicking on a row (or any event, but let's focus on click).
I did something, but it updates more than one row (maybe it updates the first visible row and the one after the last visible...).
Here is the full code
Activity code
DatabaseHandler colisageBase;
ListView listView;
List<Site> sites;
String id_tournee;
SiteAdapter siteAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_site_choice);
Intent intent = getIntent();
id_tournee = intent.getStringExtra("idTourneeSelectionnee");
this.listView = findViewById(R.id.list_view_site);
this.colisageBase = new DatabaseHandler(this);
sites = colisageBase.selectAllSite(id_tournee);
siteAdapter = new SiteAdapter(SiteChoiceActivity.this, sites);
listView.setAdapter(siteAdapter);
colisageBase.closeDB();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Site selectedSite = sites.get(position);
selectedSite.setIsBarred(true);
sites.set(position, selectedSite);
siteAdapter.notifyDataSetChanged();
//goToOperationActivity(selectedSite.SiteOut());
}
});
Adapter code
public class SiteAdapter extends ArrayAdapter<Site> {
public SiteAdapter(Context context, List<Site> sites) {
super(context, 0, sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_site,parent, false);
}
SiteViewHolder viewHolder = (SiteViewHolder) convertView.getTag();
if(viewHolder == null){
viewHolder = new SiteViewHolder();
viewHolder.heure_supposee = convertView.findViewById(R.id.heure_supposee);
viewHolder.libelle_site = convertView.findViewById(R.id.libelle_site);
viewHolder.logo_telephone = convertView.findViewById(R.id.logo_phone);
convertView.setTag(viewHolder);
}
Site site = getItem(position);
viewHolder.heure_supposee.setText(site.getHeure_supposee());
viewHolder.libelle_site.setText(site.getLibelle_site());
viewHolder.logo_telephone.setVisibility(View.INVISIBLE);
if (site.getSur_appel().equals("O")) viewHolder.logo_telephone.setVisibility(View.VISIBLE);
if (site.isBarred()) viewHolder.libelle_site.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
return convertView;
}
#Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
private class SiteViewHolder{
public TextView heure_supposee;
public TextView libelle_site;
public ImageView logo_telephone;
}
}
Please suggest what's wrong with the code.
The answer was given in the comments by I_A_Mok, but i have to add more details:
In the case of a cell, when you do an action in an "if" condition , you usually have to do the opposite in an "else" condition.
In my case, after my condition where I strike through text, I had to add an else condition where I don't strike through text.
if (site.isBarred()){
viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else {
viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}

ListView Updating a single row

I want to update a single row in my listview with different content once i press a button. I know i can use notifydatasetchanged() but that would update the whole listView.
I have read this answer and it fits perfectly for what I want to do.
I have done a listview with 5 rows and when I press the button I want to update the 4th row with a different text. I dont want to set the text programatically since this is just a dummy project just to see if refreshing a single row is possible and my real project is much more complex than just a textview.
So my question is: can i use getView() to update a single row in a listview?
Here is my code:
my Activity:
public class MainActivity extends Activity {
public ListView list1;
public listAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.my_list);
adapter = new listAdapter(this);
list1.setAdapter(adapter);
Button button1 = (Button) findViewById(R.id.my_button);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.setText("Different text");
View row2Update = list1.getChildAt(3);
list1.getAdapter().getView(3, row2Update, list1);
}
});
}
}
My adapter :
public class listAdapter extends BaseAdapter{
public Activity activity;
public String text="Normal Text";
public listAdapter(Activity activity){
this.activity = activity;
}
public void setText(String text){
this.text = text;
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout, null);
TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(text);
return rowView;
}
}
This is what the activity looks like:
But when I press my button nothing changes
You cannot call the getView() method of the adapter yourself. The adapter's getView() method is is only called, when
The listview is create
when the user scrolls the list view
when notifysetdatachanged() is called.
All these are done by the OS. GetView() is called for all the rows in the listview. It is not called for just a single row. So, if you want to change the rows, you have to provide the data again in a String[], ArrayList<> etc
If you want different text to appear for for a single row, onClick() of a button - you can do this
public class listAdapter extends BaseAdapter{
public Activity activity;
public ArrayList<String> text;
public listAdapter(Activity activity){
this.activity = activity;
}
public void setText(ArrayList<String> text){
this.text = text;
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout, null);
TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(text[position]);
return rowView;
}
}
And in your Activity :
list1 = (ListView) findViewById(R.id.my_list);
adapter = new listAdapter(this);
String[] entries={"Normal Text","Normal Text","Normal Text","Normal text","Normal text"};
ArrayList<String> text=Arrays.asList(entries);
adapter.setText(text);
list1.setAdapter(adapter);
Button button1 = (Button) findViewById(R.id.my_button);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.set(3,"Different Text");
adapter.setText(text);
list1.setAdapter(adapter);
}
});
There is another way of doing it also as #Andy suggested in one of the comments :
listViewPeople.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
//IF YOU WANT TO CHANGE THE CONTENT OF THE ROW CLICKED
if(position == someNumber) {
text.set(position,"different Text");
list1.setAdapter(text);
}
}
});
Sorry for the bold text. For some reason the CTRL+K is not working for the above code.

Android Refresh ListActivity from BaseAdapter

I have a ListActivity and a custom adapter that extends BaseAdapter.
On each row of the ListView, there is a button and a few more views. onClickListener is declared in the adapter.
How can I refresh list view after button is clicked ?
Here is the code
List Activity:
.........
super.onCreate(savedInstanceState);
here im reading content from a file and then creating adapter with that content
adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
..........
Adapter:
.......
public class UvArrayAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater linflater;
private TextView txt_1, txt_2, txt_3;
private ProgressBar pr1;
private String[] str1;
private String[] str2;
private String[] str3;
private Integer[] int1;
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
#Override
public void onClick(View arg0) {
Log.v("TAG", "onItemClick at position" + mPosition);
}
}
public UvArrayAdapter(Context context, String[] s1, String[] s2, Integer[] i1) {
mContext = context;
str1 = s1;
str2 = s2;
int1 = i1;
linflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return str1.length;
}
#Override
public Object getItem(int arg0) {
return str1[arg0];
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = linflater.inflate(R.layout.uv_rowlayout, null);
}
convertView.setOnClickListener(new OnItemClickListener(position));
txt_1 = (TextView) convertView.findViewById(R.id.textView1);
pr1 = (ProgressBar) convertView.findViewById(R.id.progressBar1);
pr1.setProgress(int1[position]);
txt_1.setText(str1[position]);
txt_2 = (TextView) convertView.findViewById(R.id.textView2);
txt_2.setText(str2[position]+"mV");
Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.v("onClickListener", "button - clicked at position " position);
do some changes in mentioned file and after that i need to update the list view with new data
}});
return convertView;
}
}
adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated
Note: If you get any errors with the above line, you could try the following code (where mListView is the name of my ListView object)
((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged();
Another way would be to invalidate the List so that it is redrawn form the updated data set once again.
mListView=(ListView) findViewById(R.id.MyListView);
mListView.invalidate();
Use notifyDataSetChanged()
adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
UvArrayAdapter.notifyDataSetChanged();
// To make the adapter aware of the changes

Categories

Resources