Usage of file storage, list control ListView and adapter SimpleAdapter and BaseAdapter

Keywords: Java Android

1, File storage

file store
File storage in Android is divided into internal storage and external storage.

Internal storage: store the data of the application into the device in the form of a file (under data / [package name of your APP] / files). When the created application is uninstalled, its internal storage file will be deleted.
External storage: refers to the storage of files on some external devices, such as SD card or memory card embedded in the device. It is a permanent storage method.

(1) Internal storage

Several methods to obtain internal storage path:

Internal storage IO stream acquisition method:

Internal storage file

Internal storage fetch file

Four modes of writing and reading

(2) External storage

Get external storage path
File file = Environment.getExternalStorageDirectory()
Return to external storage root

External storage IO stream acquisition method
Use the new keyword to create an IO stream object

Creating files from external storage

External storage file

External storage fetch file

Static permission application
The static permission application method is applicable to Android versions below 6.0. This method is in
The required permissions are declared in the AndroidManifest.xml file. Apply for external storage read / write permission as shown below.

 dynamic authority application
In Android 6.0 and above systems, Android divides permissions into normal permissions and dangerous permissions. Normal permissions refer to permissions that do not directly pose a risk to privacy, such as requesting network permissions.
Dangerous permission refers to the permission involving the user's privacy. The application applying for this permission may involve the user's privacy data, or may affect the user's stored data or the operation of other applications. The dangerous authority is divided into nine
Groups: storage, camera, location, calendar
Contacts, sensors, microphone, phone, SMS
There are 24 related permissions for SMS.
When applying for normal permissions, you can only use static permissions. For dangerous permissions, you need user authorization. Therefore, you need to add permissions not only in the AndroidManifest.xml file, but also in the
Dynamic permission application in code.

be careful:
① In the external storage, the getExternalStorageState() method of Environment has been deprecated. Although it can still be used, for the sake of user privacy and security, the getExternalStorageState() method of Context can be used
getExternalFilesDir(null); Method substitution.
② Internal storage files will be deleted with the uninstallation of the application;
External storage is permanent.

2, Usage of list control ListView and adapter

A ListView usually has two responsibilities:

(1) Populate data into layout

(2) Handle the user's selection, click and other operations

Three elements are required to create a ListView:

(1) The View of each column in the ListView; (2) Fill in the data or pictures of View; (3) Adapter for linking data to ListView.

To create an adapter:
**The adapter is a bridge linking data and AdapterView (ListView is a typical AdapterView), * * it can effectively separate the setting of data and AdapterView, making the binding between AdapterView and data easier and easier to modify. This is also the embodiment that Android fully follows the MVC design pattern.

Android provides many adapters, including several common ones:

ArrayAdapter(T) ------------------- to bind an array and support generic operations

SimpleAdapter --------------------------------------- used to bind the data corresponding to the control defined in XML

SimpleCursorAdapter -------------------- used to bind the data obtained by the cursor

BaseAdapter ---------------------------------------- common base adapter

1.ListView uses SimpleAdapter
Achieve the planet list effect as shown in the figure

(1)activity_ list_ view_ test_ Activity.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

(2) Import the pictures required for the case.
In the res directory, create a drawable xxhdpi folder and put the required pictures in the folder. Or put it in the mipmap folder

(3) Write list item layout (linear layout, relative layout)
The list items that finally achieve the effect include pictures and text, and the layout of list items needs to be customized. Relative layout is used here
item_listviewset.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >




(4) Writing java code
Write functional logic code in

public class ListViewTestActivty extends AppCompatActivity {
    private String[] datas = new String[]{"Wisdom College", "College of mechanical and electrical engineering", "College of Education", "college of art"};
    private ListView listViewTest;
    private int[] iconArray =
            {R.mipmap.shuixing, R.mipmap.jinxing, R.mipmap.diqiu,
                    R.mipmap.huoxing, R.mipmap.muxing, R.mipmap.tuxing};
    private String[] starArray={"Mercury", "Venus", "earth", "Mars", "Jupiter", "Saturn"};
    private String[] str= {"Message 1","Information 2","Information 3","Information 4","Information 5","Information 6"};
    private ArrayList dataed;
    private HashMap listData;
    protected void onCreate(Bundle savedInstanceState) {

        dataed=new ArrayList();

        //Store data and create hashmap set
        for(int i=0;i<iconArray.length;i++){
            listData=new HashMap();
            //Multiple map sets are stored

        //2. Create a SimpleAdapter adapter object
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,dataed,R.layout.item_listviewset,new String[]{"image","name","introduces"},new int[]{,,});

        //Create an ArrayAdapter adapter object
      //  ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item_listviewset, datas);

        //3. Set up adapter
        listViewTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(ListViewTestActivty.this,"You are currently clicking:"+starArray[i], Toast.LENGTH_LONG).show();


The above code can realize a simple list item, including text and pictures

The operation effect is as follows:

Posted by hermand on Sat, 30 Oct 2021 19:58:11 -0700