0

enter image description here

there to textView sub and add. on click of add value will be add and if sub then value will be cut. adapter class.

public class ListAdapter extends ArrayAdapter {

    ArrayList<RowModel> model;
    Context mContext;

    ArrayList<Integer> selections;

    static class ViewHolder
    {
        TextView text,edittext;
        ImageView indicator;
        ImageView image;
        TextView sub,add;
    }

    public ListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mContext = context;
        model = new ArrayList<RowModel>();
        selections = new ArrayList<Integer>();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if(row == null)
        {
            LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.list_row, null);
            final ViewHolder holder = new ViewHolder();
            holder.text = (TextView) row.findViewById(R.id.row_txt);
            holder.image = (ImageView)row.findViewById(R.id.row_img);
            holder.edittext = (TextView) row.findViewById(R.id.edittext);
            holder.sub = (TextView) row.findViewById(R.id.listsub);
            holder.add = (TextView) row.findViewById(R.id.listadd);
            //holder.edittext.setText("0");
            holder.indicator = (ImageView)row.findViewById(R.id.row_indicator);

            ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF007d7b"));
            holder.indicator.setImageDrawable(cd);
            row.setTag(holder);
        }


        final ViewHolder vh = (ViewHolder)row.getTag();
        vh.image.setImageResource(model.get(position).resID);
        vh.text.setText(model.get(position).country);
        vh.edittext.setText(model.get(position).editText1);



        vh.sub.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //notifyDataSetChanged();
            //  setNotifyOnChange(true);
                int i = Integer.parseInt(vh.edittext.getText().toString());
                i--;
                vh.edittext.setText(""+i);
                notifyDataSetChanged();

            }
        });
        vh.add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //notifyDataSetChanged();
                int i = Integer.parseInt(vh.edittext.getText().toString());
                i++;
                vh.edittext.setText(""+i);


            }
        });
        return row;
    }



    public String getvalue(int i)
    {
        String ii = null;
        if(!this.model.get(i).editText1.equals("0"))
        {
            ii = this.model.get(i).country + " "+this.model.get(i).editText1;
        }
        return ii;
    }


}

now code for button,need to all value of qty on button click from main activity

public class MainActivity extends Activity {

    private ListView list;
    private ListAdapter listAdapter;
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list =(ListView) this.findViewById(R.id.list);
        button1 = (Button)findViewById(R.id.button1);
        listAdapter = new ListAdapter(this, 0);
        this.initAdapter();
        list.setAdapter(listAdapter);

        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                for(int i = 0; i <listAdapter.getCount();i++)
                {
                    //listAdapter.updateSelection(i);

                     Toast.makeText(MainActivity.this, listAdapter.getvalue(i), 1).show();
                     //listAdapter.notifyDataSetChanged();  
                     ((ListAdapter)listAdapter).notifyDataSetChanged();
                }

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void initAdapter()
    {
        RowModel row = new RowModel(R.drawable.china, "China","0");
        listAdapter.addRow(row);
        RowModel row2 = new RowModel(R.drawable.japan, "Japan","1");
        listAdapter.addRow(row2);
        RowModel row3 = new RowModel(R.drawable.korea, "Korea","2");
        listAdapter.addRow(row3);
        RowModel row4 = new RowModel(R.drawable.america, "America","3");
        listAdapter.addRow(row4);
        RowModel row5 = new RowModel(R.drawable.canada, "Canada","4");
        listAdapter.addRow(row5);
        RowModel row6 = new RowModel(R.drawable.england, "England","5");
        listAdapter.addRow(row6);
        RowModel row7 = new RowModel(R.drawable.france, "France","06");
        listAdapter.addRow(row7);
    }
}

my get/set class

public class RowModel {

    int resID;
    String country;
    String editText1;

    public RowModel(int rid, String country,String editText1)
    {
        this.resID = rid;
        this.country = country;
        this.editText1 = editText1;
    }
    public String getEditText1() {
        return editText1;
    }
    public void setEditText1(String editText1) {
        this.editText1 = editText1;
    }
}
tej shah
  • 2,995
  • 2
  • 25
  • 35

2 Answers2

1

Do the following changes:--

> public class ListAdapter extends ArrayAdapter<RowModel>{

Get the row of the underlying model in your getView method as:--

> final RowModel row = getItem(position);

Now you need to play with the rowmodel, not the layout text fields. This is your on sub click:--

vh.sub.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int i = Integer.parseInt(row.getEditText1());
                    i--;
                    row.setEditText1(Integer.toString(i));
                   // vh.edittext.setText("" + i); --this is not at all required
                    notifyDataSetChanged();
                }
            });

similar is for other add click notifyDataSetChanged() will work on adapter and you are not at all updating the adapter in your code.

I would suggest you to have a look at this post. It nicely explains the working of adapter. This should work

Community
  • 1
  • 1
Nicks
  • 16,030
  • 8
  • 58
  • 65
  • thank so much. please can you see that qus and ans for that? http://stackoverflow.com/questions/31849119/how-to-get-update-values-from-expandable-listview – tej shah Aug 06 '15 at 09:41
  • Glad it helped :-). I will check it tomorrow in case no one answered. – Nicks Aug 06 '15 at 10:27
0

You are trying to do something and reverting back.Let me explain you in details in onclick listener you are taking the edittext and add the value to that is not the correct way rather you can access the object and change the value of edittext1 and then call notifydatasetchanged that will do your work.