List view items changes position when scrolling android?
Asked Answered
S

3

22

In my application i used custom list view adapter. In the list view item i added another layout. because in my service one order have any number of order items. For showing the order items i added the order item layout below to custom list view item.it is working fine. but when scroll the list view the data is changed.

adapter class:

public class PreviousDataAdapter extends BaseAdapter {
    ArrayList<PreviousOrderData> bpData;
    private ArrayList<PreviousOrderData> arraylist;
    ArrayList<OrderItemData> opData;
    private ArrayList<OrderItemData> arraylist1;
    private Activity activity;
    private LayoutInflater inflater;
    String g_orderid;
    DatabaseHandler db;
    SharedPreferences m_sharedPreference;
    SharedPreferences.Editor m_editor;

    ArrayList<String> circless = new ArrayList<String>();

    public PreviousDataAdapter(Activity activity, ArrayList<PreviousOrderData> bpData,ArrayList<OrderItemData> opdata) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.bpData = bpData;
        this.opData=opdata;//k?

       // this.opData=bpData.get()//opdata;
        inflater = LayoutInflater.from(activity);
        db = new DatabaseHandler(activity);

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return bpData.size();
    }

    @Override
    public Object getItem(int location) {
        // TODO Auto-generated method stub
        return bpData.get(location);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private int lastPosition = -1;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Typeface custom_regular = Typeface.createFromAsset(activity.getAssets(),
                "fonts/OpenSans-Regular.ttf");
        m_sharedPreference = activity.getSharedPreferences("save_details", activity.MODE_PRIVATE);
        m_editor = m_sharedPreference.edit();


        //String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        //final View view;


        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.previousorders_listitem, null);


            viewHolder.date = (TextView) convertView.findViewById(R.id.date);
            viewHolder.orderid = (TextView) convertView.findViewById(R.id.orderid);
            viewHolder.amount = (TextView) convertView.findViewById(R.id.amount);
            viewHolder.status = (TextView) convertView.findViewById(R.id.status);
            viewHolder.ordersinfo=(LinearLayout)convertView.findViewById(R.id.previousorders);
            viewHolder.date .setTypeface(custom_regular);
            viewHolder.orderid.setTypeface(custom_regular);
            viewHolder.amount.setTypeface(custom_regular);
            viewHolder.status.setTypeface(custom_regular);




            viewHolder.ll_individuvalitem = (LinearLayout) convertView.findViewById(R.id.ll_individuvalitem);

            //convertView = view;
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.date.setText(bpData.get(position).getDate());
        viewHolder.orderid.setText(bpData.get(position).getOrdername());
        viewHolder. amount.setText("Rs."+bpData.get(position).getAmount());
        viewHolder. status.setText(bpData.get(position).getStatus());
      viewHolder.ordersinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                g_orderid =bpData.get(position).getOrderid();
                Log.v("TAG_ORDERIDADAPTER",""+g_orderid);
                new GetReorder().execute(WebUrl.RechargeServiceURL+"APP_Reorder");



            }
        });


        //Log.d("size:",opData.size()+"");
        if( opData!= null && opData.size() >0){opData.clear();}
        opData = bpData.get(position).getOrderItemDataList();//
        if(opData.size()>0){
            viewHolder.ll_individuvalitem.removeAllViews();
            for (int i = 0; i < opData.size(); i++){
               // Log.v("TAG Number loop",""+opData.get(i).getNumber());
                View itemview = inflater.inflate(R.layout.previousorders_list_single_items, null);
                TextView Number = (TextView)itemview.findViewById(R.id.Number);
                TextView RechargeAmount = (TextView)itemview.findViewById(R.id.RechargeAmount);


                TextView Rechargestatus = (TextView)itemview.findViewById(R.id.Rechargestatus);
                Number.setTypeface(custom_regular);
                RechargeAmount.setTypeface(custom_regular);
                Rechargestatus.setTypeface(custom_regular);

                Number.setText(opData.get(i).getNumber());
                RechargeAmount.setText("Rs."+opData.get(i).getRechargeAmount());
                Rechargestatus.setText(opData.get(i).getRechargestatus());

                viewHolder.ll_individuvalitem.addView(itemview);
            }
        }



        return convertView;
    }


    class ViewHolder {
        TextView date, tx_month, orderid, amount,status;
        LinearLayout ll_individuvalitem,ordersinfo;
    }

    private class GetReorder extends AsyncTask<String, Void, JSONObject> {
        /*String mJourneyDate;
        public GetData(String pJourneyDate) {
            this.mJourneyDate = pJourneyDate;
        }*/
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // pDialog = ProgressDialog.show(getActivity(), "", "");


        }

        @Override
        protected JSONObject doInBackground(String... params) {
            String response;

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(params[0]);
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
                nameValuePair.add(new BasicNameValuePair("OrderID", g_orderid));
                nameValuePair.add(new BasicNameValuePair("DeviceID", m_sharedPreference.getString("deviceId","")));
                nameValuePair.add(new BasicNameValuePair("PlatformID", "3"));
                 nameValuePair.add(new BasicNameValuePair("UserUniqueID", m_sharedPreference.getString("useruid","")));



                httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                HttpResponse responce = httpclient.execute(httppost);

                HttpEntity httpEntity = responce.getEntity();

                response = EntityUtils.toString(httpEntity);
                 Log.d("response is", response);

                return new JSONObject(response);

            } catch (Exception ex) {

                ex.printStackTrace();

            }

            return null;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            String Status;

            super.onPostExecute(result);
            //  Log.v("TAG_RESULT",""+result);



            if (result != null) {
                try {
             Intent intent= new Intent(activity, Payment_Actiivity.class);
                    intent.putExtra("jsonfrompayment",result.toString());
                    Log.v("TAG_JSONPAY",""+result.toString());

                    activity.startActivity(intent);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(activity, "something went wrong", Toast.LENGTH_LONG).show();

            }
        }
    }

}
Seclusive answered 1/3, 2015 at 5:21 Comment(0)
D
111

In your adapter class override these two methods

@Override
  public int getViewTypeCount() {

   return getCount();
  }

  @Override
  public int getItemViewType(int position) {

   return position;
  }

Note: For recyclerview just method getItemViewType(int position) will work.

Dysphemia answered 1/3, 2015 at 7:3 Comment(4)
Its working fine. But question remains how is it working??Precipitant
@Precipitant Actually these methods tells the system that whether adapter needs to recycle the rows or not, and also how many types of rows we want to display. for details information about this go through this link: https://mcmap.net/q/149648/-getviewtypecount-and-getitemviewtype-methods-of-arrayadapterDysphemia
Still not resolve. I want to change color text of selected row and reset colour of previous. It's not working if scrolling case. 1. Select first row 2. Scroll to bottom and select latest row...( First row is not reset color ) Please help meKneehole
God bless. Adapter view and position seemed to mismatch. It would assign ImageView to the wrong view. With this it works.Immortalize
M
1

I had the same issue then below link resolved my problem ;

https://mcmap.net/q/37223/-android-listview-with-different-layouts-for-each-row

Basically, you should specify your item types then reconfigure your adapter via getItemViewType(int position) and getViewTypeCount() override methods.

If you look into above link, you will get it.

Micronutrient answered 15/8, 2016 at 12:57 Comment(0)
M
0

you are using two arrays - "bpData" and "opData" and you only reset if under specific conditions with "opData".

This is a bad design. You need one array - if "opData" is dependent on "bpData" then make it a variable of "bpData".

Or else write something to synchronize the two. Otherwise, you do not know how the display will work.

Memling answered 1/3, 2015 at 6:1 Comment(1)
please tell me how todesignSeclusive

© 2022 - 2024 — McMap. All rights reserved.