0

I am trying to display information from the database to my list view. I can display it, but when i want to do something with it ,I am getting inaccurate position in the getView method. Can you guys give me some tips of what should I do?

    public class ViewAdapter extends BaseAdapter {
    LayoutInflater minflater;


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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public ViewAdapter() {
        minflater = LayoutInflater.from(getBaseContext());

    }

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

        if (convertView == null) {
            convertView = minflater.inflate(R.layout.you, null);

        }

        final TextView text11 = (TextView) convertView.findViewById(R.id.text11);
        text11.setText(productsList.get(position).get_productname());
        final TextView text22 = (TextView) convertView.findViewById(R.id.text22);
        text22.setText(productsList.get(position).get_versionname());
        final TextView text33 = (TextView) convertView.findViewById(R.id.text33);
        text33.setText(productsList.get(position).get_date());
        final Button update = (Button) convertView.findViewById(R.id.update);

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           //     int a = dbhandler.checkDatabase(productsList.get(position).get_date());

                final Calendar cal = Calendar.getInstance();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int month = cal.get(Calendar.MONTH);
                int year = cal.get(Calendar.YEAR);
                StringBuffer buffer = new StringBuffer();
                buffer.append(day + "-" + (month + 1) + "-" + year);
                String date = DateFormat.getTimeInstance().format(new Date());
               String b = "Date: " + buffer + "\n" + "Time: " + "" + date + "\n";
                dbhandler = new mydbhandler(getBaseContext(), null,null,1);
                dbhandler.updateProduct(position,b);


              productsList = dbhandler.getFavList();
                listview.setAdapter(new ViewAdapter());


            }


        });


        return convertView;
    }
}
daemmie
  • 6,361
  • 3
  • 29
  • 45
  • this line creating problem i think... `productsList = dbhandler.getFavList(); listview.setAdapter(new ViewAdapter());`. just get the value in list view and call notifydatasetchanged() on adapter. no need to set the adapter again and again.. – SRB Bans Oct 05 '15 at 08:20
  • Your problem is probably similar to [this ListView problem](http://stackoverflow.com/questions/32930800/edit-text-loses-content-in-list-view-on-scrolling/32931806) – Markus Kauppinen Oct 05 '15 at 08:21
  • @oberflansch i dint use view holder – Kuanny Goh Chee Tatt Oct 05 '15 at 08:24
  • you can try using settag for a view for which you want the result – Aayushi Oct 05 '15 at 08:24
  • @sourahb can you eleaborate more? – Kuanny Goh Chee Tatt Oct 05 '15 at 08:26
  • why dont you use `SimpleCursorAdapter` ? – pskink Oct 05 '15 at 08:30
  • @pskink i noe this method better , i just cant get the position correct after deleting the data – Kuanny Goh Chee Tatt Oct 05 '15 at 08:34
  • 1
    better? ok so keep fighting with it... if you used `SCA` all of your problems would be gone long time ago... – pskink Oct 05 '15 at 08:35
  • try to debug it.. if the position is wrong after first click on button or before. – SRB Bans Oct 05 '15 at 08:43
  • Hello. You don't need to use SCA of course. In android, the array that holds data (productList) should be updated in activity and passed to adapter class with final data. Never update the data in adapter, this is really bad and it is not how android lists work. So, inside your activity, you need to use listview methods to detect click on list item (listview has onItemClickListener for example) and then change/update the data (your array) in activity. After you have updated the array in activity you need to refresh your listview/adapter calling notifyDataSetChanged(); – Marko Niciforovic Oct 05 '15 at 09:14
  • @pskink sry bro , i am just a beginner. – Kuanny Goh Chee Tatt Oct 05 '15 at 09:17
  • @marko i will learn from that – Kuanny Goh Chee Tatt Oct 05 '15 at 09:18
  • 1
    @MarkoNiciforovic i have no idea why people with no reason at all are so stubborn and when their data model is sqlite db, they are duplicating `SCA` work by iterating over the `Cursor`, implementing POJO class and custom `BaseAdapter` again and again, making lots of bugs again and again – pskink Oct 05 '15 at 09:19
  • @pskink You are right, this is good example where he could use SCA, but given that OP already started this way I feel for beginner like OP will be more confusing to switch now (his way is not incorrect btw). – Marko Niciforovic Oct 05 '15 at 09:22

2 Answers2

2

changed the code you can try this , it works well for me

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class ViewAdapter extends BaseAdapter
{
    LayoutInflater minflater;


    @Override
    public int getCount() {
        return productsList.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }


    @Override
    public long getItemId(int position) {
       return position;
    }

    public ViewAdapter() {
        minflater = LayoutInflater.from(getBaseContext());

   }

   public class Holder
   {
       TextView text11 , text22 ,text33;
       Button update;
   }

 @Override
 public View getView(final int position, View convertView, ViewGroup parent)
 {
    final Holder holder;
    if (convertView == null)
    {
        convertView = minflater.inflate(R.layout.you, null);
        holder=new Holder();
    holder.text11 = (TextView) convertView.findViewById(R.id.text11);
    holder.text22 = (TextView) convertView.findViewById(R.id.text22);
    holder.text33 = (TextView) convertView.findViewById(R.id.text33);
    holder.update = (Button) convertView.findViewById(R.id.update);


        convertView.setTag(holder);
    }
    else
    {
        holder = (Holder) convertView.getTag();
    }
    holder.text11.setText(productsList.get(position).get_productname());
    holder.text22.setText(productsList.get(position).get_versionname());
    holder.text33.setText(productsList.get(position).get_date());

    return convertView;
}


}
Aayushi
  • 574
  • 3
  • 14
0

In the BaseAdapter there are some methods that need to be overridden. Among these, there is getCount().

When the ListView is created and whatnot, it calls getCount(). If this returns a value different than 0 (I returned the size of the ArrayList which I've previously initialized in the constructor), then it calls getView() enough times to fill the screen with items. For instance, I initialized the ArrayList with 20 items. Because only 8 items initially fit on the screen, getView() was called 8 times, each time asking for the position it required for me to return (more precisely it wanted to know how the row would look like in the list on that specific position, what data it needed to contain). If I scroll down the list, getView() gets called over and over again, 'til I hit the end of the list, in my case 20 items / rows.

What notifyDataSetChanged() does is ... when called, it looks at what items are displayed on the screen at the moment of its call (more precisely which row indexes ) and calls getView() with those positions.

i.e. if you're displaying the first 8 items in the list (so those are the ones visible on the screen) and you add another item between the 2nd and 3rd item in the list and you call notifyDataSetChanged() then getView() is called 8 times, with positions starting from 0 and ending with 7, and because in the getView() method you're getting data from the ArrayList then it will automatically return the new item inserted in the list alongside 7 out of the previous 8 (7 and not 8 because the last item went one position down, so it is not visible anymore), and the ListView will redraw, or whatever, with these items. copied from here

you can try with the edited code.

public class ViewAdapter extends BaseAdapter {
    LayoutInflater minflater;


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

    @Override
    public Object getItem(int position) {
        return productsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public ViewAdapter() {
        minflater = LayoutInflater.from(getBaseContext());

    }

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

        if (convertView == null) {
            convertView = minflater.inflate(R.layout.you, null);

        }

        final TextView text11 = (TextView) convertView.findViewById(R.id.text11);
        text11.setText(productsList.get(position).get_productname());
        final TextView text22 = (TextView) convertView.findViewById(R.id.text22);
        text22.setText(productsList.get(position).get_versionname());
        final TextView text33 = (TextView) convertView.findViewById(R.id.text33);
        text33.setText(productsList.get(position).get_date());
        final Button update = (Button) convertView.findViewById(R.id.update);

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           //     int a = dbhandler.checkDatabase(productsList.get(position).get_date());

                final Calendar cal = Calendar.getInstance();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int month = cal.get(Calendar.MONTH);
                int year = cal.get(Calendar.YEAR);
                StringBuffer buffer = new StringBuffer();
                buffer.append(day + "-" + (month + 1) + "-" + year);
                String date = DateFormat.getTimeInstance().format(new Date());
               String b = "Date: " + buffer + "\n" + "Time: " + "" + date + "\n";
                dbhandler = new mydbhandler(getBaseContext(), null,null,1);
                dbhandler.updateProduct(position,b);

                productsList.clear();
                productsList.addAll(dbhandler.getFavList());
                ((ViewAdapter)listview.getAdapter()).notifyDataSetChanged();


            }


        });


        return convertView;
    }
}
Community
  • 1
  • 1
SRB Bans
  • 3,096
  • 1
  • 10
  • 21