使用BaseAdapter和AsyncTask来显示ListView

3
我正在从SOAP服务中读取数据,并希望将它们显示在列表中。为此,我创建了一个基本的适配器,并尝试在AsyncTask中使用它。但是这会导致一些错误,我不知道如何解决。根据错误提示,我认为问题出现在AsyncTask的postExecute中,但不知道该如何解决。
以下是一些代码。

该类

public class PatientBasicInfo {
private String FirstName;
private String LastName;
private String Id;
private String Gender;
private String DateOfBirth;

public PatientBasicInfo(String firstname,String lastname,String id){
    this.FirstName=firstname;
    this.LastName=lastname;
    this.Id=id;
}

public String getFirstName(){
    return this.FirstName;
}
public void setFirstName(String firstname){
    this.FirstName=firstname;
}

public String getLastName(){
    return this.LastName;
}
public void setLastName(String lastname){
    this.LastName=lastname;
}
public String getPatientId(){
    return this.Id;
}
public void setPatientId(String Id){
    this.Id=Id;
}
public String getGender(){
    return this.Gender;
}
public void setGender(String gender){
    this.Gender=gender;
}
public String getDateOfBirth(){
    return this.DateOfBirth;
}
public void setDateOfBirth(String dob){
    this.DateOfBirth=dob;
}

@Override
public String toString(){
    return FirstName+" "+LastName;
}

}

The base adapter

public class PatientListBaseAdapter extends BaseAdapter{

private static ArrayList<PatientBasicInfo> patientArrayList;

private LayoutInflater mInflater;
private Context context;

public PatientListBaseAdapter(Context ctx,ArrayList<PatientBasicInfo> result) {
    // TODO Auto-generated constructor stub
    patientArrayList=result;
    mInflater = LayoutInflater.from(context);
    this.context=ctx;
}

public int getCount() {
    // TODO Auto-generated method stub
    return patientArrayList.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return patientArrayList.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    final String patientId=patientArrayList.get(position).getPatientId();
    final String patientFirstName=patientArrayList.get(position).getFirstName();
    final String patientLastName=patientArrayList.get(position).getLastName();
    mInflater = (LayoutInflater)
            context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
           convertView = mInflater.inflate(R.layout.custom_rowview, null);
           holder = new ViewHolder();
           holder.txtFirstName = (TextView) convertView.findViewById(R.id.patientfirstname);
           holder.txtLastName = (TextView) convertView.findViewById(R.id.patientlastname);
           holder.txtID=(TextView)convertView.findViewById(R.id.patientid);

           convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }

    holder.txtFirstName.setText(patientFirstName);
    holder.txtLastName.setText(patientLastName);

    holder.txtID.setText(patientId);
    return convertView;
}
private class ViewHolder {
      TextView txtFirstName;      
      TextView txtID;
      TextView txtLastName; 

     }}

该活动

public class PatientListActivity extends Activity{

private static final String SOAP_ACTION = "";
private static final String NAMESPACE = "";
private static final String METHOD_NAME = "getPatientList";
private static final String URL = "";


TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_listview);
    Intent intent = getIntent();

    String nurseId  = intent.getStringExtra("nurseid");

    new CallgetPatientList().execute(nurseId);
}

class CallgetPatientList extends AsyncTask<String,Void,ArrayList<PatientBasicInfo>>{

    @Override
    protected ArrayList<PatientBasicInfo> doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.d("param", params[0].toString());
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

        Credetials cred=new Credetials("username","password");
        PropertyInfo credPropertyinfo=new PropertyInfo();
        credPropertyinfo.setName("credetials");
        credPropertyinfo.setValue(cred);
        credPropertyinfo.setType(cred.getClass());

        request.addProperty(credPropertyinfo);
        request.addProperty("nurseId", params[0].toString());

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

     try{

            androidHttpTransport.call(SOAP_ACTION, envelope);


            SoapObject response = (SoapObject) envelope.bodyIn;

            ArrayList<PatientBasicInfo> patientList=new ArrayList<PatientBasicInfo>();

            for(int i= 0; i< response.getPropertyCount(); i++){
                SoapObject object = (SoapObject)response.getProperty(i);
                String firstname=object.getProperty("firstName").toString();
                String lastname=object.getProperty("lastName").toString();

                String id=object.getProperty("id").toString();

               PatientBasicInfo aa=new PatientBasicInfo(firstname,lastname,id);                
               patientList.add(aa);
            }

            Log.d("patientCount", Integer.toString(patientList.size()));
            return patientList;


        }catch(Exception e){
            //return e.getMessage();
            return null;
        }

    }
    @Override
    protected void onPostExecute(ArrayList<PatientBasicInfo> result){
        //super.onPostExecute(result);
        Log.d("resultcount", Integer.toString(result.size()));
        final ListView lv = (ListView) findViewById(R.id.ListView01);
        PatientListBaseAdapter adapter=new PatientListBaseAdapter(getApplicationContext(),result);
        lv.setAdapter(adapter);
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_patient_list, menu);
    //return true;


    return true;

}
}

错误

此处输入图片描述


PatientListBaseAdapter 中的第 27 行在哪里? - ρяσѕρєя K
1
不明白你的意思?你是指mInflater = LayoutInflater.from(context)这一行代码吗?这是第27行。 - Reza.Hoque
兄弟...谢谢...错误在第27行。太好了。如果你把它写在答案里,我会标记它的。干杯 - Reza.Hoque
非常欢迎,朋友。如果您认为我的回答对解决问题有所帮助,也可以给我点赞。谢谢。 - ρяσѕρєя K
1个回答

0

目前您正在将NULL上下文传递给LayoutInflater.from,因此请更改PatientListBaseAdapter构造函数代码如下:

public PatientListBaseAdapter(Context ctx,ArrayList<PatientBasicInfo> result) {
    // TODO Auto-generated constructor stub
    patientArrayList=result;
    this.context=ctx;
    mInflater = LayoutInflater.from(this.context);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接