Android使用ListView构造复杂页面,并响应点击事件,通过intent跳转代码
基础知识及注意事项:
1、在需要使用Intent跳转到其他layout的时候,需要在AndroidManifest.xml文件中增加新的layout.
比如本文需要
<activity android:name="SongActivity"></activity>
2、需要附加创建一个xml文件,作为listview的子项。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ItemImage"> </ImageView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/ItemTitle" android:layout_toRightOf="@+id/ItemImage" android:textSize="30dip" ></TextView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_toRightOf="@+id/ItemImage" android:layout_below="@+id/ItemTitle" android:id="@+id/ItemText" ></TextView> </RelativeLayout>
package hzy.webkit; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class WebkitActivity extends Activity implements android.widget.AdapterView.OnItemClickListener { ArrayList<HashMap<String, Object>> mylist = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mylist = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<15;i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("icon", R.drawable.wlh); map.put("ItemTitle", "王力宏"); map.put("ItemText", "春天的故事"); mylist.add(map); } ListView lv = (ListView)this.findViewById(R.id.listView1); lv.setAdapter(initAdapter()); lv.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { Intent intent = new Intent(getApplicationContext(), SongActivity.class); WebkitActivity.this.startActivity(intent); } public SimpleAdapter initAdapter(){ SimpleAdapter mSchedule = new SimpleAdapter(this,mylist,//数据来源 R.layout.single, new String[] {"icon","ItemTitle", "ItemText"}, new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}); return mSchedule; } } //Method 1 //初始界面 //获取web json或者 xml数据 //代码生成html //webview加载该html数据显示 //Method 2 //初始界面 //获取web json或者xml数据 //原生listview 构造数据显示
相关推荐
chenjinlong 2020-02-19
83580494 2013-07-19
langjiao 2013-07-16
kiduo0 2013-07-10
gongzhiyao0 2010-11-15
bigdatazx 2010-11-05
Urchindong 2011-08-01
peixiaopao 2011-08-21
MeOrdinary 2014-05-13
magic00 2019-10-21
guizhongyun 2011-09-27
csuhanshuai 2015-03-30
Sunanang 2015-03-30
snailbing 2015-04-23
huohu00 2015-04-22
toperfect 2015-07-01
nickey 2012-01-29
Rgenxiao 2012-01-26
满城风絮 2011-12-03