2012-12-21 32 views
0

我有一個航班列表,我使用JSON PARSING讀取數據,我做了這個,但我的問題是這個數據顯示,當所有的textview項目被加載,但我想要一個行項目是獲取比第一個項目後加載負荷加載由一個第二個項目一個不直接顯示的所有項當完整的數據下載時,Android listview無法一一加載項目是否完全加載?

我想這個在這裏你可以看到像什麼想和列表視圖
enter image description here

enter image description here

enter image description here

enter image description here

但這不是工作時,此告訴我RESULT直這樣的形象 enter image description here 在這裏,我把我的代碼

HomeActivity.java

public class HomeActivity extends Activity { 
    @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.home); 

      new FlightListBack().execute(""); 
     } 
public class FlightListBack extends AsyncTask<String,ArrayList< HashMap<String,String>> , ArrayList<HashMap<String,String>>> 
{ 

    ArrayList<String> airportList = new ArrayList<String>(); 
    JsonParser jparser= new JsonParser(); 
    List<String> spinnercityList= new ArrayList<String>(); 
    final List<String> spinnerAirportNameList= new ArrayList<String>(); 
    JSONArray sjairport; 



    @Override 
    protected ArrayList<HashMap<String,String>> doInBackground(
      String... params) { 



       ArrayList<HashMap<String, String>> flightList = new ArrayList<HashMap<String, String>>(); 

      String flighturl = "http://api.flightstats.com/flex/flightstatus/rest/v2/json/airport/status/JFK/dep/2012/12/21/5?appId=4dde11f5&appKey=6b80a6ed5d82c8f0b&utc=false&numHours=6"; 
       String jsonstringfunction=jparser.getJSONFromUrl(flighturl); 


        try 
        { 

          JSONObject jobject= new JSONObject(jsonstringfunction); 
          JSONObject appendix = jobject.getJSONObject(TAG_APPENDIX);    Log.e("JOBJECT ","APPENDIX-->"+appendix.length()); 

          // Airlines Array For Getting Airlines Name 
          JSONArray jairlines=appendix.getJSONArray(TAG_AIRLINES);    Log.e("JSONARRAY","AIRLINES"+jairlines.length()); 



          // Airport Array For Getting Airport Name 
          JSONArray jairport=appendix.getJSONArray(TAG_AIRPORT);     Log.e("JSONARRAY","AIRPORT"+jairport.length()); 

          // Flightstatuses Array For Getting INFO 
          JSONArray jflightstatuses=jobject.getJSONArray(TAG_FLIGHTSTATUSES);  Log.e("JSONARRAY","FLIGHTSTATUSES"+jflightstatuses.length()); 

          int i; 
          int j; 
          int k; 
          int m; 
          int n; 


          //****************************** Flightstatuses Array ***************************** 
          for(i=0;i<jflightstatuses.length();i++) 
          { 

           // creating new HashMap 
           HashMap<String, String> map = new HashMap<String, String>(); 


           arrivalairportfscode=jflightstatuses.getJSONObject(i).get(TAG_ARRIVALAIRPORTFSCODE).toString();  // Log.e("ArrivalAirportFsCode","AirportFSCode"+arrivalairportfscode); 
           carrierfscode=jflightstatuses.getJSONObject(i).get(TAG_CARRIERFSCODE).toString(); 
           flightnumber=jflightstatuses.getJSONObject(i).get(TAG_FLIGHTNUMBER).toString(); 


           //****************************** Airport Array ***************************** 
           // Comparing ArrivalAirportFsCode For Finding The City Name 
           for(j=0;j<jairport.length();j++) 
           { 
            // Airport Fs Code from AirportArray 
            airportfs=jairport.getJSONObject(j).get(TAG_AIRPORTFS).toString(); // Log.e("AIRPORT","FS"+airportfs); 

            // Compare ArrivalAirport FsCode and Airport Fs Code For Finding City Name 
            if(arrivalairportfscode.equalsIgnoreCase(airportfs)) 
            { 
             // City Name From Airport Array 
             city=jairport.getJSONObject(j).get(TAG_AIRPORTCITY).toString();  //Log.e("Airport City","City-->"+city); 
             destination="("+arrivalairportfscode+") "+city;  //Log.e("Destination City","city"+destination); 
             flight=carrierfscode+" "+flightnumber; 

            } 
           } // airport for loop complete 

           map.put(TAG_MDESTINATION, destination); 
           map.put(TAG_MFLIGHT,flight); 
           //******************************************************************************* 

           //****************************** Airlines Array ***************************** 

           // Comparing CarriersFsCode To AirlinesArrayfs for Finding The Airlines Names 
           for(k=0;k<jairlines.length();k++) 
           { 

            // Airport Fs Code from AirportArray 
            airlinesfs=jairlines.getJSONObject(k).get(TAG_AIRLINESFS).toString(); //Log.e("AIRLINES","FS"+airlinesfs); 

            // Compare ArrivalAirport FsCode and Airport Fs Code For Finding City Name 
            if(carrierfscode.equalsIgnoreCase(airlinesfs)) 
            { 
             // Airlines Name From Airlines Array 
             airlinesname=jairlines.getJSONObject(k).get(TAG_AIRLINENAME).toString();  //Log.e("AIRLINES NAME","NAME"+airlinesname); 
             Log.e("--"+flight,"--"+airlinesname); 

            } 

           } // airlines for loop complete 
           map.put(TAG_MAIRLINE,airlinesname); 

           JSONObject joperationaltimes=jflightstatuses.getJSONObject(i).getJSONObject(TAG_OPERATIONALTIMES); 
           Log.e("Operational times","-->"+joperationaltimes); 

           // Schedule Time 

           JSONObject jschedulegatedeparture =new JSONObject(); 
           JSONObject jestimateddeparture =new JSONObject(); 

           for(m=0;m<joperationaltimes.length();m++) 
           { 
            if(joperationaltimes.has(TAG_SCHEDULEDGATEDEPARTURE)) 
            { 

              jschedulegatedeparture=joperationaltimes.getJSONObject(TAG_SCHEDULEDGATEDEPARTURE); 
              schedule=jschedulegatedeparture.getString(TAG_SCHEDULEDATELOCAL).toString(); 
              schedule=schedule.substring(11); 

              Log.e("Before convert","Time"+schedule); 

              DateFormat f1 = new SimpleDateFormat("hh:mm:ss"); 
              Date d = f1.parse(schedule); 
              DateFormat f2 = new SimpleDateFormat("h:mm a"); 
              schedule=f2.format(d).toLowerCase(); // "12:18am" 

              Log.e("After convert","Time"+schedule); 
             // jestimateddeparture=joperationaltimes.getJSONObject(TAG_ACTUALGATEDEPARTURE); 
             // actual=jestimateddeparture.getString(TAG_ACTUALDATELOCAL).toString(); 
             // actual=actual.substring(11); 
            } 
            else 
            { 
             schedule="Not Available"; 
            } 

           } 
           map.put(TAG_MSCHEDULE, schedule); 

           //Log.e("ScheduleGatedtime",""+jschedulegatedeparture); 
           //Log.e("Schedule Time","Work"+schedule); 
           //Log.e("ActualGatedtime",""+jestimateddeparture); 
           //Log.e("Actual Time","Work"+actual); 

           //------------------------------------------------------------------------------------ 

           // AIRPORT RESOURCES OBJECT fOR getting departure terminal and departure gate 

           if(jflightstatuses.getJSONObject(i).has(TAG_AIRPORTRESOURCES)) 
           { 

           JSONObject airportresources=jflightstatuses.getJSONObject(i).getJSONObject(TAG_AIRPORTRESOURCES); 
           Log.e("airportresources","--->"+airportresources); 

            if(airportresources.has(TAG_DEPARTURETERMINAL)) 
            { 
             departureterminal=airportresources.get(TAG_DEPARTURETERMINAL).toString(); 
            } 
            else 
            { 
             departureterminal=""; 

            } 

            if(airportresources.has(TAG_DEPARTUREGATE)) 
            { 
             departuregate=airportresources.get(TAG_DEPARTUREGATE).toString(); 

            } 
            else 
            { 
             departuregate=""; 
            } 
            terminal="T-"+departureterminal+" "; 
            gate=departuregate; 
            terminalgate=terminal+gate; 
            Log.e("Terminal Gate","-->"+terminal+gate); 

            map.put(TAG_MTERMINALGATE,terminalgate); 
           } 
           else 
           { 
            terminalgate="Not Available"; 
            map.put(TAG_MTERMINALGATE,terminalgate); 
           } 



            status=jflightstatuses.getJSONObject(i).getString(TAG_STATUS).toString(); 
            if(status.equalsIgnoreCase("S")) 
            { 
             status="Scheduled"; 
            } 
            if(status.equalsIgnoreCase("L")) 
            { 
             status="Landed"; 
            } 
            map.put(TAG_MFSTATUS, status); 



          flightList.add(map); 

          Log.e("FlightList","work"+flightList); 

          } 


        }   
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 

        return flightList; 

      } 


protected void onPostExecute(ArrayList<HashMap<String, String>> flightList) 
{   

}

} 

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter{ 
private LayoutInflater inflater; 

private ArrayList<YourListRowObject> listItems; 

public CustomListAdapter (Context context) { 
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    listItems = new ArrayList<YourListRowObject>(); 
} 

public void addItem(YourListRowObject row) { 
    listItems.add(row); 
    notifyDataSetChanged(); 
} 

public View getView(int position, View convertView, ViewGroup parent){ 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.list, null); 
     holder = new ViewHolder(); 
     holder.textdestination = (TextView)convertView.findViewById(R.id.textdestination); 
     holder.textflight=(TextView)convertView.findViewById(R.id.textflight); 
     holder.textairline=(TextView)convertView.findViewById(R.id.textairline); 
     holder.textschedule=(TextView)convertView.findViewById(R.id.textschedule); 
     holder.texttermgate=(TextView)convertView.findViewById(R.id.texttermgate); 
     holder.textstatus=(TextView)convertView.findViewById(R.id.textstatus); 

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

    holder.textdestination.setText((listItems.get(position)).getMdestination()); 
    holder.textflight.setText((listItems.get(position)).getMflight()); 
    holder.textairline.setText((listItems.get(position)).getMairline()); 
    holder.textschedule.setText((listItems.get(position)).getMschedule()); 
    holder.texttermgate.setText((listItems.get(position)).getMtermgate()); 
    holder.textstatus.setText((listItems.get(position)).getMstatus()); 
    return convertView; 
} 

public int getCount() { 
    return listItems.size(); 
} 

public Object getItem(int position) { 
    return listItems.get(position); 
} 

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

private class ViewHolder{ 
// add here views, which are defined in `list_row Layout` 
    TextView textdestination; 
    TextView textairline; 
    TextView textschedule; 
    TextView texttermgate; 
    TextView textstatus; 
    TextView textflight; 

} 

//contains data, which u get from server in json format. add here your fields 
public class YourListRowObject{ 

public String mdestination; 
public String mflight; 
public String mairline; 
public String mschedule; 
public String mtermgate; 
public String mstatus; 
public String getMdestination() { 
    return mdestination; 
} 
public void setMdestination(String mdestination) { 
    this.mdestination = mdestination; 
} 
public String getMflight() { 
    return mflight; 
} 
public void setMflight(String mflight) { 
    this.mflight = mflight; 
} 
public String getMairline() { 
    return mairline; 
} 
public void setMairline(String mairline) { 
    this.mairline = mairline; 
} 
public String getMschedule() { 
    return mschedule; 
} 
public void setMschedule(String mschedule) { 
    this.mschedule = mschedule; 
} 
public String getMtermgate() { 
    return mtermgate; 
} 
public void setMtermgate(String mtermgate) { 
    this.mtermgate = mtermgate; 
} 
public String getMstatus() { 
    return mstatus; 
} 
public void setMstatus(String mstatus) { 
    this.mstatus = mstatus; 
} 



} 

} 
+0

你知道列表視圖項加載懶適配器的概念?這會幫助你很多。 – itsrajesh4uguys

+0

我將嘗試此鏈接,但仍然是顯示列表視圖後完成數據加載FOT文本列表視圖[http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/] – Mahesh

回答

2
// initialize adapter in `onCreate()` 
    CustomListAdapter adapter = new CustomListAdapter(context); 
    setListAdapter(adapter); 

創建CustomListAdapter

public class CustomListAdapter extends BaseAdapter{ 
private LayoutInflater inflater; 

private ArrayList<YourListRowObject> listItems; 

public CustomListAdapter (Context context) { 
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    listItems = new ArrayList<YourListRowObject>(); 
} 

public void addItem(YourListRowObject row) { 
    listItems.add(row); 
    notifyDataSetChanged(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.list_row, null); 
     holder = new ViewHolder(); 
     holder.text = (TextView)convertView.findViewById(R.id.text); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 

    holder.text.setText((listItems.get(position)).stringValue); 

    return convertView; 
} 

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

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

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

private class ViewHolder{ 
// add here views, which are defined in `list_row Layout` 
    public TextView text; 
} 
} 

// contains data, which u get from server in json format. add here your fields 
public class YourListRowObject{ 

    public String stringValue; 
} 

list_row佈局 - 定義此佈局,其表示ListView

doInBackground()

// get json object from server 
// parse json object into YourListRowObject 

adapter.addItem(yourListRowObject); // add this object to adapter 
+0

你有這個任何鏈接鍵入示例,所以我可以爲此更多地註釋 – Mahesh

+0

嘗試現在編輯的代碼示例 –

+0

chek我的更新代碼是好的或改變的東西 – Mahesh