Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List view items changes position when scrolling android?

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

            }
        }
    }

}
like image 873
android learner Avatar asked Mar 01 '15 05:03

android learner


People also ask

How do I make list view scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .

How do I stop list view from scrolling?

Just call stopScroll(myListView); when you need to stop scroll. Show activity on this post. // Stop scrolling smoothScrollBy(0, 0);

Is List view scrollable by default?

ListView itself is scrollable.

How do I edit list view in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };


2 Answers

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.

like image 170
Rahul Sharma Avatar answered Oct 19 '22 10:10

Rahul Sharma


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

https://stackoverflow.com/a/36738935/3341089

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.

like image 1
oguzhan Avatar answered Oct 19 '22 12:10

oguzhan