标签:相同 .class 数据源 2.0 界面 new putextra 代码简洁 ack
package com.oldtogether.adapterdemo2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity4 extends ActionBarActivity implements OnItemClickListener, OnItemLongClickListener {
// 创建数组,采用for循环进行遍历
private int[] imageIds = new int[] { R.drawable.daxiang, R.drawable.maozi, R.drawable.nangua, R.drawable.nanguobq,
R.drawable.tiaopi, R.drawable.xiaolian, R.drawable.xin, R.drawable.weixin, R.drawable.hongx,
R.drawable.xm };
private String[] titles = new String[] { "卖萌大象", "圣诞帽子", "愤怒南瓜", "难过表情", "调皮表情", "我倩的笑脸表情", "oldtogether爱心", "微信",
"粉色少女心", "一代撸" };
private String[] versions = new String[] { "版本:1.10", "版本:2.10", "版本:1.11", "版本:2.12", "版本:3.10", "版本:2.20",
"版本:5.20", "版本:1.23", "版本:2.21", "版本:2.40", };
private String[] sizes = new String[] { "大小:32.01M", "大小:32.02M", "大小:23.01M", "大小:11.01M", "大小:41.01M",
"大小:11.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// 1、过的ListView对象
ListView lv = (ListView) findViewById(R.id.lv_play);
// 2、数据源
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < titles.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", imageIds[i]);
map.put("title", titles[i]);
map.put("version", versions[i]);
map.put("size", sizes[i]);
list.add(map);
}
// 3、设置适配器
MyAdapter3 adapter3 = new MyAdapter3(this);
adapter3.setList(list);// 传入数据
// 4、关联适配器
lv.setAdapter(adapter3);
// 5、为ListView设置事件监听器(一下两个常用事件),让主MainActivity2实现接口,并重写方法
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* 创建了一个意图对象,调用setClass方法表明“在那个Activity中调用那个Activity”次意图*/
Intent intent = new Intent();
intent.setClass(getApplicationContext(), DetailActivity.class);
//获得点击行的数据
HashMap <String,Object> itemMap= (HashMap<String,Object>) parent.getItemAtPosition(position);
intent.putExtra("index", ""+position);//获得编号
intent.putExtra("title", ""+itemMap.get("title"));//获得标题
startActivity(intent);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("消息提示");
builder.setMessage("这是一个简单的标题");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点击对话框中的确定按钮会出现一个Toast提示框
Toast.makeText(MainActivity4.this, "点击了确定", Toast.LENGTH_LONG).show();
}
});
builder.create().show();
return true;
/*
* 返回值代表了事件的消化 1、true:表示消化掉事件,事件不会继续传递 2、false:表示不消化事件,事件继续传递
* 结论:如果希望长按事件后,不在出现点击事件,则应将返回值设置为true
*/
}
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("消息提示")
.setMessage("这是一个简单的标题")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity4.this, "点击了确定", Toast.LENGTH_LONG).show();
}
})
.show();
return true;
}
package com.oldtogether.adapterdemo2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity4 extends ActionBarActivity implements OnItemClickListener, OnItemLongClickListener {
// 创建数组,采用for循环进行遍历
private int[] imageIds = new int[] { R.drawable.daxiang, R.drawable.maozi, R.drawable.nangua, R.drawable.nanguobq,
R.drawable.tiaopi, R.drawable.xiaolian, R.drawable.xin, R.drawable.weixin, R.drawable.hongx,
R.drawable.xm };
private String[] titles = new String[] { "卖萌大象", "圣诞帽子", "愤怒南瓜", "难过表情", "调皮表情", "我倩的笑脸表情", "oldtogether爱心", "微信",
"粉色少女心", "一代撸" };
private String[] versions = new String[] { "版本:1.10", "版本:2.10", "版本:1.11", "版本:2.12", "版本:3.10", "版本:2.20",
"版本:5.20", "版本:1.23", "版本:2.21", "版本:2.40", };
private String[] sizes = new String[] { "大小:32.01M", "大小:32.02M", "大小:23.01M", "大小:11.01M", "大小:41.01M",
"大小:11.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// 1、过的ListView对象
ListView lv = (ListView) findViewById(R.id.lv_play);
// 2、数据源
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < titles.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", imageIds[i]);
map.put("title", titles[i]);
map.put("version", versions[i]);
map.put("size", sizes[i]);
list.add(map);
}
// 3、设置适配器
MyAdapter3 adapter3 = new MyAdapter3(this);
adapter3.setList(list);// 传入数据
// 4、关联适配器
lv.setAdapter(adapter3);
// 5、为ListView设置事件监听器(一下两个常用事件),让主MainActivity2实现接口,并重写方法
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* 创建了一个意图对象,调用setClass方法表明“在那个Activity中调用那个Activity”次意图*/
Intent intent = new Intent();
intent.setClass(getApplicationContext(), DetailActivity.class);
//获得点击行的数据
HashMap <String,Object> itemMap= (HashMap<String,Object>) parent.getItemAtPosition(position);
intent.putExtra("index", ""+position);//获得编号
intent.putExtra("title", ""+itemMap.get("title"));//获得标题
startActivity(intent);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//长按之确定对话框
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("你确定要退出吗?")
.setNegativeButton("取消", null)//null,说明不进行自定义的任何操作,只是单纯的关闭当前对话框,又停在原界面
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();//关闭当前Activity
}
})
.show();
return true;
}
}
package com.oldtogether.adapterdemo2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity4 extends ActionBarActivity implements OnItemClickListener, OnItemLongClickListener {
// 创建数组,采用for循环进行遍历
private int[] imageIds = new int[] { R.drawable.daxiang, R.drawable.maozi, R.drawable.nangua, R.drawable.nanguobq,
R.drawable.tiaopi, R.drawable.xiaolian, R.drawable.xin, R.drawable.weixin, R.drawable.hongx,
R.drawable.xm };
private String[] titles = new String[] { "卖萌大象", "圣诞帽子", "愤怒南瓜", "难过表情", "调皮表情", "我倩的笑脸表情", "oldtogether爱心", "微信",
"粉色少女心", "一代撸" };
private String[] versions = new String[] { "版本:1.10", "版本:2.10", "版本:1.11", "版本:2.12", "版本:3.10", "版本:2.20",
"版本:5.20", "版本:1.23", "版本:2.21", "版本:2.40", };
private String[] sizes = new String[] { "大小:32.01M", "大小:32.02M", "大小:23.01M", "大小:11.01M", "大小:41.01M",
"大小:11.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", "大小:33.01M", };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// 1、过的ListView对象
ListView lv = (ListView) findViewById(R.id.lv_play);
// 2、数据源
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < titles.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("logo", imageIds[i]);
map.put("title", titles[i]);
map.put("version", versions[i]);
map.put("size", sizes[i]);
list.add(map);
}
// 3、设置适配器
MyAdapter3 adapter3 = new MyAdapter3(this);
adapter3.setList(list);// 传入数据
// 4、关联适配器
lv.setAdapter(adapter3);
// 5、为ListView设置事件监听器(一下两个常用事件),让主MainActivity2实现接口,并重写方法
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* 创建了一个意图对象,调用setClass方法表明“在那个Activity中调用那个Activity”次意图*/
Intent intent = new Intent();
intent.setClass(getApplicationContext(), DetailActivity.class);
//获得点击行的数据
HashMap <String,Object> itemMap= (HashMap<String,Object>) parent.getItemAtPosition(position);
intent.putExtra("index", ""+position);//获得编号
intent.putExtra("title", ""+itemMap.get("title"));//获得标题
startActivity(intent);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//列表对话框
final String [] sites={"打印店","男生公寓","女生公寓","清真食堂","大众食堂","图书馆","洗浴中心"};//设置为final(常量)的原因是可以穿透作用域
new AlertDialog.Builder(this)
.setTitle("兰州城市学院培黎校区")
.setItems(sites, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//参数 which代表了消息对话框中的第几项
Toast.makeText(MainActivity4.this, "您选择了"+sites[which], Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭的对话框的第二种方式,重写一个没有任何逻辑的空方法
}
})
.show();
return true;
}
}
//在主类中的全局变量1、暂存用户所选的评分级别;(另外如果将此变量初始值设为0,直接点击确定会出现1星评分,有bug)2、方便穿透作用域;
int selectedId = -1;
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//长按之单选对话框
final String [] stars={"1星","2星","2星","3星","4星","5星"};//设置为final(常量)的原因是可以穿透作用域
new AlertDialog.Builder(this)
.setTitle("大胸弟,来个5星的评分吧!")
.setSingleChoiceItems(stars, -1, new DialogInterface.OnClickListener() {
/*
* 第一个参数:所要显示的资源数组;
* 第二个参数:在单选对话框列表中默认情况先所选择的行标(这里设置为-1的原因是数组默认情况先下标从0开始,如果将此参设置为0,跳出单选对话框时默认情况下会为1星评分,有bug);
* 第三个参数:点击某行事件,自定义业务逻辑代码(这里只用简单的Toast来实现);
* 补充:dialog.dismiss()让对话框消失。
*/
@Override
public void onClick(DialogInterface dialog, int which) {
selectedId = which;//在用户单选时起到一个暂存的作用,方便确定是被引用
Toast.makeText(MainActivity4.this, "您当前选择了:"+stars[which], Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity4.this, "您最终的决定是:"+stars[selectedId], Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//空操作的方法
}
})
.show();
return true;
}
ArrayList<Integer> choice = new ArrayList<>();//创建一个ArrayList用来保存在Dialog中所选择的选项,同理设置为全局变量为了穿透作用域
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//长按之多选框对话框
final String [] interests={"看书","打篮球","大lol","听音乐","看电影","跑步"};//设置为final(常量)的原因是可以穿透作用域
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("你的爱好是什么?")
/*
* setMultiChoiceItems()方法中的三个参数
* 第一个参数:资源数组;
* 第二个参数:默认情况下的初始值,这里因为是多选,所以设置为boolean类型的数组;
* 第三个参数:多选框的点击事件;
*
*/
.setMultiChoiceItems(interests,
new boolean []{false,false,false,false,false,false},
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
/*
* 第一个参数:dialog对象;
* 第二个参数:多选对话框中用户所选中的项;
* 第三个参数:是否选中,true为选中,false为没有选中
*
*/
//选中则添加,后悔是时还可以remove掉
if(isChecked){
choice.add(which);
}else{
choice.remove(which);
}
Toast.makeText(MainActivity4.this, "您当前的选择是:"+choice.toString(), Toast.LENGTH_LONG).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity4.this, "您最终的决定是:"+choice.toString(), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", null)
.show();
return true;
}
}
标签:相同 .class 数据源 2.0 界面 new putextra 代码简洁 ack
原文地址:http://blog.csdn.net/oldtogether/article/details/53704466