0
votes

enter image description here

The problem is if click on first button then the twelfth button also working this error only in mobile if run on emulator the list show all elements and run perfectly.but in mobile list view shows 10 items and need to scroll down to see more`public class MyCustomAdapter extends BaseAdapter implements ListAdapter { private ArrayList list = new ArrayList(); private Context context;

public MyCustomAdapter(ArrayList<String> list, Context context) {
    this.list = list;
    this.context = context;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int pos) {
    return list.get(pos);
}

@Override
public long getItemId(int pos) {
    return 0; //list.get(pos).getId();
    //just return 0 if your list items do not have an Id variable.
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list, null);
    }

    //Handle TextView and display string from your list
    TextView listItemText = (TextView)view.findViewById(R.id.name);
    listItemText.setText(list.get(position));

    //Handle buttons and add onClickListeners
    final Button deleteBtn = (Button)view.findViewById(R.id.present);
    final Button addBtn = (Button)view.findViewById(R.id.absent);

    deleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
           // list.remove(position); //or some other task
            Toast.makeText(context,"hello",Toast.LENGTH_SHORT).show();
            deleteBtn.setBackgroundColor(Color.GREEN);
            addBtn.setEnabled(false);
          //  notifyDataSetChanged();
        }
    });
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
            addBtn.setBackgroundColor(Color.GREEN);
            deleteBtn.setEnabled(false);
         //   notifyDataSetChanged();
        }
    });

    return view;
}

}`

this is list item adding

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            str = sp1.getSelectedItem().toString();

            //handle listview and assign adapter
            if(str.equals("3rd year")) {
                Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();

               for(int i=0;i<15;i++)
               {
                   list3.add("Item"+i);
               }
            }
            ListView lView = (ListView) findViewById(R.id.listView);
            lView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lView.setAdapter(adapter);

        }
3
What you tried, and how you did post your code? - Nigam Patro
this is problem in list view adapter, user a boolean flag to identify in which item you have clicked with id or some thing - Sree
there is no error in code but it work well on emulator...but if use small size mobiles then need to scroll the list to see more items...then the problem happened - unniraja
problem might be the adapter position, post your code - rajahsekar
the problem happens only when list item not showing full items - unniraja

3 Answers

0
votes

Custom LIstview works perfect with ViewHolder.Here the problem is Your button status changes with getView() method.if you done it with ViewHolder Your ListView Items Will Persist their status.

0
votes

This happens, because your view is reused. Emulator is weak, it doesn't reuse view, but create a new one.

change this

View view = convertView;    
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list, null);
    }

to this

ViewHolder holder = null;
 if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.list, viewGroup, false);
        holder = new ViewHolder(view);
        // init your views here, f.e.:
        holder.listItemText = (TextView)view.findViewById(R.id.name);            
        // or just use Butterknife library
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // now you can set value
    holder.listItemText.setText(list.get(position));

where ViewHolder holds the objects, that stores information about your view

class ViewHolder {
    TextView listItemText ;
    Button deleteBtn;
    Button addBtn;
        public ViewHolder(View view) {
           // I would advice you to use Butterknife, it saves time. http://jakewharton.github.io/butterknife/ 
           //ButterKnife.inject(this, view);
        }
    }
0
votes

The issue is that listview layouts are recycled for performance, this means that the first visible cell on the screen has position 0 and increment up to the last visible cell. So if the device is showing 10 views then every tenth view is essentially the same view. Keeping a list of the view positions that you want modified is a quick and simple way to make sure the correct schemes are drawn on the correct views.

Solution

List<Integer> presentPositions = new ArrayList<>();
List<Integer> absentPositions = new ArrayList<>();

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list, parent, false);
    }

    //Handle TextView and display string from your list
    TextView listItemText = (TextView)view.findViewById(R.id.name);
    listItemText.setText(list.get(position));

    //Handle buttons and add onClickListeners
    final Button deleteBtn = (Button)view.findViewById(R.id.present);
    final Button addBtn = (Button)view.findViewById(R.id.absent);

    deleteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            presentPosition.add(position);
            Toast.makeText(context,"hello",Toast.LENGTH_SHORT).show();
            deleteBtn.setBackgroundColor(Color.GREEN);
            addBtn.setEnabled(true);
        }
    });

    addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            absentPositions.add(position);
            addBtn.setBackgroundColor(Color.GREEN);
            deleteBtn.setEnabled(false);
         //   notifyDataSetChanged();
        }
    });

    if (presentPositions.contains(position)) {
        deleteBtn.setBackgroundColor(Color.GREEN);
        addBtn.setEnabled(false);
    } else {
        deleteBtn.setBackgroundColor(Color.RED);
        addBtn.setEnabled(true);
    }

    if (absentPositions.contains(position)) {
        addBtn.setBackgroundColor(Color.GREEN);
        deleteBtn.setEnabled(false);
    } else {
        addBtn.setBackgroundColor(Color.RED);
        deleteBtn.setEnabled(true);
    }

    return convertView;
}