I have a list view that contains Product (productid, productname) + one button
I am filling ArrayList below.
ArrayList<Product> products = new ArrayList<Product>();
//fill products here
productAdapter adapter = new ProductAdapter(getApplicationContext(), products);
productListView.setAdapter(adapter);
productListView.setVisibility(View.VISIBLE);
Now in adapter
public View getView(final int position, View convertView, ViewGroup parent) {
// some other code
holder.btn_addtocart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
products.get(position).setname("ABC");
}
});
}
Problem is its not updating immediately. When we scroll down and up its getting changed.
Why is not not updating listview immediately?
How to fix this?
To refresh the list, you need to call notifyDataSetChanged() after updating the data.
eg:
holder.btn_addtocart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
products.get(position).setname("ABC");
notifyDataSetChanged();
}
});
Related
I have button on items in my list adapter. The problem occurs when I click the button. The action is performed on the right item, but it disables wrong button.
My code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_friend, parent, false);
user = userList.get(position);
ImageView imageView = (ImageView)view.findViewById(R.id.user_image);
Picasso.with(JSONUserAdapter.this.getContext()).load("https://s3.amazonaws.com/profiles-pictures/"+ user.getPath()).into(imageView);
sendRequest = (ImageButton)view.findViewById(R.id.sendRequest);
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendFriendRequest();
disabledAdd();
}
});
return view;
}
Do not try to catch click events for the buttons inside the rows of ListView. Each row of ListView is already clickable. Set the onItemClickListener for the ListView:
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long l) {
//DO SOMETHING HERE
//Use "position" to get the index of item clicked
}
});
As i can see that we had defined the "User" object as a globel variable and it will store the last postions item data.
So when you will click on any button the last items data will be reflected.
{UserType}Object user = userList.get(position);
You can set Tag on View
Use like this:
sendRequest.setTag(user);
And When user clicks item let's get data from tag like this
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here you have user that was clicked.
User user = v.getTag();
sendFriendRequest();
disabledAdd();
}
});
I am trying to convert my app to android version, and I am a bit new in android. I have a list view in which the items include buttons. It is a user list and you can follow each user, when the button is clicked, only in the item including the button, the button text should turn in to "followed". Retrieving the list works fine, but with my below code the button text is not changing. How can I make this happen? Thank you very much.
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(getActivity().getApplicationContext(), R.layout.fragment_users_cell, myItemList);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View cellView = convertView;
if (cellView == null){
cellView = getActivity().getLayoutInflater().inflate(R.layout.fragment_users_cell, parent, false);
}
profileBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_profileBtn);
followBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_followBtn);
profileBtn.setText(myItemList.get(position));
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
System.out.println(position);
}
});
followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
profileBtn.setText("followed");
}
});
return cellView;
}
}
You have to update your dataset and refersh the list after you make changes so that it reflects the latest changes. In your case the text changes.
followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
profileBtn.setText("followed");
myItemList.add(position, "followed");//Change your dataset
notifyDataSetChanged(); //And refresh the adapter
}
});
you need to change the value in myItemList so that next time when view load, the value from list set as text on profile button comes which ix followed
i.e. you need to update the list on click of follow button and notify the ListView.
followBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
myItemList.get(position).set("followed");
profileBtn.setText("followed");
}
});
What I have tried:
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
// // // // NON-FUNCTIONING CODE BELOW
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
};
}
The AdapterView.onItemClickListener doesnt yield any errors but doesnt seem to function whatsoever.
What is the proper way of setting this onClick Listener?
Note: I have to set it in this adapter class, not the main class for my own reasons.
You shuldn't implement a listener inside the getView() unless you what set a listener on a particular view inside your row layout.
You should instead use setOnItemClickListener() method on your ListView:
ListView lv = (ListView) findViewById(R.id.your_listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(context, NewsItemActivity.class);
context.startActivity(intent);
}
});
EDIT:
If, for each action inside the onclick, you need information that resides in your Objects (Item) then you can get it in this way:
Item item = (Item)listview.getAdapter().getItem(position);
from inside of onItemClick() method
You can use:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make what you want
}
});
Or depending on your item view, you can make the OnCLickListener on your global layout of the item or a specific item's layout
Try by this code :-
Spinner cb_tv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter;
list.add(phone1);
list.add(name1);
dataAdapter = new ArrayAdapter<String>(Display_Tank.this,android.R.layout.simple_spinner_item, list);
cb_tv.setAdapter(dataAdapter);
cb_tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
at the place of cb_tv Spinner You can use ListView
First of all, the best way to achieve this is to use your own ViewHolders in ListView.
This is an example of a custom ViewHolder
class MyViewHolder extends ViewHolder implements View.OnItemClickListener {
public MyViewHolder(View view) {
/* Implement your views here
* like this: mTextView = (TextView) view.findViewById(R.id.myId);
*/
view.setOnItemClickListener(this);
}
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), NewsItemActivity.class);
convertView.getContext().startActivity(intent);
}
}
And on your getView method you create this ViewHolder and populate your Views with your data.
You can try this way... it is working in my case Change your context to
Activity activity = (Activity) context;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, NewsItemActivity.class);
activity.startActivity(intent);
}
});
I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}
In my android app I have two List Views and two buttons.On clicking first button, first List View will be visible and on second button second List View will be visible.In one List View I will have list of items and in second List View there will be ADD button. Now clicking on ADD button my first List View will be visible and user can select items to add in second List View. I have done it successfully. But my problem is, by clicking on second button List View is appearing but there are no data which are added from First List View. Below is my code.
What am I missing?
final ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
final View header = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(header, null, false);
btnCollege.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItems[] = {};
final ArrayAdapter<String> string = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1);
lv.setAdapter(string);
Button btnAdd = (Button) header.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (j == 0)
{
lv.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
j = 1;
}
else if (j == 1)
{
lv.setVisibility(View.INVISIBLE);
header.setVisibility(View.INVISIBLE);
j = 0;
}
else {}
lv1.setVisibility(View.VISIBLE);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selected = lv1.getItemAtPosition(arg2).toString();
string.add(selected);
string.notifyDataSetChanged();
setListAdapter(string);
}
});
}
});
}
});
The view is not getting updated. use handlers for updating listview. That would help you solve the issue