2

I want to increment/decrement the count and update it to the database when click on button. the problem is button is on adapter, how should i apply an listener to individual button and update the count for particular user on UI as well as in Database.

enter image description here

What I have achieved: above image shows what i have created. www, aaa, xxx are the users and when I click on the + or - button I want to increment the count and also update in database. below is my code for this all what I have done:

Admin_Dashbord.java Activity Class

public class Admin_Dashbord extends Activity 
{
    //TextView a_shop_key,a_shop_name,a_shop_owner,a_shop_ophone,a_shop_address1,a_shop_address2,a_shop_address3,a_e_names;
    String key,name,owner,ownerphone,address1,address2,address3,empnames,n_emp1,txtname="",getdata;
    int n_emp;
    Button submit;
    TextView txtv_shopname;
    LinearLayout linear;
    Customer_Adapter custom;
    //ArrayList<No_Of_Emp_Pojo> getdata;

    ArrayList<No_Of_Emp_Pojo> newdata=new ArrayList<No_Of_Emp_Pojo>();
    ArrayList<No_Of_Emp_Pojo> temp=new ArrayList<No_Of_Emp_Pojo>();
    Network_Class net=new Network_Class();
    ListView li;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_dashbord);

        linear=(LinearLayout)findViewById(R.id.admin_dashbord_layout2);
        submit=(Button)findViewById(R.id.dashbord_shop_submit);
        li=(ListView)findViewById(R.id.emp_list);
        txtv_shopname=(TextView)findViewById(R.id.shopname);


        Intent intent=getIntent();
        Bundle b=intent.getExtras();
        key=b.getString("SHOP_ID");
        name=b.getString("SHOP_NAME");
        owner=b.getString("OWNER_NAME");
        ownerphone=b.getString("OWNER_PHONE");
        address1=b.getString("ADDRESS1");
        address2=b.getString("ADDRESS2");
        address3=b.getString("ADDRESS3");
        //empnames=b.getString("EMP_NAMES");
        n_emp1=b.getString("N_EMP");
        n_emp=Integer.parseInt(b.getString("N_EMP"));

        txtv_shopname.setText(name);



        newdata=getdetail(key);
        System.out.println("This is Admin Dashboard");
        custom=new Customer_Adapter(getApplicationContext(), R.layout.shop_listview_item, newdata);
        li.setAdapter(custom);
        //newdata.clear();
        custom.notifyDataSetChanged();

    }   
    public ArrayList<No_Of_Emp_Pojo> getdetail(final String key)
    {
        Thread th=new Thread(new Runnable() 
        {
            @Override
            public void run() 
            {
                getdata=net.n_employees(key);
                System.out.println("In Thread first");
                temp = no_emp(getdata);
            }
        });

        th.start();
        try
        {
            th.join();
        }
        catch(Exception e)
        {
            System.out.println("Thread Jpoin Problem "+e.getMessage());

        }

        return temp;
    }

    public ArrayList<No_Of_Emp_Pojo> no_emp(String result)
    {
        ArrayList<No_Of_Emp_Pojo> emp=new ArrayList<No_Of_Emp_Pojo>();
        try
        {
            JSONArray j_array=new JSONArray(result);
            for(int i=0; i<j_array.length(); i++)
            {
                JSONObject j_object=j_array.getJSONObject(i);
                No_Of_Emp_Pojo no=new No_Of_Emp_Pojo();

                no.setId(j_object.getInt("emp_id"));
                no.setCount(j_object.getInt("count"));
                no.setE_name(j_object.getString("emp_name"));

                emp.add(no);
            }
        }   
            catch(JSONException e)
            {
                Log.e("Log_tag", "Error Parsing in Area......"+e.toString());               
                Log.e("Log_Answers","Second Error Parsing in Area......"+result);
            }
        System.out.println("This is parsing  "+emp.toString());
        return emp;
    }

}

Customer_Adapter.java

  public class Customer_Adapter extends ArrayAdapter<No_Of_Emp_Pojo>
{
    private int res;
    private LayoutInflater inflaters;
    Button signal,increment,decrement;
    public Customer_Adapter(Context context, int resource, List<No_Of_Emp_Pojo> objects) 
    {
        super(context, resource, objects);
        res=resource;
        inflaters=LayoutInflater.from(context);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        convertView=(LinearLayout)inflaters.inflate(res, null);
        TextView count=(TextView)convertView.findViewById(R.id.show_waiting);
        TextView name=(TextView)convertView.findViewById(R.id.show_emp_name);
        signal=(Button)convertView.findViewById(R.id.green_signal);
        increment=(Button)convertView.findViewById(R.id.pluse_signal);
        decrement=(Button)convertView.findViewById(R.id.minus_signal);


        count.setTextColor(Color.parseColor("#000000"));
        name.setTextColor(Color.parseColor("#000000"));

        No_Of_Emp_Pojo no=getItem(position);
        count.setText(Integer.toString(no.getCount()));
        name.setText(no.getE_name());

        increment.setOnClickListener(no.addlistner);
        decrement.setOnClickListener(no.sublistner);

        return convertView;
    }
}

No_Of_Emp_Pojo.java

  public class No_Of_Emp_Pojo 
{
    int count,id;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    String e_name;
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public String getE_name() {
        return e_name;
    }
    public void setE_name(String e_name) {
        this.e_name = e_name;
    }

    public OnClickListener addlistner=new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("Add Count is "+ getId());

        }
    };

    public OnClickListener sublistner=new OnClickListener() 
    {       
        @Override
        public void onClick(View v) 
        {
            System.out.println("Minus Count is "+getId());

        }
    };

}

I surfed and I found, from that I wrote the OnClickListener in No_Of_Emp_Pojo class when I click on button it prints id But i am not able to update it on UI as well as in database because it gives me the listener in pojo so what to do to get it on Admin_Dashbord.java activity so that it will become more easy to update the count on adapter as well as the value in database.

Soham s More
  • 95
  • 12
  • Instead of writing Listeners in POJO ,write in Adapter and register listeners to appropriate buttons you want in getView() of adapter. once see this to write effective adapter http://www.piwai.info/android-adapter-good-practices/ – shobhan Jun 03 '15 at 06:15

4 Answers4

3

Instead of POJO write inside getView method of the adapter

Button button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                     Toast.makeText(context,"your msg", Toast.LENGTH_LONG)
                    }
            });

basically adapter sets the view for each element of the list differently so for setting listener on the button will also work dynamically as required for reference check.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Maniya Joe
  • 761
  • 6
  • 24
0

In the Adapter, getView method, write onClickListener for specific buttons. You should maintain proper array/pojo files to save the numbers of clicks in the textview.

Pavandroid
  • 1,586
  • 2
  • 15
  • 30
0

Add click listener on button of item in getView: increment.setOnClickListener(this); decrement.setOnClickListener(this);

then add tag of that object on these button in getView: increment.setTag(no); // "no" is your cuttent object(mode) decrement.setTag(no); // "no" is your cuttent object(mode)

Now implement Onclick method in adapter:

public void onClick(View v) {
  switch(v.getId()) {
     case R.id.pluse_signal:
       No_Of_Emp_Pojo selectedItem = (No_Of_Emp_Pojo)v.getTag();
       //now you can update this item in db or in any place.
       break;
     case R.id.minus_signal:
       No_Of_Emp_Pojo selectedItem1 = (No_Of_Emp_Pojo)v.getTag();
       //now you can update this item in db or in any place.
       break;
}
}

And this click listnder will be called for every item of listview. And you can get model related to every item.

Qandil Tariq
  • 539
  • 1
  • 3
  • 15
  • I tried what u gave, this just increment the value in last element in adapter I wrote this case: case R.id.pluse_signal: No_Of_Emp_Pojo selectedItem = (No_Of_Emp_Pojo)v.getTag(); int count_val=selectedItem.getCount(); count_val++; count.setText(Integer.toString(count_val)); break; here i came to khow count.setText sets text to last element how set it for particular element. that is if i clicked + in front of www the count in front of www should be incremented. @Qandil Tariq – Soham s More Jun 03 '15 at 08:43
0

Write you listener on Custom_Adapter.java not on No_Of_Emp_Pojo.java

Try this code on Customer_Adapter.java

 public class Customer_Adapter extends ArrayAdapter<No_Of_Emp_Pojo>
{
    private int res;
    private LayoutInflater inflaters;
    Button signal,increment,decrement;
    public Customer_Adapter(Context context, int resource, List<No_Of_Emp_Pojo> objects) 
    {
        super(context, resource, objects);
        res=resource;
        inflaters=LayoutInflater.from(context);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        convertView=(LinearLayout)inflaters.inflate(res, null);
        TextView count=(TextView)convertView.findViewById(R.id.show_waiting);
        TextView name=(TextView)convertView.findViewById(R.id.show_emp_name);
        signal=(Button)convertView.findViewById(R.id.green_signal);
        increment=(Button)convertView.findViewById(R.id.pluse_signal);
        decrement=(Button)convertView.findViewById(R.id.minus_signal);


        count.setTextColor(Color.parseColor("#000000"));
        name.setTextColor(Color.parseColor("#000000"));

        No_Of_Emp_Pojo no=getItem(position);
        count.setText(Integer.toString(no.getCount()));
        name.setText(no.getE_name());

        increment.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {
             //your incrementation code here
             }
        });

        decrement.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View arg0) {
             //your decrementation code here
             }
        });

        return convertView;
    }
}