2014-12-02 29 views
0
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; 

    List<RowItem> rowItems; 

    @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(); 


     CustomList adapter = new CustomList(this, rowItems); 
     listView1.setAdapter(adapter); 
    } 

    @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); 

        Id = _obj.getString("Id"); 
        Designation = _obj.getString("Designation"); 
        EmployeeName = _obj.getString("EmployeeName"); 
        System.out.println(Id + "" + Designation + "" 
          + EmployeeName); 
       } 

      } 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); 
      rowItems = new ArrayList<RowItem>(); 


     } 
    } 

} 

这是我的代码,我能够杰森在此行中的System.out.println(编号+“” +名称+“” 分析后显示值+员工姓名);后无法打印在Android的ListView的数据JSON解析

,但我无法在列表视图打印数据有未来,而我已创建 数据模型

public class RowItem { 

    public RowItem(String title, String desc) { 

     this.title = title; 
     this.desc = desc; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDesc() { 
     return desc; 
    } 

    public void setDesc(String desc) { 
     this.desc = desc; 
    } 

    private String title; 
    private String desc; 
} 

我已创建是从基地适配器,做工精细延长适配器u能请告诉我如何错误绑定值并在Json解析后在列表视图中显示请建议我即时尝试实现。

+1

它不是json它的xml亲爱的 – 2014-12-02 09:43:50

+0

使用http://developer.and roid.com/training/basics/network-ops/xml.html而不是 – 2014-12-02 09:44:19

+0

我能够获得数据正面临问题绑定在lisview – Departure 2014-12-02 09:44:49

回答

0

将您的值绑定到自定义适配器的getView()方法中。

+0

这也工作但我无法添加Datamodel数据在Preexcute方法 – Departure 2014-12-02 09:47:38

+0

我有问题在列表视图中打印数据 – Departure 2014-12-02 09:51:16

1

使用Custom AdapterJSON数据绑定到你的列表视图象下面这样:

public class ListViewAdapterForLead extends BaseAdapter { 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<SpinnerNavItem> data; 
    TextView txtText; 


    public ListViewAdapterForLead(Context context,ArrayList<SpinnerNavItem> arraylist) { 
     this.context = context; 
     data = arraylist; 

    } 

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

    @Override 
    public Object getItem(int index) { 
     return data.get(index); 
    } 

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

    public View getView(final int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) 
       context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.row_lead, null); 
    } 


    txtText = (TextView) convertView.findViewById(R.id.textLeadMenu); 



    txtText.setText(data.get(position).getTitle()); 
    return convertView; 




    } 



    public View getDropDownView(int position,View convertView,ViewGroup parent){ 

     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.row_lead, null); 
     } 


     txtText = (TextView) convertView.findViewById(R.id.textLeadMenu); 


     txtText.setText(data.get(position).getTitle()); 


    return convertView; 

    } 
} 
+0

我已经完成了@Gulnaz Ghanchi我有doninbackground和onPost在这里执行的问题是我的http://pastie.org/9755703自定义视图 – Departure 2014-12-02 09:58:52

+0

编写你的**'System.out.println(Id +“”+ Designation +“”+ EmployeeName);'**在你的帖子中执行。 – 2014-12-02 10:08:21

0

后台任务被完成后,您应该设置数据。

*首先尝试在onPostExecute()方法中设置列表适配器。
*确保您的模型中的所有细节都被填充。
*在您的Adapter类中使用getView()方法来解析数据。

对于与BaseAdapter ListView的示例代码,请参阅以下链接
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

+0

我有问题绑定数据inRowitem – Departure 2014-12-02 09:54:45

+0

将它添加到AsyncTask类中的全局 ArrayList mArrayList = new ArrayList ();然后尝试在doBackground方法中添加 RowItem mArrayList.add(rItem); – 2014-12-02 10:17:13

0

你忘了一两件事,这就是为什么列表为空

解析数据添加到列表中

//initialize your list here 
rowItems = new List<RowItems>(); 

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

         JSONObject _obj = _jarray.getJSONObject(i); 
          RowItemsr = new RowItems(); 
         Id = _obj.getString("Id"); 
         Designation = _obj.getString("Designation"); 
         EmployeeName = _obj.getString("EmployeeName"); 
         System.out.println(Id + "" + Designation + "" 
           + EmployeeName); 
         // you must create an object and add it to your list 
         r.setTitle(EmployeeName); 
         r.setDesc(Designation); 
         rowItems.add(r); 
        } 

这就是你需要的