Android implements a simple plain text ListView

line of thought:

1. Create a ListViewActivity, and write a ListView layout in the LinearLayout layout

2. Create a TextView layout for the ArrayAdapter adapter to use

3. Import the TextView layout and data into the adapter ArrayAdapter

3. Import the content adapted by the ArrayAdapter into the ListView layout

code show as below:

Layout of ListViewActivity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/SimpleListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

Layout of TextView:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListTextView"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginLeft="20dp"
    android:text="内容"
    android:textSize="50dp">


</TextView>

onCreate implementation code:

package com.example.prize.mylistviewdemoapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SimpleListView extends AppCompatActivity {
    private String [] data ={"Apple","Orange","Mango","Banana","Lemon","Dragon Fruit","Watermelon","Plum",
            "Guava", "Pomegranate", "Grape", "Lychee", "Saint Tomato", "Yrici", "Persimmon", "Mangosteen", "Carambola", "Sydney", "Kiwi", "Durian"
            ,"loquat","cherry","grapefruit","peach","mulberry","lotus mist"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_simple_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SimpleListView.this,R.layout.list_layout,data);//适配器
        ListView listView = (ListView) findViewById(R.id.SimpleListView); //找到ListView布局
        listView.setAdapter(adapter); //Import
    }
}

Realize the effect diagram:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733925&siteId=291194637