TrainActivityjava
package com.example.traindemo; import java.io.IOException; import java.util.List; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import com.example.traindemo.util.Train; import com.example.traindemo.util.TrainLogic; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class TrainActivity extends Activity { List<Train> list; TrainLogic tLogic; ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.train); listView = (ListView) findViewById(R.id.listView1); tLogic = new TrainLogic(); try { list = tLogic.Train(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } // et.setText(list.toString()); listView.setAdapter(new MyAdapter()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent intent = new Intent(TrainActivity.this, DetailActivity.class); intent.putExtra("detail", "車次: " + list.get(arg2).getTrain_no() + "\n車次始發站: " + list.get(arg2).getStart_station_name() + "\n車次終點站: " + list.get(arg2).getEnd_station_name() + "\n從「" + list.get(arg2).getFrom_station_name() + "」站到「" + list.get(arg2).getTo_station_name() + "」站" + "\n出發時間: " + list.get(arg2).getStart_time() + "\n到達時間: " + list.get(arg2).getArrive_time() + "\n車次類型: " + list.get(arg2).getTrain_class_name() + "\n歷時天數: " + list.get(arg2).getDay_difference() + "\n總歷時時間: " + list.get(arg2).getLishi() + "\n高級軟臥: " + list.get(arg2).getGr_num() + "\n特等座: " + list.get(arg2).getTz_num() + "\n商務座: " + list.get(arg2).getSwz_num() + "\n一等座: " + list.get(arg2).getZy_num() + "\n二等座: " + list.get(arg2).getZe_num() + "\n軟臥: " + list.get(arg2).getRw_num() + "\n軟座: " + list.get(arg2).getRz_num() + "\n硬臥: " + list.get(arg2).getYw_num() + "\n硬座: " + list.get(arg2).getYz_num() + "\n無座: " + list.get(arg2).getWz_num() + "\n其餘: " + list.get(arg2).getQt_num()); startActivity(intent); } }); } /** * 自定義適配器 * * @author Administrator * */ class MyAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup arg2) { ViewHolder hold = null; if (convertView == null) { convertView = LayoutInflater.from(TrainActivity.this).inflate( R.layout.demo, null); hold = new ViewHolder(convertView); convertView.setTag(hold); } else { hold = (ViewHolder) convertView.getTag(); } hold.tv.setText(list.get(position).getTrain_no() + " 起點:" + list.get(position).getStart_station_name() + " 終點:" + list.get(position).getEnd_station_name()); return convertView; } } class ViewHolder { TextView tv; public ViewHolder(View v) { tv = (TextView) v.findViewById(R.id.textView_demo); } } }
DetailActivityandroid
package com.example.traindemo; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class DetailActivity extends Activity { TextView tv_detail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); tv_detail = (TextView) findViewById(R.id.detail_textView); String str = getIntent().getStringExtra("detail"); tv_detail.setText(str); } }
detail.xmlapache
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/detail_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
train.xmljson
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#FFFFFF" android:background="#6699FF" android:dividerHeight="2dp"/> </LinearLayout>