我有一个航班列表,我使用JSON PARSING获取数据,我完成了这个,但我的问题是这个数据显示所有textview项目加载但我想要一个行项目获取比首先加载item load是逐个加载第二个项目而不是直接显示所有项目
我想在这里你可以看到图片想要用listview做什么
但这不是工作,这显示我直接想象这个图像 在这里我把我的代码放在
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 :(得分: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