`

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 构造数据显示
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics