如何在 android 中动态地将侦听器添加到 customAdapter 上的按钮
How to dynamically add listener to button on customAdapter in android
我想 increment/decrement 计数并在单击按钮时将其更新到数据库。问题是按钮在适配器上,我应该如何将侦听器应用于单个按钮并更新 UI 以及数据库中特定用户的计数。
我的收获:
上图显示了我创建的内容。 www、aaa、xxx 是用户,当我单击 + 或 - 按钮时,我想增加计数并更新数据库。
以下是我为此所做的所有代码:
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());
}
};
}
我上网发现,我在 No_Of_Emp_Pojo class 中编写了 OnClickListener,当我点击按钮时它会打印 id 但我无法在 UI 上更新它以及数据库中,因为它给了我 pojo 中的侦听器,所以如何在 Admin_Dashbord.java activity 上获取它,这样更新适配器上的计数以及中的值将变得更加容易数据库。
在Adapter中,getView方法,为特定的按钮写onClickListener。您应该维护适当的 array/pojo 文件以保存文本视图中的点击次数。
在适配器的 getView 方法中写入而不是 POJO
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 的要求动态工作。
在getView中的项目按钮上添加点击监听器:
increment.setOnClickListener(这个);
decrement.setOnClickListener(这个);
然后在 getView 中的这些按钮上添加该对象的标签:
increment.setTag(否); // "no" 是你的 cuttent 对象(模式)
decrement.setTag(否); // "no" 是你的 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;
}
}
并且将为列表视图的每个项目调用此点击列表。并且您可以获得与每个项目相关的模型。
在 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;
}
}
我想 increment/decrement 计数并在单击按钮时将其更新到数据库。问题是按钮在适配器上,我应该如何将侦听器应用于单个按钮并更新 UI 以及数据库中特定用户的计数。
我的收获: 上图显示了我创建的内容。 www、aaa、xxx 是用户,当我单击 + 或 - 按钮时,我想增加计数并更新数据库。 以下是我为此所做的所有代码:
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());
}
};
}
我上网发现,我在 No_Of_Emp_Pojo class 中编写了 OnClickListener,当我点击按钮时它会打印 id 但我无法在 UI 上更新它以及数据库中,因为它给了我 pojo 中的侦听器,所以如何在 Admin_Dashbord.java activity 上获取它,这样更新适配器上的计数以及中的值将变得更加容易数据库。
在Adapter中,getView方法,为特定的按钮写onClickListener。您应该维护适当的 array/pojo 文件以保存文本视图中的点击次数。
在适配器的 getView 方法中写入而不是 POJO
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 的要求动态工作。
在getView中的项目按钮上添加点击监听器: increment.setOnClickListener(这个); decrement.setOnClickListener(这个);
然后在 getView 中的这些按钮上添加该对象的标签: increment.setTag(否); // "no" 是你的 cuttent 对象(模式) decrement.setTag(否); // "no" 是你的 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;
}
}
并且将为列表视图的每个项目调用此点击列表。并且您可以获得与每个项目相关的模型。
在 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;
}
}