空白列表视图中获得的android

问题描述:

public class Setting extends Activity implements OnClickListener { 

    ListView listView1; 
    ImageView backbutton; 
    String Url = "http://182.71.212.110:8083/api/values/userdetails"; 
    String Id; 
    String Designation; 
    String EmployeeName; 
    JSONArray _jarray; 

    DataModel datamodel = new DataModel(); 
    ArrayList<DataModel> list = new ArrayList<DataModel>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.setting); 
     listView1 = (ListView) findViewById(R.id.listView1); 
     backbutton = (ImageView) findViewById(R.id.backbutton); 
     backbutton.setOnClickListener(this); 

     new GetUserdetail().execute(); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v.getId() == R.id.backbutton) { 
      finish(); 
     } 
    } 

    class GetUserdetail extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      try { 
       String json = HttpHitter.ExecuteData(Url); 
       _jarray = new JSONArray(json); 
       System.out.println("_jarray" + _jarray); 

       for (int i = 0; i <= _jarray.length(); i++) { 
        JSONObject _obj = _jarray.getJSONObject(i); 

        if (Id != null) { 
         datamodel.setId(_obj.getString("Id")); 
        } 
        if (Designation != null) { 
         datamodel.setDesignation(_obj.getString("Designation")); 
        } 
        if (EmployeeName != null) { 
         datamodel.setEmployeeName(_obj 
           .getString("EmployeeName")); 
        } 

        list.add(datamodel); 
       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 

      CustomList adapter = new CustomList(Setting.this, list); 
      listView1.setAdapter(adapter); 

     } 
    } 

} 



public class DataModel implements Parcelable { 
    private String Id = ""; 

    private String Designation = ""; 

    private String EmployeeName = ""; 

    public String getId() { 
     return Id; 
    } 

    public void setId(String id) { 
     this.Id = id; 
    } 

    public String getDesignation() { 
     return Designation; 
    } 

    public void setDesignation(String designation) { 
     this.Designation = designation; 
    } 

    public String getEmployeeName() { 
     return EmployeeName; 
    } 

    public void setEmployeeName(String employeeName) { 
     this.EmployeeName = employeeName; 
    } 

    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public DataModel() { 
     // TODO Auto-generated constructor stub 
    } 

    public DataModel(Parcel in) { 
     Id = in.readString(); 
     EmployeeName = in.readString(); 
     Designation = in.readString(); 

    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     // TODO Auto-generated method stub 
     dest.writeString(Id); 
     dest.writeString(EmployeeName); 
     dest.writeString(Designation); 
    } 

} 


public class CustomList extends BaseAdapter { 
    Context context; 
    private ArrayList<DataModel> arrModel; 

    public CustomList(Context context, ArrayList<DataModel> arrModel) { 
     this.context = context; 
     this.arrModel = arrModel; 
    } 

    /* private view holder class */ 
    private class ViewHolder { 

     TextView txtTitle; 
     TextView txtDesc; 

     ImageView locationimage; 
     ImageView roleimageview; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     LayoutInflater mInflater = (LayoutInflater) context 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.settingrowitem, null); 
      holder = new ViewHolder(); 
      holder.txtDesc = (TextView) convertView 
        .findViewById(R.id.rowcontact_txtName); 
      holder.txtTitle = (TextView) convertView 
        .findViewById(R.id.rowcontact_txtrole); 

      holder.locationimage = (ImageView) convertView 
        .findViewById(R.id.imageView1); 
      holder.roleimageview = (ImageView) convertView 
        .findViewById(R.id.imageView2); 

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

     holder.txtDesc.setText(arrModel.get(position).getEmployeeName()); 
     holder.txtTitle.setText(arrModel.get(position).getDesignation()); 

     if (position % 2 == 0) { 
      convertView.setBackgroundColor(Color.parseColor("#ffffff")); 
     } 

     else { 
      convertView.setBackgroundColor(Color.parseColor("#f5f6f1")); 
     } 

     return convertView; 
    } 

    @Override 
    public int getCount() { 
     return arrModel.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return arrModel.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

} 

有三个班,我有使用到从Web服务解析数据,并试图在列表视图。我是能够从服务器insetting.javaclass做JSON解析和获取数据打印:空白列表视图中获得的android

list.add(datamodel); 

和列表onpostexcute方法结合本:

CustomList adapter = new CustomList(Setting.this, list); 
      listView1.setAdapter(adapter); 

使用此我试图打印列表视图项数据,但我在每个项目获得的空白值,而数JSON是49的我得到那49但getDesignation和getEmploye我得到空白值请检查并告诉我我在哪里做错误我已经尝试过很多无法获得打印该值请检查建议我

你在哪里inistailizind Id,指定和EmployeeName,它将本身为空。所以doInBackground不会添加任何数据模型。删除检查

class GetUserdetail extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     try { 
      String json = HttpHitter.ExecuteData(Url); 
      _jarray = new JSONArray(json); 
      System.out.println("_jarray" + _jarray); 

      for (int i = 0; i <= _jarray.length(); i++) { 
       DataModel datamodel = new DataModel(); 
       JSONObject _obj = _jarray.getJSONObject(i); 
       datamodel.setId(_obj.getString("Id")); 
       datamodel.setDesignation(_obj.getString("Designation")); 
       datamodel.setEmployeeName(_obj.getString("EmployeeName")); 
       list.add(datamodel); 
      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 

     CustomList adapter = new CustomList(Setting.this, list); 
     listView1.setAdapter(adapter); 

    } 
} 

初始化for循环内的模型和尝试一次。

+0

仍然是空白值 – Edge 2014-12-02 11:26:09

+0

我不是为什么这个问题来了 – Edge 2014-12-02 11:27:51

+0

http://pastie.org/9755844看到这个我有使用此代码 – Edge 2014-12-02 11:30:24

为了确定您的列表项发生改变,你应该叫 adapter.notifyDataSetChanged(); 设置适配器列表视图之前调用它。