Hide textview form ArrayAdaptor from specific item postion - android

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

Related

how to add listener to a spinner inside listview adapter in android

public class XpressGoldLoanAdapter extends ArrayAdapter<TransactionDetailsUserData> {
private List<TransactionDetailsUserData>list;
private Context context;
String securityNo;
public XpressGoldLoanAdapter( List<TransactionDetailsUserData> list, Context ctx) {
super(ctx, R.layout.reminderinflater, list);
this.list = list;
this.context = ctx;
}
#Override
public int getCount() {
return list.size();
}
#Override
public TransactionDetailsUserData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return list.indexOf(getItem(position));
}
private class ViewHolder{
TextView tv_securityNo;
Spinner spin_securityType,spin_security,spin_purityconfirmed ;
LinearLayout sublayout;
EditText ed_numbers,ed_grossWeight,ed_netWeight,ed_rate_perGram,ed_branchMarginRate,ed_amount;
Button btn_savesecurity;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Spinner spin_securityType,spin_security,spin_purityconfirmed;
TextView tv_securityNo;
EditText ed_numbers,ed_grossWeight,ed_netWeight,ed_rate_perGram,ed_branchMarginRate,ed_amount;
LinearLayout sublayout;
Button btn_savesecurity;
ViewHolder holder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expressloanitem,null);
spin_securityType =(Spinner) view.findViewById(R.id.spin_securityType);
spin_security =(Spinner) view.findViewById(R.id.spin_security);
spin_purityconfirmed =(Spinner) view.findViewById(R.id.spin_purityconfirmed);
tv_securityNo =(TextView)view.findViewById(R.id.tv_securityNo);
ed_numbers =(EditText)view.findViewById(R.id.ed_numbers);
ed_grossWeight =(EditText)view.findViewById(R.id.ed_grossWeight);
ed_netWeight =(EditText)view.findViewById(R.id.ed_netWeight);
ed_rate_perGram =(EditText)view.findViewById(R.id.ed_rate_perGram);
ed_branchMarginRate =(EditText)view.findViewById(R.id.ed_branchMarginRate);
ed_amount =(EditText)view.findViewById(R.id.ed_amount);
sublayout =(LinearLayout)view.findViewById(R.id.sublayout);
btn_savesecurity =(Button) view.findViewById(R.id.btn_savesecurity);
holder.spin_securityType = spin_securityType;
holder.spin_security = spin_security;
holder.spin_purityconfirmed = spin_purityconfirmed;
holder.tv_securityNo = tv_securityNo;
holder.ed_numbers = ed_numbers;
holder.ed_grossWeight = ed_grossWeight;
holder.ed_netWeight = ed_netWeight;
holder.ed_rate_perGram = ed_rate_perGram;
holder.ed_branchMarginRate = ed_branchMarginRate;
holder.ed_amount = ed_amount;
holder.sublayout = sublayout;
holder.btn_savesecurity = btn_savesecurity;
view.setTag(holder);
}
else{
holder =(ViewHolder) view.getTag();
}
TransactionDetailsUserData pojo = enqList.get(position);
securityNo = pojo.getSecurityCount();
Log.d("securityNo",securityNo);
holder.tv_securityNo.setText(securityNo);
SpinnerLogoAdapter spinnerAdapter3=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),securityTpeIcons,securityTpeitems);
holder.spin_securityType.setAdapter(spinnerAdapter3);
SpinnerLogoAdapter spinnerAdapter4=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),securityIcons,securityitems);
holder.spin_security.setAdapter(spinnerAdapter4);
SpinnerLogoAdapter spinnerAdapter5=new SpinnerLogoAdapter(XpressGoldLoan.this.getApplicationContext(),purityIcons,purityitems);
holder.spin_purityconfirmed.setAdapter(spinnerAdapter5);
//spinner security type
holder.spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//save security
holder.btn_savesecurity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
}
This is my adpater of listview.when itry to add spinner.getSelectedItem() inside the listener its showing some errors.App crashes inside onItemSelected of spinner .please find a solution.I just only need to get the selected item from spinner.I dont have much to elaborate my question since it is simple to understand.Please help
Change your holder to be final (as the error tells you to)
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder()
... //the rest of code here stays the same
} else {
holder =(ViewHolder) view.getTag();
}
add this line in onItemselectedListener
holder.spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent,final View view,final int position,final long id) {
getSelectedItem(parent, view, position, id);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Write one more method in XpressGoldLoanAdapter class which does task for you.
final void getSelectedItem(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = holder.spin_securityType.getSelectedItem().toString();
// do your task here
}
i solved it trying this code since securityTpeitems[] is a string array and i have taken position from the spinner.
String selectedSecType = securityTpeitems[position].toString();

Listview showing previous selection [ANDROID]

In my app i am showing some items in list view, each row have one name and one tick image. Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible. How this can be fixed? Any help will be appreciated
here is my code
Adapter
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount() {
return responseList.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 view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart (Item Click Listener)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}
It sounds like you're using a CheckBox.
Have you tried conditionally updating the Views visibility depending on whether the item isChecked (and not isSelected)?
you should set tag for each image to keep track of its state (0 = not selected/invisible , 1 = selected/visible) , e.g:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
and you can add this :
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}

Android : Listview Change Button Text On clilck

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

Cant update data from within a ArrayAdapter

Im using a array adapter with a so show a list of linear layouts. The layouts have 3 text fields and a hidden button that is shown when the list item is clicked. What I am trying to do is attach a onclicklistener to the button that will delete that row from the arraylist/adapter and update the data set. I have the following code:
public class CheckAdapter extends ArrayAdapter<Check> {
Context context;
int layoutResourceId;
ArrayList<Check> data = null;
public CheckAdapter(Context context, int resource, ArrayList<Check> objects) {
super(context, resource, objects);
this.context = context;
this.layoutResourceId = resource;
this.data = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder(convertView.findViewById(R.id.checklistRoot));
holder.checkName = (TextView) convertView.findViewById(R.id.checklistname);
holder.checkNumber = (TextView) convertView.findViewById(R.id.checklistnumber);
holder.checkTotal = (TextView) convertView.findViewById(R.id.checklisttotal);
holder.deleteButton = (Button) convertView.findViewById(R.id.deletecheck);
holder.swipeButtons();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
holder.swipeButtons();
}
String name = data.get(position).getName();
Double total = data.get(position).getAmmount();
Long number = data.get(position).getCheckNumber();
holder.checkName.setText(name);
holder.checkNumber.setText(number.toString());
holder.checkTotal.setText(total.toString());
holder.deleteButton.setTag(position);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer position = (Integer) view.getTag();
Log.d("Check Adapter", "Button Clicked: " + position);
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
static class ViewHolder{
TextView checkName;
TextView checkNumber;
TextView checkTotal;
Button deleteButton;
View rootView;
ViewHolder(View rootView) {
this.rootView = rootView;
}
public void swipeButtons(){
rootView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
deleteButton.setVisibility(View.VISIBLE);
return false;
}
});
}
}
}
My logs are showing I have the correct position being sent into data.remove(position); However the ListView doesn't seem to be updating and the data seems to be remaining. Any suggestions?
Thanks so much!
You can also try using position value provided by getView method itself.
Also remove line holder.deleteButton.setTag(position); from getView method.
so your code will be like below :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// --- Your other code ---//
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
Hope this will help you.

How to make spinner show full items in Android

I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.

Categories

Resources