Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

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

            }
        }
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...