点击按钮我试图使用谷歌的凌空库将一些数据发送到我的PHP脚本。发送数据的方法在Activity内部,我编写了一个接口来调用该方法 定制列表适配器。
以下是我的适配器代码:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="GridItem" TargetType="DataGridRow">
<Setter Property="BorderThickness" Value="3"/>
</Style>
<Style x:Key="GridSeparator" TargetType="DataGridRow">
<Setter Property="BorderBrush" Value="Red"/>
</Style>
</ResourceDictionary>
当按下列表中的按钮时,理想情况下应该调用以下代码:
public class RoleList extends ArrayAdapter<String> {
public static final String DELETEUSERINFO = VirtualMachineIp.VMIP_ADDRESS.trim() + "goldmine/getuserinfo.php";
public static final String KEY_NAME = "name";
public static final String KEY_ROLE = "role";
private ArrayList<String> name;
private ArrayList<String> username;
private ArrayList<String> password;
private ArrayList<String> role;
private Activity context;
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
int pos;
public boolean isDelPressed() {
return delPressed;
}
public void setDelPressed(boolean delPressed) {
this.delPressed = delPressed;
}
boolean delPressed;
private onDelCallback listener;
public RoleList(Activity context, ArrayList<String> name, ArrayList<String> username, ArrayList<String> password, ArrayList<String> role, onDelCallback listener) {
super(context, R.layout.role_list, name);
this.context = context;
this.name = name;
this.username = username;
this.password = password;
this.role = role;
this.listener = listener;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View listViewItem = inflater.inflate(R.layout.role_list, null, true);
final TextView textViewName = (TextView) listViewItem.findViewById(R.id.tv_empname);
final TextView textViewusername = (TextView) listViewItem.findViewById(R.id.tv_empusername);
final TextView textViewPass = (TextView) listViewItem.findViewById(R.id.tv_emppassword);
final TextView textViewRole = (TextView) listViewItem.findViewById(R.id.tv_emprole);
Button edit = (Button) listViewItem.findViewById(R.id.btn_editRole);
Button delete = (Button) listViewItem.findViewById(R.id.btn_delRole);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setDelPressed(true);
name.remove(position);
username.remove(position);
password.remove(position);
role.remove(position);
listener.deleteuser(position);
notifyDataSetChanged();
setPos(position);
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Emp Info", name.get(position) + " " + username.get(position) + " " + password.get(position) + " " + role.get(position));
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.userreg);
dialog.setTitle("Edit Employee " + name.get(position) + " details");
final String[] arraySpinner = new String[]{"Manager", "Stockist", "Cashier", "Accountant"};
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
final EditText emp_name = (EditText) dialog.findViewById(R.id.editTextName);
final EditText emp_uname = (EditText) dialog.findViewById(R.id.editTextUserName);
final EditText emp_pw = (EditText) dialog.findViewById(R.id.editTextPassword);
final Spinner emp_role = (Spinner) dialog.findViewById(R.id.spinner_role);
final TextView textRole = (TextView) dialog.findViewById(R.id.tv_selected_role);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, arraySpinner);
emp_role.setAdapter(adapter);
emp_role.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "Role Selected is " + arraySpinner[position], Toast.LENGTH_SHORT).show();
String employee_role = arraySpinner[position];
textRole.setText(employee_role);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
emp_name.setText(name.get(position));
emp_uname.setText(username.get(position));
emp_pw.setText(password.get(position));
emp_role.setSelection(position);
Button buttoncancel = (Button) dialog.findViewById(R.id.buttonCancel);
buttoncancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button buttonChange = (Button) dialog.findViewById(R.id.buttonRegister);
buttonChange.setText("Change");
buttonChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textViewName.setText(emp_name.getText().toString());
textViewusername.setText(emp_uname.getText().toString());
textViewPass.setText(emp_pw.getText().toString());
textViewRole.setText(textRole.getText());
dialog.dismiss();
}
});
dialog.show();
}
});
textViewName.setText(name.get(position));
textViewusername.setText(username.get(position));
textViewPass.setText(password.get(position));
textViewRole.setText(role.get(position));
return listViewItem;
}
public interface onDelCallback {
void deleteuser(int pos);
}
}
我在parseUserinfo()中设置适配器,该适配器在@Override
public void deleteuser(final int pos)
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, DELETEUSERINFO, new Response.Listener<String>() {
@Override
public void onResponse(String response)
{
String resp = response.toString().trim();
if (resp.equals("UNABLE TO DELETE USER"))
{
Log.d("UNABLE TO DEL",resp);
}else if (resp.equals("USER DELETED SUCCESSFULLY"))
{
Toast.makeText(UserRegistration.this, "USER DELETED SUCCESSFULLY", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(UserRegistration.this, "Please check your internet connection", Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put(KEY_NAME, employee_name.get(pos));
params.put(KEY_ROLE,employee_role.get(pos));
return params;
}
};
RequestQueue rq = Volley.newRequestQueue(this);
rq.add(stringRequest);
}
的响应下运行,我在getUserinfo()
内调用它来从远程服务器请求数据。
这是我设置适配器的方式:
onCreate().getUserinfo()
我想知道我是否在正确的位置调用适配器。我需要再打电话吗?
感谢任何帮助或建议。谢谢。
答案 0 :(得分:0)
您可以通过某些方式从适配器调用您的活动方法:
当你在那时创建适配器的构造函数时,可以在其参数中传递活动对象,然后在适配器中使用该对象。
如果您在适配器中创建接口,则必须在相应的活动中实现该接口,然后您可以通过适配器构造函数传递活动对象,并且可以使用您所需的方法。
答案 1 :(得分:0)
public RoleList(Activity context,ArrayList name,ArrayList username,ArrayList password,ArrayList role,onDelCallback listener){} 在上面的构造函数中,你必须传递相应的Activity对象,例如MyActvity mActivity,而不是传递通用的Activity对象。我希望您的方法在MyActvity类中可用,并且还考虑到您已在MyActvity类上实现了该接口。
答案 2 :(得分:0)
您可以在此适配器中执行活动的删除任务
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserRegistration act = (UserRegistration) context;
act.deleteuser(position);
}
});