使用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> listdata ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);
        listdata = new ArrayList>();
        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 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> getdata() {
// TODO Auto-generated method stub
Map map_1 = new HashMap();
map_1.put("photo", R.drawable.c);
map_1.put("text", "第一张图片");
listdata.add(map_1);
Map map_2 = new HashMap();
map_2.put("photo", R.drawable.fr);
map_2.put("text", "第二张图片");
listdata.add(map_2);
Map map_3 = new HashMap();
map_3.put("photo", R.drawable.minit);
map_3.put("text", "第三张图片");
listdata.add(map_3);
Map map_4 = new HashMap();
map_4.put("photo", R.drawable.net);
map_4.put("text", "第四张图片");
listdata.add(map_4);
Map map_5 = new HashMap();
map_5.put("photo", R.drawable.t);
map_5.put("text", "第五张图片");
listdata.add(map_5);
Map map_6 = new HashMap();
map_6.put("photo", R.drawable.vf);
map_6.put("text", "第六张图片");
listdata.add(map_6);
return listdata;
}

 

 

}

 

主视图界面布局文件:

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.simpadapter2.MainActivity" >
   android:id="@+id/listview"
   android:layout_width="match_parent"
   android:layout_height="match_parent">


 

 

第二个界面,用于显示从主视图界面传递的值:

package com.example.simpadapter2;
使用SimpleAdapter创建ListView,加入了点击事件,并传值_第1张图片使用SimpleAdapter创建ListView,加入了点击事件,并传值_第2张图片

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);
}
}

第二个界面布局文件:

    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" >
   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"/>


你可能感兴趣的:(Android)