更改listview的一个子布局,反映在其他listview子布局中

时间:2016-08-01 06:37:20

标签: android listview layout-inflater

我创建了一个扩展布局的listview。膨胀的布局有一个edittext和两个按钮,用于增加和减少edittext中的值。但问题是如果我在listview中更改第一个edittext的值,则更改会反映在listview的第四个edittext(第四个子项)中。类似地,第二个edittext中的更改反映到第五个edittext,依此类推。我在listview中编写的用于扩充布局的代码如下所示。请帮我解决这个问题。

public class DisplayList extends AppCompatActivity {

private static final String TAG_CATEGORY = "main_name";
private static final String TAG_SUBCAT = "cat_name";
private static final String TAG_CODE = "code";
private static final String TAG_NAME = "name";
private static final String TAG_RATE = "rate";
private static final String TAG_RATE2 = "rate2";
private static final String TAG_RATE3 = "rate3";
private static final String TAG_IMAGE = "img";

ImageAdapter mImageAdapter = null;
ListView mListView = null;
DatabaseHelper myDb;
FloatingActionButton fab1;

public  ArrayList<DetailsVO> mListDetails = new ArrayList<DetailsVO>();

String subc;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_list);

    myDb=new DatabaseHelper(DisplayList.this);

    fab1 = (FloatingActionButton) findViewById(R.id.fab);
    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(DisplayList.this,NewCart.class);
            startActivity(intent);
        }
    });

    mListView = (ListView) findViewById(R.id.list_view);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        subc = extras.getString("Subcat");
    }

    new AsyncCallSoap1().execute();



}

public class AsyncCallSoap1 extends AsyncTask<String,Void,String> {
    private final ProgressDialog dialog=new ProgressDialog(DisplayList.this);

    @Override
    protected String doInBackground(String... params) {
        CallSoap com=new CallSoap();
        String SOAP_ACTION="http://tempuri.org/IprotechService/SelectSuCatgeory";
        String OPERATION_NAME="SelectSuCatgeory";
        String response = com.getDetails(OPERATION_NAME,SOAP_ACTION);

        if(response!=null){
            try {

                JSONArray array1 = new JSONArray(response);

                for(int i=0;i<array1.length();i++)
                {

                    final DetailsVO detailsVO = new DetailsVO();
                    JSONObject obj1 = array1.getJSONObject(i);

                    String out_category = obj1.getString(TAG_CATEGORY);
                    String out_subcat = obj1.getString(TAG_SUBCAT);
                    String out_code = obj1.getString(TAG_CODE);
                    String out_name = obj1.getString(TAG_NAME);
                    String out_rate = obj1.getString(TAG_RATE);
                    String out_rate2 = obj1.getString(TAG_RATE2);
                    String out_rate3 = obj1.getString(TAG_RATE3);
                    String out_img = obj1.getString(TAG_IMAGE);

                    if(subc.equalsIgnoreCase(out_subcat)){
                        detailsVO.setImage(out_img);
                        detailsVO.setName(out_name);
                        detailsVO.setRate(out_rate);
                        detailsVO.setRate2(out_rate2);
                        detailsVO.setRate3(out_rate3);

                        mListDetails.add(detailsVO);}
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Showing progress dialog
        dialog.setMessage("Please wait...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();

        if(mListDetails != null){
            mImageAdapter = new ImageAdapter(DisplayList.this, mListDetails);
            mListView.setAdapter(mImageAdapter);

        }

    }
}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<DetailsVO> mItems;
    private ArrayList<DetailsVO> mOriginalItems;


    public ImageAdapter(Context context, ArrayList<DetailsVO> items) {
        this.mContext = context;
        this.mItems = items;
    }

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

    @Override
    public DetailsVO getItem(int position) {
        return mItems.get(position);
    }

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

    public ArrayList<DetailsVO> getItems(){
        return mItems;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_layout, null);

        }

        DetailsVO detailsVO = mItems.get(position);

        View listView = (View) convertView;

        ImageView image = (ImageView) listView.findViewById(R.id.imageView2);

        final TextView textTitle = (TextView) listView.findViewById(R.id.textView2);

        final Spinner spinner=(Spinner) listView.findViewById(R.id.spinner);

        Button btnplus = (Button) listView.findViewById(R.id.max);
        Button btnminus = (Button) listView.findViewById(R.id.min);
        final Button btnadd=(Button) listView.findViewById(R.id.btn_add);
        btnadd.setClickable(true);

        final TextView quantity = (TextView) listView.findViewById(R.id.quantity);

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

        rate.add("Rs."+detailsVO.getRate());
        if(detailsVO.getRate2()!= null && !detailsVO.getRate2().isEmpty())
        {
            rate.add("Rs."+detailsVO.getRate2());
        }
        if(detailsVO.getRate3()!= null && !detailsVO.getRate3().isEmpty())
        {
            rate.add("Rs."+detailsVO.getRate3());
        }
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(DisplayList.this, android.R.layout.simple_spinner_item,rate);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btnminus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String value = quantity.getText().toString();
                int quantity1 = Integer.parseInt(value);

                if(quantity1>0)
                {
                    quantity1 = quantity1 -1;

                    String t = Integer.toString(quantity1);
                    quantity.setText(t);
                }
            }
        });


        btnplus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String value1 = quantity.getText().toString();
                int quantity2 = Integer.parseInt(value1);
                quantity2 = quantity2 + 1;

                String t=Integer.toString(quantity2);

                quantity.setText(t);
                   }
        });

        final  String u=detailsVO.getImage();
        btnadd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String im="http://protech.absoftwaresolution.com/img/grocery_imgs/grains/"+u;
                String nm = textTitle.getText().toString();
                String value = quantity.getText().toString();
                int q= Integer.parseInt(value);

                String rate=spinner.getSelectedItem().toString();
                String val = rate.substring(rate.lastIndexOf("-")- 1);
                final String val1=rate.substring(3,rate.lastIndexOf("-"));
                int b = Integer.parseInt(val1.trim());
                final int  a= q*b;
                String c=Integer.toString(a);

                boolean isInserted = myDb.insertData(im,nm,val1,q,a);
                if (isInserted == true) {
                    Toast.makeText(DisplayList.this, "Item Added Successfully!!!", Toast.LENGTH_LONG).show();

                }

                  }

        });

        Picasso.with(mContext)
                .load("http://protech.absoftwaresolution.com/img/grocery_imgs/grains/"+detailsVO.getImage())
                .placeholder(R.drawable.placeholder)
                .into(image);


        textTitle.setText(detailsVO.getName());

        return listView;
    }

}

当我点击btnplus&amp; btnminus我正在更改edittext值,该值反映在listview

中的第四个下一个edittext中

1 个答案:

答案 0 :(得分:1)

我认为这种情况正在发生,因为你的监听器是在getView()中声明的。当您向上和向下滚动时,ListView会回收视图及其引用。基本上,当您单击第一行中的某些内容时,ListView将更改这些对其他对象的引用。如果没有视图ID或标记来跟踪它,那么这些点击就会转移到其他位置。

我尝试解决此问题的方法是在适配器外为listview项声明一个OnItemClickListener。在其中,检测单击哪个子视图(按钮+/-),然后相应地调整您的字符串。一旦你有一个对容器/ ViewGroup的引用,你应该设置...