2016-07-21 8 views
0

Die JSON-Daten erhalten, und ich wollte es analysieren und in listview.I Adapter-Methode verwendet haben. Hier ist mein Code:Drucken von Listenansicht für JSON-Daten in Android-App

DisplayListView Klasse:

public class DisplayListView extends AppCompatActivity { 
     String json_string; 
     JSONObject jsonObject =null; 
     JSONArray jsonArray=null; 
     dataAdapter dataAdapter; 
     ListView listView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.displaylistview_layout); 
      listView=(ListView) findViewById(R.id.listview); 
      listView.setAdapter(dataAdapter); 
      dataAdapter=new dataAdapter(this,R.layout.row_layout); 
      json_string=getIntent().getExtras().getString("json_data"); 
      Log.w("var32", json_string); 
      try { 
       jsonArray = new JSONArray(json_string); 
       jsonObject = new JSONObject(); 
       jsonObject.put("arrayName",jsonArray); 
       jsonArray=jsonObject.getJSONArray("arrayName"); 
       int count=0; 
       String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO; 


while (count<jsonObject.length()){ 
      JSONObject JO=jsonArray.getJSONObject(count); 
      LabLocation=JO.getString("LabLocation"); 
      RackLocation=JO.getString("RackLocation"); 
      ShelfLocation=JO.getString("ShelfLocation"); 
      fourBid=JO.getString("fourBid"); 
      Cluster=JO.getString("Cluster"); 
      fourBookingName=JO.getString("fourBookingName"); 
      SoftwareVersion=JO.getString("SoftwareVersion"); 
      HardwareType=JO.getString("HardwareType"); 
      AssetNo=JO.getString("AssetNo"); 
      SerialNO=JO.getString("SerialNO"); 
      data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO); 
      Log.w("var34", "Arguments Verified"); 
      dataAdapter.add(data); 
      count++; 
     } 
     listView.setAdapter(dataAdapter); 
     dataAdapter.notifyDataSetChanged(); 

    }catch (JSONException e){ 
       Log.w("var33", "error JSON exception"); 
       e.printStackTrace(); 
      } 
     } 
    } 

Adapter Klasse:

public class dataAdapter extends ArrayAdapter { 
    List list=new ArrayList(); 
    public dataAdapter(Context context, int resource) { 
     super(context, resource); 
    } 

    public void add(data object) { 
     super.add(object); 
     list.add(object); 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row; 
     row=convertView; 
     dataHolder dataHolder; 
     if (row==null){ 
      LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row=layoutInflater.inflate(R.layout.row_layout,parent,false); 
      dataHolder = new dataHolder(); 
      dataHolder.tx_LabLocation=(TextView) row.findViewById(R.id.tx_LabLocation); 
      dataHolder.tx_RackLocation=(TextView) row.findViewById(R.id.tx_RackLocation); 
      dataHolder.tx_ShelfLocation=(TextView) row.findViewById(R.id.tx_ShelfLocation); 
      dataHolder.tx_fourBid=(TextView) row.findViewById(R.id.tx_fourBid); 
      dataHolder.tx_Cluster=(TextView) row.findViewById(R.id.tx_Cluster); 
      dataHolder.tx_fourBookingName=(TextView) row.findViewById(R.id.tx_fourBookingName); 
      dataHolder.tx_SoftwareVersion=(TextView) row.findViewById(R.id.tx_SoftwareVersion); 
      dataHolder.tx_HardwareType=(TextView) row.findViewById(R.id.tx_HardwareType); 
      dataHolder.tx_AssetNo=(TextView) row.findViewById(R.id.tx_AssetNo); 
      dataHolder.tx_SerialNO=(TextView) row.findViewById(R.id.tx_SerialNO); 
      row.setTag(dataHolder); 
     } else { 
      dataHolder=(dataHolder) row.getTag(); 
     } 
     data data = (data) this.getItem(position); 
     dataHolder.tx_LabLocation.setText(data.getLabLocation()); 
     dataHolder.tx_RackLocation.setText(data.getRackLocation()); 
     dataHolder.tx_ShelfLocation.setText(data.getShelfLocation()); 
     dataHolder.tx_fourBid.setText(data.getFourBid()); 
     dataHolder.tx_Cluster.setText(data.getCluster()); 
     dataHolder.tx_fourBookingName.setText(data.getFourBookingName()); 
     dataHolder.tx_SoftwareVersion.setText(data.getSoftwareVersion()); 
     dataHolder.tx_HardwareType.setText(data.getHardwareType()); 
     dataHolder.tx_AssetNo.setText(data.getAssetNo()); 
     dataHolder.tx_SerialNO.setText(data.getSerialNO()); 
     return row; 
    } 

    static class dataHolder { 
     TextView tx_LabLocation,tx_RackLocation,tx_ShelfLocation,tx_fourBid,tx_Cluster,tx_fourBookingName,tx_SoftwareVersion,tx_HardwareType,tx_AssetNo,tx_SerialNO; 
    } 
} 

data.java:

public class data { 

    private String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO; 

    public data (String LabLocation,String RackLocation,String ShelfLocation,String fourBid,String Cluster,String fourBookingName,String SoftwareVersion,String HardwareType,String AssetNo,String SerialNO) { 
     this.setLabLocation(LabLocation); 
     this.setRackLocation(RackLocation); 
     this.setShelfLocation(ShelfLocation); 
     this.setFourBid(fourBid); 
     this.setCluster(Cluster); 
     this.setFourBookingName(fourBookingName); 
     this.setSoftwareVersion(SoftwareVersion); 
     this.setHardwareType(HardwareType); 
     this.setAssetNo(AssetNo); 
     this.setSerialNO(SerialNO); 

    } 

    public String getLabLocation() { 
     System.out.println("LabLocation" +LabLocation); 
     return LabLocation; 
    } 

    public void setLabLocation(String labLocation) { 
     LabLocation = labLocation; 
    } 

    public String getRackLocation() { 
     return RackLocation; 
    } 

    public void setRackLocation(String rackLocation) { 
     RackLocation = rackLocation; 
    } 

    public String getShelfLocation() { 
     return ShelfLocation; 
    } 

    public void setShelfLocation(String shelfLocation) { 
     ShelfLocation = shelfLocation; 
    } 

    public String getFourBid() { 
     return fourBid; 
    } 

    public void setFourBid(String fourBid) { 
     this.fourBid = fourBid; 
    } 

    public String getCluster() { 
     return Cluster; 
    } 

    public void setCluster(String cluster) { 
     Cluster = cluster; 
    } 

    public String getFourBookingName() { 
     return fourBookingName; 
    } 

    public void setFourBookingName(String fourBookingName) { 
     this.fourBookingName = fourBookingName; 
    } 

    public String getSoftwareVersion() { 
     return SoftwareVersion; 
    } 

    public void setSoftwareVersion(String softwareVersion) { 
     SoftwareVersion = softwareVersion; 
    } 

    public String getHardwareType() { 
     return HardwareType; 
    } 

    public void setHardwareType(String hardwareType) { 
     HardwareType = hardwareType; 
    } 

    public String getAssetNo() { 
     return AssetNo; 
    } 

    public void setAssetNo(String assetNo) { 
     AssetNo = assetNo; 
    } 

    public String getSerialNO() { 
     return SerialNO; 
    } 

    public void setSerialNO(String serialNO) { 
     SerialNO = serialNO; 
    } 
} 

Es gibt keine Fehler ausgelöst nach dem Parsen. Es öffnet nur die neue Seite leer. Danke.

row_layout XML-Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="75dp"> 

    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_LabLocation" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_RackLocation" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_ShelfLocation" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_fourBid" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_Cluster" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_fourBookingName" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_SoftwareVersion" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_HardwareType" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_AssetNo" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 
    <TextView 
     android:layout_width="120dp" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_SerialNO" 
     android:layout_alignParentLeft="true" 
     android:text="Albyn" 
     android:gravity="center" 
     android:textAppearance="?android:textAppearanceLarge" 
     /> 



</RelativeLayout> 

displaylistview_layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.albbaby.nokialabs.DisplayListView"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/listview" 
    /> 

</RelativeLayout> 
+0

Adapter auf Listenansicht einstellen, nachdem JSON-Antwort erhalten wurde. – Bee

+0

können Sie DataAdapter.notifyDataSetChanged() nach dem Auffüllen der Liste aufrufen? –

+0

Sie setzen den Adapter, der null ist und bindet an die Listview. Sie müssen den Adapter nach dem Parsen setzen –

Antwort

0

ich die JSON-Daten in Textform angezeigt haben gerade von ihnen zurück. return; Dies wird mein Hauptzweck der Anzeige der Informationen in der Android App.

json_string=responseOutput.toString(); 
JSONArray jsonArray = new JSONArray(json_string); 
       int count =0; 
       count= jsonArray.length(); 
       JSONObject jobj = jsonArray.getJSONObject(0); 
       String LabLocation = jobj.getString("LabLocation"); 
       String RackLocation = jobj.getString("RackLocation"); 
       String ShelfLocation = jobj.getString("ShelfLocation"); 
       String fourBid = jobj.getString("fourBid"); 
       String Cluster = jobj.getString("Cluster"); 
       String fourBookingName = jobj.getString("fourBookingName"); 
       String SoftwareVersion = jobj.getString("SoftwareVersion"); 
       String HardwareType = jobj.getString("HardwareType"); 
       String AssetNo = jobj.getString("AssetNo"); 
       String SerialNO = jobj.getString("SerialNO"); 
       String Location = jobj.getString("Location"); 

       return " "+"LabLocation" + ":" +" " +LabLocation + "\n" + " "+"RackLocation" + ":" +" " +RackLocation 
         + "\n" + " "+"ShelfLocation" + ":" +" " +ShelfLocation + "\n" + " "+"4BookingID" + ":" +" " +fourBid 
         + "\n" + " "+"Cluster" + ":" +" " +Cluster + "\n" + " "+"4BookingName" + ":" +" " +fourBookingName 
         + "\n" + " "+"SoftwareVersion" + ":" +" " +SoftwareVersion + "\n" + " "+"HardwareType" + ":" +" " +HardwareType 
         + "\n" + " "+"AssetNo" + ":" +" " +AssetNo + "\n" + " "+"SerialNO" + ":" +" " +SerialNO + "\n" + " "+"Location" + ":" +" " +Location; 
      } 
0

Schreib diese Linie listView.setAdapter (dataadapter); nach der while-Schleife

Sie setzen den Adapter, bevor Sie Daten in dataAdapter hinzufügen.

Es sollte getan werden, nachdem die Daten hinzugefügt wird

0
public class DisplayListView extends AppCompatActivity { 
String json_string; 
JSONObject jsonObject =null; 
JSONArray jsonArray=null; 
dataAdapter dataAdapter; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.displaylistview_layout); 
    listView=(ListView) findViewById(R.id.listview); 
    //listView.setAdapter(dataAdapter); // set this line after your parsing is done 
    dataAdapter=new dataAdapter(this,R.layout.row_layout); 
    listView.setAdapter(dataAdapter); 
    json_string=getIntent().getExtras().getString("json_data"); 
    Log.w("var32", json_string); 
    try { 
     jsonArray = new JSONArray(json_string); 
     jsonObject = new JSONObject(); 
     jsonObject.put("arrayName",jsonArray); 
     jsonArray=jsonObject.getJSONArray("arrayName"); 
     int count=0; 
     String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO; 
     while (count<jsonObject.length()){ 
      JSONObject JO=jsonArray.getJSONObject(count); 
      LabLocation=JO.getString("LabLocation"); 
      RackLocation=JO.getString("RackLocation"); 
      ShelfLocation=JO.getString("ShelfLocation"); 
      fourBid=JO.getString("fourBid"); 
      Cluster=JO.getString("Cluster"); 
      fourBookingName=JO.getString("fourBookingName"); 
      SoftwareVersion=JO.getString("SoftwareVersion"); 
      HardwareType=JO.getString("HardwareType"); 
      AssetNo=JO.getString("AssetNo"); 
      SerialNO=JO.getString("SerialNO"); 
      Log.w("var34", "Arguments Passed"); 
      data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO); 
      dataAdapter.add(data); 
      count++; 
     } 

    listView.notifyDataSetChanged(); // or notify it if you have previously added it 

    }catch (JSONException e){ 
     Log.w("var33", "error JSON exception"); 
     e.printStackTrace(); 
    } 
} 

}

+0

habe ich per Vorschlag hinzugefügt. Die Ausgabe wird übereinander angezeigt. Ich habe den XML-Code hinzugefügt – Alby