Android : Listview Change Button Text On clilck - android

When I click on the first button only clicked on one button it sets the button background ok . but problem when I scroll down i found more buttons was changed randomly look like picture.
See picture here.
part of code :- ContactSug_Adapter
public class ContactSug_Adapter extends ArrayAdapter {
List list = new ArrayList();
ImageLoader imgLoader = new ImageLoader(getContext());
private Context context;
public ContactSug_Adapter(Context context, int resource) {
super(context, resource);
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View row;
row = convertView;
final ContactHolder contactHolder;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row, parent, false);
contactHolder = new ContactHolder();
contactHolder.tx_id = (TextView) row.findViewById(R.id.usersName2);
contactHolder.tx_name = (TextView) row.findViewById(R.id.usersName);
contactHolder.sug_add = (Button) row.findViewById(R.id.sug_id);
row.setTag(contactHolder);
} else {
contactHolder = (ContactHolder) row.getTag();
}
final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position);
contactHolder.image_tx.setImageResource(R.mipmap.ic_launcher);
contactHolder.tx_id.setText(contacts.getId());
contactHolder.tx_name.setText(contacts.getName());
contactHolder.sug_add.setTag(position);
contactHolder.sug_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contactHolder.sug_add.setText("Selected");
}
});
return row;
}
public class ContactHolder {
TextView tx_id, tx_name,loadId;
ImageView image_tx;
public Button sug_add;
}/********* act

This is due to recycling of views.
You need to set the text "Selected" for the items you want to be and set the default text for the other items. You can do that using if-else statement.
For that you need to have a member variable in Contacts_Sug to hold the selectionPos like this -
private int selectionPos;
public void setSelectedPosition(int position){
selectionPos = position;
}
public int getSelectedPosition(){
return selectionPos;
}
And set it in button onClick() -
contactHolder.sug_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contacts.setSelectedPosition(position); //Set position here
contactHolder.sug_add.setText("Selected");
}
});
And outside this onClick() where you are setting the values for your views.
Add this -
if(contacts.getSelectedPosition() == position){
//Set your button state to "selected" here
contactHolder.sug_add.setText("Selected");
} else{
//Set your button state to default here
contactHolder.sug_add.setText("Follow");
}

Related

Hide textview form ArrayAdaptor from specific item postion

I want to hide a TextView which is inside of the item tv_description_of_tution on the btn_chat_room_withdraw_offer click listener. Hide TextView from particular item in ArrayAdaptor. This is my chat layout I want to hide the particular items TextView how to do this ?
In item there are muliple textview but onclick I want to hide one TextView.
class ChatRoomAdapter extends ArrayAdapter<ChatMessage> {
public ChatRoomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.context = context;
notifyDataSetChanged();
}
#Override
public void add(ChatMessage object) {
chatMessageList.add(object);
super.add(object);
}
public int getCount() {
return this.chatMessageList.size();
}
public ChatMessage getItem(int index) {
return this.chatMessageList.get(index);
}
public View getView(final int position, final View convertView, ViewGroup parent) {
final ChatMessage chatMessageObj = getItem(position);
View row = convertView;
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (chatMessageObj.left) {
row = inflater.inflate(R.layout.chat_incoming_msg, parent, false);
offer = "N";
} else {
row = inflater.inflate(R.layout.chat_outgoing_msg, parent, false);
}
/*Offered Message*/
ll_offer = row.findViewById(R.id.ll_offer);
ll_btns = row.findViewById(R.id.ll_btns);
ll_withdrawn = row.findViewById(R.id.ll_withdrawn);
btn_chat_room_withdraw_offer = row.findViewById(R.id.btn_chat_room_withdraw_offer);
btn_chat_room_withdraw_offer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendData(chatMessageObj.msg_id, position);
}
});
return row;
}
}
You can have a method in ChatRoomAdapter
private void hideMessageAt(int position){
chatMessageList.get(position).setHidden(true);
notifyDataSetChanged();
}
But you will need to add another field to the ChatMessageobject. And eventually, in getView do something like this
final ChatMessage chatMessageObj = getItem(position);
row.setVisibility(chatMessageObj.isHidden() == true ? View.GONE : View.VISIBLE);

Removing Values inside BaseAdapter removed always last Value

I have created a ListView for My Model class and displayed it successfully.
I have successfully done removing value/raw from my ListView onItemClickListener of ListView.
It's Ok, But, my ListView also contains an ImageView and I have to delete a row on click of that ImageView.
So, I have taken a BaseAdapter and handle click of my that ImageView inside my BaseAdapter as below :
My Adapter class is as below :
public class MyAdapter extends BaseAdapter {
private ArrayList<ModelClass> mArrayListModel;
private LayoutInflater mInflator;
private Context mContext;
public MyAdapter(Context mContext, ArrayList<ModelClass> mArrayListModel) {
this.mContext = mContext;
this.mArrayListModel = mArrayListModel;
}
#Override
public int getCount() {
return mArrayListModel.size();
}
#Override
public Object getItem(int position) {
return mArrayListModel.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
if (convertView == null) {
view = new ViewHolder();
mInflator = LayoutInflater.from(mContext);
convertView = mInflator.inflate(R.layout.custom_layout, parent, false);
view.textName = (TextView) convertView.findViewById(R.id.txtName);
view.textAddress = (TextView) convertView.findViewById(R.id.txtAddress);
// view.btnShow = (Button) convertView.findViewById(R.id.btnShow);
view.imgDelete=(ImageView) convertView.findViewById(R.id.imgDelete);
view.textName.setText("" + mArrayListModel.get(position).getStrName());
view.textAddress.setText("" + mArrayListModel.get(position).getStrAddress());
convertView.setTag(view);
view.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, ""+mArrayListModel.get(position).getStrName(), Toast.LENGTH_SHORT).show();
mArrayListModel.remove(position);
notifyDataSetChanged();
}
});
} else {
view = (ViewHolder) convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView textName, textAddress;
//Button btnShow;
ImageView imgDelete;
}
}
I am deleting as below :
view.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, ""+mArrayListModel.get(position).getStrName(), Toast.LENGTH_SHORT).show();
mArrayListModel.remove(position);
notifyDataSetChanged();
}
});
The Problem is with deleting item, When I click on ImageView it deletes only last value.. last position from listview. I think its issue with my position.
Will you pls. check it and let me know what might be the issue ?
Thanks.

Android: changing ImageView in one ListView item changes the same ImageView in other ListView items?

My ListView item consists of the following components - TextView and then under it there is one ImageView - like.
So when I click on like I want to be able to change the like ImageView from grey to blue.
At the moment when I click on like the like ImageView does change from grey to blue. But not only for the corresponding ListView item but for every 3rd item in the list - so if I have 10 items in my list and I click on the like of the first item in the list then 4th, 7th and 10th items like ImageView change from grey to blue.
In my post_list_item.xml in the root element of the file I specified the following android:descendantFocusability="blocksDescendants" but it doesn't help either.
My question is - what do I have to do so that when I click on like I would be able to change the like ImageView from grey to blue without affecting other list items?
Here is my code:
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater;
ArrayList<Post> list;
public CustomListAdapter(PostActivity postActivity, ArrayList<Post> list) {
inflater = LayoutInflater.from(postActivity);
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_list_item, parent, false);
holder = new ViewHolder();
holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost);
holder.ivLike = (ImageView) convertView.findViewById(R.id.ivLike);
holder.tvLikes = (TextView) convertView.findViewById(R.id.tvLikeCount);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Post post = list.get(position);
holder.tvPost.setText(post.getPost());
holder.tvLikes.setText(post.getLiked());
holder.ivLike.setOnClickListener(mClickListener);
holder.ivLike.setTag(position);
return convertView;
}
private View.OnClickListener mClickListener = new View.OnClickListener() {
public void onClick(View v) {
int pos = (Integer) v.getTag();
Post post = (Post) list.get(pos);
post.setIvLike(v);
CustomListAdapter.this.notifyDataSetChanged();
}
};
static class ViewHolder
{
TextView tvPost ;
TextView tvLikes;
ImageView ivLike;
}
}
Post.java
public class Post {
String post;
String liked;
public void setIvLike(View view){
if(view.isActivated())
view.setActivated(false);
else
view.setActivated(true);
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post= post;
}
public String getLiked() {
return liked;
}
public void setLiked(String liked) {
this.liked = liked;
}
}
You're not "resetting" the view in getView when a view is reused. You need to set the recycled view back to the correct state. This is done after the if-else block where you create or reuse a ViewHolder.
setIvLike will need to modify that Post object in one way or another such that when it is scrolled off screen the view can be recreated exactly as it appeared even if it is using a recycled view. For that reason it is important to know which properties of the view you change and consistently set every one of those properties in getView after reuse/creation.
You need to have a property in your Post Class to track that post has been liked
Updated Post.Java
public class Post {
String post;
String liked;
public bool isLiked; // Add this
public void setIvLike(View view){
if(!isLiked)
view.setActivated(false);
else
view.setActivated(true);
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post= post;
}
public String getLiked() {
return liked;
}
public void setLiked(String liked) {
this.liked = liked;
}
}
And in getView method of Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_list_item, parent, false);
holder = new ViewHolder();
holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost);
holder.ivLike = (ImageView) convertView.findViewById(R.id.ivLike);
holder.tvLikes = (TextView) convertView.findViewById(R.id.tvLikeCount);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Post post = list.get(position);
holder.tvPost.setText(post.getPost());
holder.tvLikes.setText(post.getLiked());
holder.ivLike.setOnClickListener(mClickListener);
holder.ivLike.setTag(position);
holder.setIvLike(ivLike); // Add this
return convertView;
}
private View.OnClickListener mClickListener = new View.OnClickListener() {
public void onClick(View v) {
int pos = (Integer) v.getTag();
list.get(pos).isLiked = true;
// post.setIvLike(v); // No need to update here
CustomListAdapter.this.notifyDataSetChanged();
}
};

how to get listview item on button click and save to sqlite

I've four textviews and a button in listview. when user clicks on the button i want to get the value of these four textviews and pass to the database. I'm confuse how to save the data of selected item to sqlite.
public class PlannningListViewAdapter extends BaseAdapter{
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity planActivity;
public PlannningListViewAdapter(Context context,
ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvDocCode = (TextView) convertView.findViewById(R.id.Plan_no);
TextView tvDocName = (TextView) convertView.findViewById(R.id.doc_name);
TextView tvmon = (TextView) convertView.findViewById(R.id.mon);
TextView tvAdr = (TextView) convertView.findViewById(R.id.adr);
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setOnClickListener(ButtonClickListener);
tvDocCode.setText(p.getDocCode());
tvDocName.setText(p.getDocName());
tvmon.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
return convertView;
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
// Log.v(TAG, "item clicked, row %d", position);
}
};
}
You can use setTag()and getTag() method on your button.
Please look at the code below:
You have to change getItem method:
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
Update your getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setTag(position);
...
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = (int)v.getTag();
Planning planning = getItem(position);
yourDatabaseHelper. getRuntimeExceptionDao(Planning.class).createOrUpdate(planning);
}
};
One more thing. You should implement ViewHolder please read about this pattern, and the best solution for you is use RecyclerView, getting position is already implemented there in RecyclerView.ViewHolder.

Custom NumberPicker buttons.onClickListener

One of my activities requires a list view with custom number pickers in each row.
I created the number picker as suggested here. When I click on the buttons, however, the counter doesn't increment or decrement at all.
The following is the code found at my Custom ListView Adapter.
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
private ArrayList<MaterialClass> data;
public static LayoutInflater inflater = null;
public static Dialog dialog;
String materialName;
public MaterialListViewAdapter(Context applicationContext,
int materialdialogcontent, ArrayList<MaterialClass> materials)
{
this.data = materials;
inflater = (LayoutInflater)applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return data.size();
}
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
holder.num.setText("0");
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.sub.setText("" + counter);
}
});
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}
When I try debugging to see what's going on, counter doesn't even initialize with '0'.
I tried initializing the counter in the getView() method, however in order to do so I need to set the counter to final, and then another error shows up saying
The final local variable counter cannot be assigned, since it is defined in an enclosing type.
Any suggestions?
Updated for additional code:
public class Material extends Activity
{
ArrayList<String> materialList;
ListView lv;
Button btnConfirm;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_material);
lv = (ListView)findViewById(R.id.list);
btnConfirm = (Button)findViewById(R.id.btnConfirm);
materialList = new ArrayList<String>();
Intent i = getIntent();
materialList = i.getStringArrayListExtra("materialList");
if(materialList != null)
{
Toast t = Toast.makeText(getApplicationContext(), materialList.get(0).toString(), Toast.LENGTH_LONG);
t.show();
}
else
{
//FILL HERE LATER
}
ArrayList<MaterialClass> materials = new ArrayList<MaterialClass>();
for(String temp : materialList)
{
MaterialClass m = new MaterialClass();
m.setName(temp);
materials.add(m);
}
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
lv.setAdapter(adapter);
}
}
You should implement the onItemClickListener for your listView . You are using listener in adapter class but practically those numbers are nothing but items of that listView, so that to operate on those buttons (listView's items) you've not defined onItemClickListener. So after setting adapter write
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View item, int position, long id) {
// here you are getting position of clicked item
}
}
UPDATE:
I've gone through your code again and I found these:
1.you are writing this line:
MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
but I didn't see any constructor in your Adapter.
2.As you are extending BaseAdapter, you must override getCount() , getItem(int position) and getItemId(int position) method as like getView() . There you are getting issue.
Try this :
public class MaterialListViewAdapter extends BaseAdapter
{
ViewHolder holder;
int counter = 0;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
//ViewHolder holder = new ViewHolder();
if(vi == null)
{
holder = new ViewHolder();
vi = inflater.inflate(R.layout.materialdialogcontent, null);
//Initialize Buttons and TextViews.
//holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
//holder.pk.setMaxValue(20);
//holder.pk.setMinValue(0);
holder.add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
holder.num.setText("" + counter);
}
});
holder.sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
holder.num.setText("" + counter);
}
});
return vi;
}
private class ViewHolder
{
TextView txt;
Button add;
Button sub;
TextView num;
//NumberPicker pk;
}
}

Categories

Resources