【发布时间】:2016-02-18 07:25:10
【问题描述】:
我的片段类中的按钮列表视图是使用自定义适配器填充的。当按下列表视图中存在的按钮时,我想删除该项目。这里我使用了notifyDataSetChanged从我的适配器类中单击按钮,但它不反映任何结果。谁能告诉我如何从适配器类更新列表视图
这是我在单击按钮后在适配器类中编写的代码
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyDataSetChanged();
}
});
我的完整适配器代码
import java.util.List;
import java.util.Objects;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.lotus.com.androidmis.R;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.lotus.mis.modle.Complaints.Complaint;
import com.lotus.mis.modle.MyAppApplication;
import com.subtabs.complaints.Complaints_pending;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Text;
public class Support_ComplainAdapter extends BaseAdapter {
Context context;
List<Complaint> objects;
ProgressDialog progressdialog;
String UserID;
MyAppApplication mApp;
public Support_ComplainAdapter(Context context,int resource,List<Complaint> objects)
{
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// View POItemRow = inflater.inflate(R.layout.activity_poitem_listrow,parent,false);
View PurchaseOrderRow = LayoutInflater.from(context).inflate(R.layout.layout_complaint_adapter,null);
progressdialog = new ProgressDialog(context);
if (position % 2 == 1) {
PurchaseOrderRow.setBackgroundColor(Color.WHITE);
} else {
PurchaseOrderRow.setBackgroundColor(Color.LTGRAY);
}
mApp = ((MyAppApplication)context.getApplicationContext());
final Complaint TR = (Complaint)objects.get(position);
final String RequestNO = TR.getTicketNo();
final String reqID = TR.getComplaintID();
Complaint Complaintlist = (Complaint) objects.get(position);
//TextView txt_PONo = (TextView) PurchaseOrderRow.findViewById(R.id.txt_PONo);
//TextView txt_SuppName = (TextView) PurchaseOrderRow.findViewById(R.id.txt_SuppName);
TextView username = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_empname);
username.setText(mApp.getmGlobal_UserName());
TextView userid = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_userid);
userid.setText(mApp.getmGlobal_UserID());
TextView module_name = (TextView)PurchaseOrderRow.findViewById(R.id.txt_module_name);
module_name.setText(Complaintlist.getText2());
TextView Ref_no = (TextView)PurchaseOrderRow.findViewById(R.id.txt_ref_no);
Ref_no.setText(Complaintlist.getTicketNo());
TextView created_by = (TextView)PurchaseOrderRow.findViewById(R.id.txt_created_by_complaint);
created_by.setText(Complaintlist.getCreatedBy());
TextView created_Date= (TextView)PurchaseOrderRow.findViewById(R.id.txt_created_date_complaint);
created_Date.setText(Complaintlist.getCreatedDate());
TextView complaint_sub = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_sub);
complaint_sub.setText(Complaintlist.getComplaintName());
TextView complaint_body = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_body);
complaint_body.setText(Complaintlist.getText1());
final Button chat_support = (Button)PurchaseOrderRow.findViewById(R.id.btn_chat_support);
chat_support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
final Button close_complaint = (Button)PurchaseOrderRow.findViewById(R.id.btn_close);
close_complaint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new set_updateSupport().execute("UpdateValues", "1011",
reqID, "", "", "", "",
"", "","","","");
}
});
return PurchaseOrderRow;
}
class set_updateSupport extends AsyncTask<String, Integer, String> {
private static final String NAME_SPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.1.106:99/Service.asmx";
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressdialog.setMessage("Adding Sales Request");
progressdialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String TempMethod = params[0];
String Flag = params[1];
String value1 = params[2];
String value2 = params[3];
String value3 = params[4];
String value4 = params[5];
String value5 = params[6];
String value6 = params[7];
String value7 = params[8];
String value8 = params[9];
String value9 = params[10];
String value10 = params[11];
try {
SoapObject request = new SoapObject(NAME_SPACE, TempMethod);
request.addProperty("Flag", Flag);
request.addProperty("value1", value1);
request.addProperty("value2", value2);
request.addProperty("value3", value3);
request.addProperty("value4", value4);
request.addProperty("value5", value5);
request.addProperty("value6", value6);
request.addProperty("value7", value7);
request.addProperty("value8", value8);
request.addProperty("value9", value9);
request.addProperty("value10", value10);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE Android_HttpTransport = new HttpTransportSE(URL);
Android_HttpTransport.debug = true;
Android_HttpTransport.call(NAME_SPACE + TempMethod, envelope);
String responseXml = envelope.getResponse().toString();
// String responseXml = Android_HttpTransport.responseDump;
Log.i("RITESH", "responseXml" + responseXml);
return responseXml;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);
progressdialog.dismiss();
if (result == null) {
Toast.makeText(context, "Error while reading data",
Toast.LENGTH_SHORT).show();
} else if (result.equals("0")) {
Toast.makeText(context, "No record for Update",
Toast.LENGTH_SHORT).show();
} else if (result.equals("1")) {
Toast.makeText(context, "Complain/Support Close Successfully", Toast.LENGTH_SHORT)
.show();
notifyDataSetChanged();
} else {
Toast.makeText(context, "Not Updated result " + result,
Toast.LENGTH_SHORT).show();
}
}
}
}
【问题讨论】:
-
你能解释一下你的用例吗
-
我编辑了我的问题,请看
-
调用 notifyDataSetChanged() 时您期望的变化
-
发布您的适配器代码
-
我发布了完整的适配器代码,并且在异步任务中执行结果后,我在异步任务中调用了 notifyOndatasetChanged
标签: android listview refresh baseadapter