list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/img" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="5px" />
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/title" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#FFFFFFFF"
android:textSize="22px" />
<TextView android:id="@+id/info" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#FFFFFFFF"
android:textSize="13px" />
</LinearLayout>
<Button android:id="@+id/view_btn" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="123"
android:layout_gravity="bottom|right" />
</LinearLayout>
Activity
package net.lekusoft.android.wanzhuanyayun;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.lekusoft.android.wanzhuanyayun.commons.NotesDbAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class StadiumActivity extends Activity {
private int images[] = { R.drawable.test_stadium_1,
R.drawable.test_stadium_2, R.drawable.test_stadium_3 };
private NotesDbAdapter mDbHelper;
private final String TABLE_NAME = "stadium";
private final String[] col = { "stid", "name", "detail", "events",
"address", "route", "image" };
private List<Map<String, Object>> mData;
private ListView listView;
private LinearLayout layout ;
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
this.setContentView(R.layout.stadium);
mDbHelper=new NotesDbAdapter(this);
mDbHelper.open();
// MyThread mt = new MyThread();
// mt.start();
Cursor cur = mDbHelper.get(TABLE_NAME, col);
mData = getData(cur);
MyAdapter adapter = new MyAdapter(this);
listView = new ListView(this);
listView.setCacheColorHint(0);
listView.setAdapter(adapter);
// setListAdapter(adapter);
layout = (LinearLayout)this.findViewById(R.id.stadium);
layout.addView(listView);
}
class MyThread extends Thread {
public void run() {
redDate();
}
}
public int redDate() {
int flag = 0;
try {
String[][] data;
AssetManager am = this.getAssets();
InputStream is = am.open("stadium_data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(new String(str.getBytes(), "UTF-8") + "/n");
}
String stadiums[] = (sb.toString()).split("#");
data = new String[stadiums.length][];
for (int i = 0; i < stadiums.length; i++) {
data[i] = stadiums[i].split("&");
data[i][0] = data[i][0].replace("/n", "");
data[i][0] = data[i][0].replace("/r", "");
mDbHelper.create_4(data[i][0], data[i][1], data[i][2],
data[i][3], data[i][4], i);
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/***********************列表布局***************************/
private List<Map<String, Object>> getData(Cursor cur) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map ;
while(cur.moveToNext()){
map = new HashMap<String, Object>();
map.put("title", cur.getString(1));
map.put("info", subString(cur.getString(2) , 30));
map.put("img", images[cur.getInt(6)]);
list.add(map);
}
return list;
}
// ListView 中某项被选中后的逻辑
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.v("MyListView4-click", (String) mData.get(position).get("title"));
}
public final class ViewHolder {
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list, null);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.info = (TextView) convertView.findViewById(R.id.info);
// holder.viewBtn = (Button) convertView
// .findViewById(R.id.view_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.img.setBackgroundResource((Integer) mData.get(position).get(
"img"));
holder.title.setText((String) mData.get(position).get("title"));
holder.info.setText((String) mData.get(position).get("info"));
// holder.viewBtn.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// showInfo();
// }
// });
return convertView;
}
}
public String subString(String str , int to){
String str2 = "";
try{
if(str.length() > to){
str2 = str.substring(0, to) + "...";
}
}catch(Exception e){
e.printStackTrace();
}
return str2;
}
}