/*******************************************************************************
* 本例使用ListView和自定义的行部局实现了一个最基本的表格部局
* 和一个可变长度的表格部局
* 并实现的简单的行组件的单击事件
*
* 作者:http://svn1.bcoder.com
*
*******************************************************************************/
package com.bcoder.listviewtable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class ListViewTableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_table);
ArrayList<HashMap<String, String>> dItems = new ArrayList<HashMap<String, String>>();
HashMap<String, String> mItem1 = new HashMap<String, String>();
mItem1.put("Name", "张三");
mItem1.put("URL", "http://svn1.bcoder.com");
dItems.add(mItem1);
HashMap<String, String> mItem2 = new HashMap<String, String>();
mItem2.put("Name", "李四");
mItem2.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem2);
HashMap<String, String> mItem3 = new HashMap<String, String>();
mItem3.put("Name", "李四");
mItem3.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem3);
HashMap<String, String> mItem4 = new HashMap<String, String>();
mItem4.put("Name", "李四");
mItem4.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem4);
HashMap<String, String> mItem5 = new HashMap<String, String>();
mItem5.put("Name", "李四");
mItem5.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem5);
HashMap<String, String> mItem6 = new HashMap<String, String>();
mItem6.put("Name", "李四");
mItem6.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem6);
HashMap<String, String> mItem7 = new HashMap<String, String>();
mItem7.put("Name", "李四");
mItem7.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem7);
HashMap<String, String> mItem8 = new HashMap<String, String>();
mItem8.put("Name", "李四");
mItem8.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem8);
HashMap<String, String> mItem9 = new HashMap<String, String>();
mItem9.put("Name", "李四");
mItem9.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem9);
HashMap<String, String> mItem10 = new HashMap<String, String>();
mItem10.put("Name", "李四");
mItem10.put("URL", "http://svn1.bcoder.com/?page_id=111");
dItems.add(mItem10);
//下面两行用于切换使用listview_row_basic或者listview_row的行部局
MyAdapter adapter = new MyAdapter(this, dItems, R.layout.listview_row,
//MyAdapter adapter = new MyAdapter(this, dItems, R.layout.listview_row_basic,
new String[] {"Name", "URL"},
new int[] {R.id.textView1, R.id.textView2});
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_list_view_table, menu);
return true;
}
public class MyAdapter extends SimpleAdapter{
public MyAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View curRowView = super.getView(position, convertView, parent);
final TextView tv = (TextView)curRowView.findViewById(R.id.textView2);
//用于设置当字符长度超出TextView的宽度时,在末尾显示为"..."
//此属于也可以在activity设计界面中设置,还可以为其他值,如:START, MIDDLE 或者 MARQUEE
tv.setEllipsize(TruncateAt.END);
//设置此属于上面的属性才会生效
tv.setSingleLine(true);
//设置最大行数为1,则最多只显示一行,此属性可以在activity设计界面中设置
tv.setMaxLines(1);
OnClickListener onUrlTxtClick = new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse(tv.getText().toString());
Intent logintent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(logintent);
}
};
//实现TextView的点击效果
tv.setOnClickListener(onUrlTxtClick);
return curRowView;
}
}
}