【实验】ListView、bundle的使用

2024-08-27 06:18
文章标签 使用 实验 bundle listview

本文主要是介绍【实验】ListView、bundle的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


主要内容:

从跳转到另一个界面,并且可以返回。

                                                               

                           图一                                                                                                                        图二



xml:

item:

<LinearLayout 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:orientation="horizontal"tools:context=".MainActivity" ><ImageView android:id="@+id/image"android:layout_width="50dp"android:layout_height="50dp"/><TextView android:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>


activity_main:

<LinearLayout 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:orientation="vertical"tools:context=".MainActivity" ><ListView android:id="@+id/list"android:layout_width="fill_parent"android:layout_height="wrap_content"></ListView></LinearLayout>


activity_main2:

<LinearLayout 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:orientation="vertical"tools:context=".MainActivity" ><TextView android:id="@+id/tv"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Button android:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="back"/></LinearLayout>


第一种方法是使用SimpleAdapter:

package com.example.lab3;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity {ListView list;private List<Map<String,Object>> mDataList = new ArrayList<Map<String,Object>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();setData();  //为mDataList添加元素//为ListView添加元素的适配器SimpleAdapter listItemAdapter = new SimpleAdapter(this,mDataList,R.layout.item,new String[]{"name","image"},new int[]{R.id.name,R.id.image});//ListView关联适配器list.setAdapter(listItemAdapter);//定义适配器的各个点击事件list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {Bundle bundle = new Bundle();String fruitName ="";switch(arg2){case 0:fruitName = "apple";//bundle.putString("fruit", "apple");break;case 1:fruitName = "banana";//bundle.putString("fruit", "banana");//Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();break;case 2:fruitName = "cherry";//bundle.putString("fruit", "cherry");//Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();break;case 3:fruitName = "coco";break;case 4:fruitName = "kiwi";break;case 5:fruitName = "orange";break;case 6:fruitName = "pear";break;case 7:fruitName = "strawberry";break;case 8:fruitName = "watermelon";break;default:break;}bundle.putString("fruit", fruitName);Intent intent = new Intent(MainActivity.this, MainActivity2.class);intent.putExtras(bundle);startActivity(intent);}});}public void init(){list = (ListView)findViewById(R.id.list);}public void setData(){Map<String,Object> map = new HashMap<String,Object>();map.put("name", "Apple");map.put("image", R.drawable.apple);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Banana");map.put("image", R.drawable.banana);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Cherry");map.put("image", R.drawable.cherry);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Coco");map.put("image", R.drawable.coco);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Kiwi");map.put("image", R.drawable.kiwi);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Orange");map.put("image", R.drawable.orange);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Pear");map.put("image", R.drawable.pear);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Strawberry");map.put("image", R.drawable.strawberry);mDataList.add(map);map = new HashMap<String,Object>();map.put("name", "Watermelon");map.put("image", R.drawable.watermelon);mDataList.add(map);}}/** * 1.init()位置* 2.break的使用;* * */


然后跳转到MainActivity2:

package com.example.lab3;import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class MainActivity2 extends Activity{TextView tv;Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);init();Bundle bundle = new Bundle();bundle = this.getIntent().getExtras();String fruit = bundle.getString("fruit");  //获取标签为fruit的内容tv.setText("I love " + fruit + "!!!");tv.setTextColor(Color.RED);        //设置字体颜色btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//Intent intent_back = new Intent(MainActivity2.this, MainActivity.class);//startActivity(intent_back);finish();  //finish()可以避免了上述方法中新建一个activity的冗赘}});}public void init(){tv = (TextView)findViewById(R.id.tv);btn = (Button)findViewById(R.id.btn);}
}


或者是自定义一个FruitAdapter方法。

首先是Fruit一个类的定义:

/** 定义一个Fruit类,封装name和image*/
package com.example.lab3;public class Fruit {private String name;private int imageId;public Fruit(String name,int imageId){this.name = name;this.imageId = imageId;}public String getName(){return name;}public int getImageId(){return imageId;}}

然后自定义适配器:

/** 自定义一个适配器*/
package com.example.lab3;import java.util.List;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;public class FruitAdapter extends ArrayAdapter<Fruit> {private int resourceId;public FruitAdapter(Context context, int textViewResourceId,List<Fruit> fruits) {super(context,  textViewResourceId, fruits);// TODO Auto-generated constructor stubresourceId = textViewResourceId;}@Overridepublic View getView(int position,View convertView,ViewGroup parent){Fruit fruit = getItem(position);View view = LayoutInflater.from(this.getContext()).inflate(resourceId,null);ImageView fruitImage = (ImageView)view.findViewById(R.id.image);	TextView fruitName = (TextView)view.findViewById(R.id.name);fruitImage.setImageResource(fruit.getImageId());fruitName.setText(fruit.getName());return view;}}


最后是MainActivity3:

package com.example.lab3;import java.util.ArrayList;
import java.util.List;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.AdapterView.OnItemLongClickListener;
import android.widget.ListView;public class MainActivity3 extends Activity {private List<Fruit> fruitList = new ArrayList<Fruit>();ListView list;FruitAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fruitList.add(new Fruit("apple",R.drawable.apple));fruitList.add(new Fruit("banana",R.drawable.banana));fruitList.add(new Fruit("cherry",R.drawable.cherry));fruitList.add(new Fruit("coco",R.drawable.coco));fruitList.add(new Fruit("kiwi",R.drawable.kiwi));fruitList.add(new Fruit("orange",R.drawable.orange));fruitList.add(new Fruit("pear",R.drawable.pear));fruitList.add(new Fruit("strawberry",R.drawable.strawberry));fruitList.add(new Fruit("watermelon",R.drawable.watermelon));adapter = new FruitAdapter(MainActivity3.this, R.layout.item, fruitList);list = (ListView)findViewById(R.id.list);//ListView关联适配器list.setAdapter(adapter);list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubBundle bundle = new Bundle();String fruitName ="";switch(arg2){case 0:fruitName = "apple";//bundle.putString("fruit", "apple");break;case 1:fruitName = "banana";//bundle.putString("fruit", "banana");//Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();break;case 2:fruitName = "cherry";//bundle.putString("fruit", "cherry");//Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();break;case 3:fruitName = "coco";break;case 4:fruitName = "kiwi";break;case 5:fruitName = "orange";break;case 6:fruitName = "pear";break;case 7:fruitName = "strawberry";break;case 8:fruitName = "watermelon";break;default:break;}bundle.putString("fruit", fruitName);Intent intent = new Intent(MainActivity3.this, MainActivity2.class);intent.putExtras(bundle);startActivity(intent);}});list.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubfruitList.remove(arg2);list.setAdapter(adapter);return false;}});}
}


注:两种方法共用了MainActivity2的界面,以后还是第二种方法比较常见。


这篇关于【实验】ListView、bundle的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110869

相关文章

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当