实验六外部存储于ListView
来源:互联网 发布:知乎 日本的穷人 编辑:程序博客网 时间:2024/06/11 13:35
1.ListView与SimpleAdapter
开始的时候由于使用的Android版本太高,在DDMS中的File Explorer中没有显示任何东西,可能是因为权限的问题吧。改用Android4.4后就行了。
MainActivity.javapackage zhku.edu.exp6_1;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;public class MainActivity extends Activity { TextView tv; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv= (TextView) findViewById(R.id.textView1); int[] ids={R.id.title,R.id.score,R.id.people, R.id.edition,R.id.date,R.id.file_size,R.id.system, R.id.img}; String[] keys={"title","score","people","edition", "date","file_size","system","img"}; List<Map<String,Object>> data=getDataFromFile(); SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.list_item,keys,ids); ListView lv= (ListView) findViewById(R.id.listView1); lv.setAdapter(adapter); //设置点击事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listView = (ListView)parent;//获得listView Map<String, Object> map = (Map<String, Object>) listView.getItemAtPosition(position);//取数据 Intent intent = new Intent(MainActivity.this, BActivity.class); ArrayList list = new ArrayList();//用来封装map 数据的list Bundle bundle = new Bundle(); list.add(map);//添加map 数据到list bundle.putParcelableArrayList("data", list);//传数据到bundle intent.putExtras(bundle); startActivity(intent);//启动下一个窗体 } }); } private List<Map<String,Object>> getDataFromFile() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// Map<String,Object>map=new HashMap<String,Object>(); String state = Environment.getExternalStorageState();//获取当前状态 if (state.equals(Environment.MEDIA_MOUNTED)) { //判断SD卡是否已挂载 File SDPath = Environment.getExternalStorageDirectory();//获取外部存储根目录 String filePath = SDPath.getPath() + "/DCIM"; File file = new File(filePath, "exp6_1.txt"); FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); Log.i("getDataFromFile", "error"); return list; } Scanner in = new Scanner(fis); while (in.hasNext()) { Map<String, Object> map = new HashMap<String, Object>(); map.put("title", in.next()); map.put("score", in.next()); map.put("people", in.nextInt()); map.put("edition", in.next()); map.put("date", in.next()); map.put("file_size", in.next()); map.put("system", in.next()); String imgFile = in.next(); int id = getImageId(imgFile, this); //封装图片id 转换 map.put("img", id); list.add(map); } } return list; } public static int getImageId(String imgName,Context context){ String[] names=imgName.split("\\."); if(names.length==0) return R.drawable.ic_launcher; int id=context.getResources().getIdentifier(names[0], "drawable", "zhku.edu.exp6_1"); if(id==0) return R.drawable.ic_launcher; else return id; }}
Activity_main.xml<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="zhku.edu.exp6_1.MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sumtitle" android:textColor="#0000FF" android:textSize="18sp"/> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#AAAAAA" android:dividerHeight="2px"/></LinearLayout>Lsit_item.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" android:textSize="20sp"/> <TextView android:layout_marginLeft="10dp" android:layout_below="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/score" /> <TextView android:id="@+id/people" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_toRightOf="@+id/score"/> <TextView android:layout_below="@+id/title" android:layout_toRightOf="@+id/people" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edition"/> <TextView android:layout_below="@+id/title" android:layout_toRightOf="@+id/edition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/date" /> <TextView android:layout_below="@+id/title" android:layout_toRightOf="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/file_size" /> <TextView android:layout_below="@+id/title" android:layout_toRightOf="@+id/file_size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/system" /> </RelativeLayout></LinearLayout>结果显示:
2.文件外部存储
BActivity.javapackage zhku.edu.exp6_1;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.util.ArrayList;import java.util.Map;public class BActivity extends Activity implements View.OnClickListener { TextView topTitle,tv_score,tv_people,tv_title; Button btn_sure,btn_cancel; Map<String, Object>map; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); Intent it = getIntent(); Bundle bundle = it.getExtras();//接收传入数据 ArrayList list = bundle.getParcelableArrayList("data"); map = (Map<String, Object>) list.get(0);//map 是BActivity 的成员变量 RefreshData();//在界面上TextView 显示map 中的数据 btn_sure= (Button) findViewById(R.id.btn_sure); btn_cancel= (Button) findViewById(R.id.btn_cancel); btn_sure.setOnClickListener(this); btn_cancel.setOnClickListener(this); } public void onClick(View v){ if(v.getId()==R.id.btn_sure){//点击确定按钮,更新数据 Double score=Double.parseDouble(map.get("score").toString()); int people=Integer.parseInt(map.get("people").toString()); score=(score*people+100)/(people+1); people=people+1; String aftScore=String.format("%.3f",score);//保留小数点后3位 String aftPeople=String.valueOf(people); tv_score.setText(aftScore); tv_people.setText(aftPeople); } else if(v.getId()==R.id.btn_cancel){//点击取消按钮,返回到MainActivity finish(); } } private void RefreshData() { topTitle= (TextView) findViewById(R.id.tv_Toptitle); tv_score= (TextView) findViewById(R.id.tv_score); tv_people= (TextView) findViewById(R.id.tv_people); tv_title= (TextView) findViewById(R.id.tv_title); String perScore=map.get("score").toString(); String prePeople=map.get("people").toString(); String preTitle=map.get("title").toString(); topTitle.setText(preTitle); tv_score.setText(perScore); tv_people.setText(prePeople); tv_title.setText(preTitle); }}
Activity_b.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_Toptitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="35sp" android:textColor="#0000ff"/> <LinearLayout android:layout_marginTop="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/score" android:textSize="20sp"/> <TextView android:id="@+id/tv_score" android:layout_width="200dp" android:layout_height="26dp" android:textSize="20sp" android:textColor="#ff0000"/> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/people" android:textSize="25sp"/> <TextView android:id="@+id/tv_people" android:layout_width="200dp" android:layout_height="26dp" android:textSize="20sp" android:textColor="#ff0000"/> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sure1" android:textSize="20sp"/> <TextView android:id="@+id/tv_title" android:layout_width="250dp" android:layout_height="26dp" android:textSize="18sp" android:textColor="#ff0000"/> </LinearLayout> <TextView android:layout_marginLeft="30sp" android:layout_marginTop="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sure2" android:textSize="20sp"/><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_sure" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sure" android:background="#00ff00"/> <Button android:id="@+id/btn_cancel" android:layout_marginTop="50dp" android:layout_marginLeft="120dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel" android:background="#ffff00"/> </LinearLayout></LinearLayout>结果显示:
0 0
- 实验六外部存储于ListView
- 数据库实验六---存储过程
- 实验六 共享存储区通信
- 实验六:实现二叉树链式存储结构
- 实验六
- 实验六
- 实验六
- 实验六
- 实验六
- 实验六
- 实验六
- 实验六
- 外部存储
- 外部存储
- 实验 ListView
- 【STM32l0B-LK1学习板】之六 例程5- 外部中断实验
- 内部存储 外部存储
- 【实验三】外部中断实验
- 【深度学习:CNN】GoogLeNet (1)
- 航空售票系统--数据库代码部分
- Laravel 5.1 中路由绑定 Controller 包含子目录写法
- Codeforces Round #379 (Div. 2)
- 建议FastDateFormat来代替JDK自带的DateFormat
- 实验六外部存储于ListView
- eclipse java项目无法添加到servers
- 机器学习算法的面试表达
- 程序员面试金典: 9.5位操作 5.8通过位操作进行两点连线
- 【安卓开发-3】Activity的四种启动模式、Intent的显式和隐式使用方式
- cache-cloud 编译
- 关于g++和gcc的一些误区
- STM32端口重映射
- 第2章 装配Bean(笔记)