滚动android时列表视图项改变位置?

时间:2015-03-01 05:21:54

标签: android android-listview

在我的应用程序中,我使用自定义列表视图适配器。在列表视图项中,我添加了另一个布局。因为在我的服务中,一个订单有任意数量的订单商品。为了显示订单项目,我将下面的订单商品布局添加到自定义列表视图item.it工作正常。但滚动列表视图时,数据会发生变化。

适配器类:

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

            }
        }
    }

}

4 个答案:

答案 0 :(得分:99)

在适配器类中重写这两个方法

@Override
  public int getViewTypeCount() {

   return getCount();
  }

  @Override
  public int getItemViewType(int position) {

   return position;
  }

答案 1 :(得分:1)

我遇到了同样的问题,然后链接解决了我的问题;

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

基本上,您应指定项目类型,然后通过getItemViewType(int position)getViewTypeCount()覆盖方法重新配置适配器。

如果你查看上面的链接,你会得到它。

答案 2 :(得分:0)

您正在使用两个数组 - “bpData”和“opData”,只有在特定条件下使用“opData”时才会重置。

这是一个糟糕的设计。你需要一个数组 - 如果“opData”依赖于“bpData”,那么使它成为“bpData”的变量。

或者写点东西来同步两者。否则,您不知道显示器的工作方式。

答案 3 :(得分:0)

@Override
  public int getViewTypeCount() {

   return getCount();
  }

  @Override
  public int getItemViewType(int position) {

   return position;
  }

适合我的作品