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.
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.