我想增加/减少计数,并在点击按钮时将其更新到数据库。问题是按钮是在适配器上,我应该如何将监听器应用于单个按钮并更新UI和数据库中特定用户的计数。
我取得的成就: 上面的图像显示了我创建的内容。 www,aaa,xxx是用户,当我点击+或 - 按钮时,我想增加计数并在数据库中更新。 下面是我所做的所有代码:
Admin_Dashbord.java活动类
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());
}
};
}
我冲浪了,我发现,当我点击按钮时我在No_Of_Emp_Pojo类中写了OnClickListener它打印id但是我无法在UI和数据库中更新它因为它给了我pojo的监听器所以如何在Admin_Dashbord.java活动上获取它,以便更新适配器上的计数以及数据库中的值。
答案 0 :(得分:3)
而不是POJO写入适配器的getView方法
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)
}
});
基本上适配器为列表的每个元素设置不同的视图,因此在按钮上设置监听器也将根据reference check的需要动态工作。
答案 1 :(得分:0)
在Adapter,getView方法中,为特定按钮编写onClickListener。您应该维护正确的数组/ pojo文件以保存textview中的点击次数。
答案 2 :(得分:0)
在getView中的项目按钮上添加单击侦听器: increment.setOnClickListener(本); decrement.setOnClickListener(本);
然后在getView中的这些按钮上添加该对象的标记: increment.setTag(无); //&#34;不&#34;是你的cuttent对象(模式) decrement.setTag(无); //&#34;不&#34;是你的cuttent对象(模式)
现在在适配器中实现Onclick方法:
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;
}
}
此列表视图的每个项目都会调用此点击列表。并且您可以获得与每个项目相关的模型。
答案 3 :(得分:0)
在Custom_Adapter.java上编写监听器而不是No_Of_Emp_Pojo.java
在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;
}
}