实验六外部存储于ListView

1.ListViewSimpleAdapter

开始的时候由于使用的Android版本太高,在DDMS中的File Explorer中没有显示任何东西,可能是因为权限的问题吧。改用Android4.4后就行了。

MainActivity.java
package 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.java
package 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值