1. 程式人生 > >使用SimpleAdapter建立ListView,加入了點選事件,並傳值

使用SimpleAdapter建立ListView,加入了點選事件,並傳值

主檢視介面用於顯示ListView:

package com.example.simpadapter2;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;




public class MainActivity extends Activity {
private ListView listView;
private List<Map<String, Object>> listdata ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);
        listdata = new ArrayList<Map<String,Object>>();
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, getdata(), R.layout.item, new String[]{"photo","text"}, new int[]{R.id.photo,R.id.textview});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Map<String, Object> map = listdata.get(position);
Toast.makeText(MainActivity.this, "第"+(position+1)+"行", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("name", (String)map.get("text"));
startActivity(intent);
}
});
    }
    /**
     * SimpleAdapter的第二個引數getdata();
     * @return
     */
private List<Map<String, Object>> getdata() {
// TODO Auto-generated method stub
Map<String, Object> map_1 = new HashMap<String, Object>();
map_1.put("photo", R.drawable.c);
map_1.put("text", "第一張圖片");
listdata.add(map_1);
Map<String, Object> map_2 = new HashMap<String, Object>();
map_2.put("photo", R.drawable.fr);
map_2.put("text", "第二張圖片");
listdata.add(map_2);
Map<String, Object> map_3 = new HashMap<String, Object>();
map_3.put("photo", R.drawable.minit);
map_3.put("text", "第三張圖片");
listdata.add(map_3);
Map<String, Object> map_4 = new HashMap<String, Object>();
map_4.put("photo", R.drawable.net);
map_4.put("text", "第四張圖片");
listdata.add(map_4);
Map<String, Object> map_5 = new HashMap<String, Object>();
map_5.put("photo", R.drawable.t);
map_5.put("text", "第五張圖片");
listdata.add(map_5);
Map<String, Object> map_6 = new HashMap<String, Object>();
map_6.put("photo", R.drawable.vf);
map_6.put("text", "第六張圖片");
listdata.add(map_6);
return listdata;
}

}

主檢視介面佈局檔案:

<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"
    tools:context="com.example.simpadapter2.MainActivity" >
<ListView 
   android:id="@+id/listview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"></ListView>


</RelativeLayout>

第二個介面,用於顯示從主檢視介面傳遞的值:

package com.example.simpadapter2;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class SecondActivity extends Activity {
private TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView) findViewById(R.id.tv);
String data = getIntent().getStringExtra("name");
textView.setText(data);
}
}

第二個介面佈局檔案:

<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="com.example.simpadapter2.SecondActivity" >
<TextView 
   android:id="@+id/tv"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="demo"
   android:textColor="#000"
   android:textSize="24sp"
   android:gravity="center_horizontal"/>


</RelativeLayout>