首先贴代码:
1.ListActivity:
public class NewsListActivity extends ListActivity
{
private int power = 0; //用户权限
ListView listView;
ArrayList<HashMap<String, String>> newsList; //新闻列表
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.news_list);
newsList = getNewsList(); //得到新闻列表
//创建一个适配器
SimpleAdapter listAdapter = new SimpleAdapter(this, newsList, R.layout.list, new String[]{"newsName"}, new int[]{R.id.newsName});
setListAdapter(listAdapter);
}
//用户点击List列表时回调此方法
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
System.out.println("选项位置是:" + position);
Toast.makeText(this, "你选择的id是:" + id, Toast.LENGTH_LONG).show();
}
//这是测试数据
private ArrayList<HashMap<String, String>> getNewsList()
{
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
HashMap<String, String> map4 = new HashMap<String, String>();
HashMap<String, String> map5 = new HashMap<String, String>();
HashMap<String, String> map6 = new HashMap<String, String>();
HashMap<String, String> map7 = new HashMap<String, String>();
HashMap<String, String> map8 = new HashMap<String, String>();
HashMap<String, String> map9 = new HashMap<String, String>();
map1.put("newsName", "1解放军要求全军学习***论述摘编"); //设置新闻的名字
map1.put("newsXmlUrl", "191.45.67.44"); //设置新闻的Xml文件的Url地址
map2.put("newsName", "2成都试运行全国首个“井盖办” 3小时内完成井盖替补");
map2.put("newsXmlUrl", "127.0.9.2");
map3.put("newsName", "3天府新区彭山片区铁路专线增至3条");
map3.put("newsXmlUrl", "127.0.0.1");
map4.put("newsName", "4解放军要求全军学习****论述摘编"); //设置新闻的名字
map4.put("newsXmlUrl", "191.45.67.44"); //设置新闻的Xml文件的Url地址
map5.put("newsName", "5成都试运行全国首个“井盖办” 3小时内完成井盖替补");
map5.put("newsXmlUrl", "127.0.9.2");
map6.put("newsName", "6天府新区彭山片区铁路专线增至3条");
map6.put("newsXmlUrl", "127.0.0.1");
map7.put("newsName", "7解放军要求全军学习****论述摘编"); //设置新闻的名字
map7.put("newsXmlUrl", "191.45.67.44"); //设置新闻的Xml文件的Url地址
map8.put("newsName", "8成都试运行全国首个“井盖办” 3小时内完成井盖替补");
map8.put("newsXmlUrl", "127.0.9.2");
map9.put("newsName", "9天府新区彭山片区铁路专线增至3条");
map9.put("newsXmlUrl", "127.0.0.1");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
list.add(map7);
list.add(map8);
list.add(map9);
return list;
}
}
2.ListActivity的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NewsListActivity" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
</RelativeLayout>
3.ListActivity对应的ListView布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dip">
<TextView
android:id="@+id/newsName"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"
android:textIsSelectable="true"
/>
</LinearLayout>
模拟器上运行效果,一切OK啊:
可是在手机上,不行啊。。。。手点酸了,都不行,只能选中里面的文字。。。
求解。。。。。
|