Android 实现数据的列表显示

界面操作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
    android:id ="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登陆记录"
    android:textSize="20dp"
    />
    <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:id="@+id/list"
    >
    </ListView>
</RelativeLayout>

后台实现:

package com.example.abaka.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                final TextView textView = (TextView)findViewById(R.id.text);
                ListView listView=(ListView)findViewById(R.id.list);
                List<String> list = new ArrayList<String>();
                list.add("西安市未央区");
                list.add("西安市未央区");
                list.add("西安市未央区");
                list.add("西安市未央区");
                list.add("西安市未央区");
                list.add("西安市未央区");
                ///可以一直添加,在真机运行后可以下拉列表
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
                listView.setAdapter(adapter);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39032310/article/details/84726223
今日推荐