Problem - when I click on confirm button item do not disappear from the list and after touch on the listview again item is removed from the list. Why this happening please give me solution.
Overall remove method is not working in real time.
final ArrayList<orderTrialHelper> list_data;
Context context;
int resource;
ProgressDialog pd;
public orderTrial(Context context, int resource, ArrayList<orderTrialHelper> list_data) {
super(context, resource, list_data);
this.list_data = list_data;
this.context = context;
this.resource = resource;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.cust_list_trials, null, true);
}
final orderTrialHelper listdata = getItem(position);
id_no.setText(String.valueOf(listdata.getId_no()));
date.setText(listdata.getDate());
quantity.setText(String.valueOf(listdata.getQuantity()));
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
list_data.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
Try using recyclerview. May be that can solve your problem.
Related
I have a ListView and a CustomAdapter. The elements are all successfully loaded into the list. Now I want to change the background color of a certain element of the list by clicking on an external button. But I do not know how to access a specific item in the list.
Here is the CustomAdapter class:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
}
You can do it like this inside your getView() method
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
}
});
If you have a button on your view then performs the listener on that button
If you want to get your selected item view from your parent activity then :
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
selectedposition = position ;
}
});
View view = listView.getAdapter().getView(selectedposition,null,listview);
Then change its background:
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
please define your color in your color.xml file
If you have more than one view then , create an ArrayList<View> and do some loop
create a custom listener interface in your activity and your
adapter will implement this.
public interface OnClickListenerFromActivity {
void onActivityButtonClick(int position);
}
on click in your button call your listener's method
mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));
implement this listener into your adapter
public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
public void onActivityButtonClick(int position) {
// get your item through position and
// set your color here
}
}
in my android application I am displaying the contact list of phone in a list view and a check box in each row for selecting contacts. But when I am selecting a particular row about tenth row is also getting selected automatically. I am giving my code below, if any one knows please help..
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
final ViewHolder mHolder;
if (convertView == null)
{
// gridView = new View(context);
// get layout from mobile.xml
//gridView = inflater.inflate(R.layout.contact, null);
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
));
}
else
{
mHolder = (ViewHolder) convertView.getTag();
mHolder.textSelector.setOnCheckedChangeListener(null);
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textSelector.setFocusable(false);
return convertView;
}
private class ViewHolder {
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Well Thats because ViewHolder will recycle the Views everytime you Scroll
i suggest you to Use onCLick on ListItem instead checkbox
To overcome this Declare a SparseBooleanArray
SparseBooleanArray sba=new SparseBooleanArray();//this should be global
Then set the items checked state as soon as you render it
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
mHolder.textSelector.setChecked(sba.get(position));
Then write a onClickListener to you convertView and check it manually
convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mHolder.textSelector.setChecked(isChecked);
sba.put(position,isChecked); //storing the state
}
}
);
**Well Now the sba has list items checked and you can use that for further Actions**
# Jocheved... edit your code like this...
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
SparseBooleanArray sba=new SparseBooleanArray();
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder mHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textName.setSelected(true);
mHolder.textSelector.setChecked(sba.get(position));
mHolder.textSelector.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if(mHolder.textSelector.isChecked())
{
sba.put(position, true);
}
else
{
sba.put(position, false);
}
}
});
return convertView;
}
private class ViewHolder
{
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
I have a listview item with a label and two buttons. I tried to change the label on button click. But it's changing text on another listview item. Not the label with button. I did this using a custom list adapter. I tried it like following,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem listItem = objects.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, null);
btnPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
cartQtyTextView.setText("" + ++i);
}
});
}
}
How may I fix this?
You might want to create holder.
I didn't include your menu item code due to i didn't see you got use it.
public class Holder {
ButtonRectangle buttonPlus;
TextView cartQtyTextView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get Holder
final Holder holder = new Holder();
// Change Layout
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.menu_list_item, null);
// Find Control
holder.buttonPlus = (ButtonRectangle)view.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView)view.findViewById(R.id.cartQtyTextView);
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
if (holder.cartQtyTextView != null) {
holder.cartQtyTextView.setText("" + ++i);
}
}
});
}
return view;
}
This is because you are setting the listener inside the if(convertView==null).
So the listener is only set once a view is created, but when you scroll, the list view is reusing a hidden item, but it keep the first assigned listener since the convertView is not null.
You need to set your onClickListener outside the if. And it is better if you use a holder for better performances
public class ExampleAdapter extends ArrayAdapter<MenuItem> {
private Activity activity;
private int resource;
private List<MenuItem> objects;
public ExampleAdapter(Activity activity, int resource, List<MenuItem> objects) {
super(activity, resource, objects);
this.activity = activity;
this.resource = resource;
this.objects= objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MenuItem listItem = objects.get(position);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(resource, parent, false);
holder.labelTextView= (TextView) convertView.findViewById(R.id.labelTextView);
holder.btnPlus= (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
holder.labelTextView.setText("" + ++i);
}
});
return convertView;
}
static class ViewHolder {
TextView labelTextView;
ButtonRectangle btnPlus;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.textView1,
(String[]) myArr.toArray(new String[]{})));
}
class MyAdapter extends ArrayAdapter<String>{
final int which;
public MyAdapter(Context context, int resource,
int textViewResourceId, String[] string) {
super(context, resource, textViewResourceId, string);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int which=position;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_view, parent,false);
TextView tv = (TextView) row.findViewById(R.id.textView1);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
ToggleButton tb = (ToggleButton) row.findViewById(R.id.toggleButton1);
tv.setTag(position);
iv.setTag(position);
tb.setTag(position);
tv.setText(myArr.get(position).toString());
tb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(tb.isChecked()){
which=(Integer) tb.getTag();
}else{
}
}
});
return row;
}
}
}
Hello I am trying to work with the setTag view method to detect what items is pressed. my layout is composed of a list each row containing a textfield a togglebutton and an imageview. I would like to change the image of the row containing the pressed button. I have gone this far and i'm stuck on how to continue can i get some help please?
You can simply the position passed to your getView(final int position, View convertView, ViewGroup parent). Just make it final and access it directly without the need to make tag.
However, you should use tagging of views in order to make use of handling scrollable views offered by android.
class MyAdapter extends ArrayAdapter<String>{
final int which;
ArrayList<Boolean> mListImageStates = new ArrayList<Boolean>();
public MyAdapter(Context context, int resource,
int textViewResourceId, String[] string) {
super(context, resource, textViewResourceId, string);
for(int i = 0; i<string.length;i++)
{
mListImageStates.add(new Boolean(false));
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int which=position;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ChildViewHolder holder = new ChildViewHolder();
if(convertView == null){
convertView = inflater.inflate(R.layout.custom_view, parent,false);
holder.mTv = (TextView) row.findViewById(R.id.textView1);
holder.mIv = (ImageView) row.findViewById(R.id.imageView1);
holder.mTb = (ToggleButton) row.findViewById(R.id.toggleButton1);
convertView.setTag(holder)}
else
{
holder = (ChildViewHolder) convertView.getTag();
}
holder.mTv.setText(myArr.get(position).toString());
holder.mTb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
boolean listState = mListImageStates.get(position);
mListImageStates.set(position,new Boolean(!listState));
notifyDataSetChanged();
}});
if(mListImageStates.get(position) == false)
{
//TODO
}else
{
//TODO
}
return convertView;
}
class ChildViewHolder
{
public TextView mTv;
public ImageView mIv;
public ToggleButton mTb;
}
What I am trying to do is access an ArrayList that is in my main activity from a customer adapter for a listview.
I have a listview that I am making clickable, which I have done within the customer adapter(where the onclick resides). When the user clicks it they will enter a caption(text) which will update an arraylist(string) in the main activity.
I have read a few other questions about this topic but I'm lost on how to make this work.
I didn't see the need to post a code snippet because it's just a basic arraylist(string) and a custom adapter for a listview. If code is needed I can post though. Thanks!
Here part of the adapter code:
public class PhotoAdapter extends ArrayAdapter<Photo> {
private final ArrayList<Photo> objects;
public PhotoAdapter(Context context, int textViewResourceId,
ArrayList<Photo> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Photo i = objects.get(position);
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_row, null);
v.setClickable(true);
v.setFocusable(true);
}
Here is come code from the main activity
public ArrayList<Photo> m_photos = new ArrayList<Photo>();
public ArrayList<String> m_photo_captions = new ArrayList<String>();
private PhotoAdapter m_adapter;
this.list1 = (ListView) findViewById(R.id.lstPhotos);
m_adapter = new PhotoAdapter(this, R.layout.listview_row, m_photos);
LayoutInflater infalter = getLayoutInflater();
list1.setAdapter(m_adapter);
if (Intent.ACTION_SEND_MULTIPLE.equals(action)
&& intentGallery.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list = intentGallery
.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable p : list) {
Uri uri = (Uri) p;
imagePath = getPath(uri);
m_photos.add(new Photo(imagePath, ""));
m_photo_locations.add(imagePath);
m_photo_captions.add("");
m_adapter.notifyDataSetChanged();
}
}
onclick listener
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Do whatever you want with m_photo_captions here
Log.v("listview", "listview item clicked");
appErrorAlert("test", "test");
}
});
You can define the ArrayList you want to access as a global instance variable, that way you can access it from inside your custom adapter.
It'll also be helpful if you provided a code snippet for your problem.
Now that you have posted your code, I would suggest a different approach than Mohamed A. Karim's.
You are trying to set a simple OnClickListener on the entire row in your Adapter but access members of your Activity. Why not just set an OnItemClickListener in your ListView which already has access to the List that you want?
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want with m_photo_captions here
}
});
Next remove everything that will intercept the touch event before it reaches your ListView. You can also make your getView() a little smaller and faster, like this:
public class PhotoAdapter extends ArrayAdapter<Photo> {
LayoutInflater mInflater;
public PhotoAdapter(Context context, int textViewResourceId,
ArrayList<Photo> objects) {
super(context, textViewResourceId, objects);
mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Photo i = get(position);
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.listview_row, null);
// Initialize your ViewHolder here!
}
// Update your TextViews, ImageViews, etc here
...
}
}
Hope that helps!