2014-12-02 55 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); 
        } 

這就是你需要的