Android显示实例列表中的字段

时间:2016-01-08 21:54:36

标签: java android

我有一个来自课堂的实例列表。每个实例都有4个字段。现在我想显示这些字段。这样做的正确方法是什么?

这是我的文件夹hierarchie

image_1

MainActivity

public class MainActivity extends AppCompatActivity {
    public userManager userManager = new userManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createUser(View view){
        EditText username = (EditText)findViewById(R.id.iUsername);
        EditText password = (EditText)findViewById(R.id.iPassword);
        EditText age = (EditText)findViewById(R.id.iAge);
        EditText sex = (EditText)findViewById(R.id.iSex);


        this.userManager.createUser(username.getText().toString(), password.getText().toString(), Integer.parseInt(age.getText().toString()), Integer.parseInt(sex.getText().toString()));
    }

    public void showAllUser(View view){

    }
}

userManager

public class userManager {
    public List<user> userList = new ArrayList<user>();

    public boolean createUser(String username, String password, int age, int sex){
        if(username != null && !username.isEmpty() || password != null && !password.isEmpty()) {
            this.userList.add(new user(username, password, age, sex));
            return true;
        }
        return false;
    }

    public void displayAllUser(){

    }
}

user

public class user {
    //region properties
    //region username
    private String username;

    public void setUsername(String username){
        this.username = username;
    }

    public String getUsername(){
        return this.username;
    }
    //endregion

    //region password
    private String password;

    public void setPassword(String password){
        this.password = password;
    }

    public String getPassword(){
        return this.password;
    }
    //endregion

    //region age
    private int age;

    public void setAge(int age){
        this.age = age;
    }

    public int getAge(){
        return this.age;
    }
    //endregion age

    //0 don't know, 1 male, 2 female
    //region sex
    int sex;

    public void setSex(int sex){
        this.sex = sex;
    }

    public int getSex(){
        return this.sex;
    }
    //endregion sex
    //endregion properties

    //region c'tor
    public user(){

    }

    public user(String username, String password, int age, int sex){
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    //endregion
}

这是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:orientation="vertical"
    tools:context="overbit.user.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_username"/>

    <EditText
        android:id="@+id/iUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_password"/>

    <EditText
        android:id="@+id/iPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_age"/>

    <EditText
        android:id="@+id/iAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_sex"/>

    <EditText
        android:id="@+id/iSex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/button_add_user"
            android:onClick="createUser"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/button_output_user"
            android:onClick="showAllUser"/>
    </LinearLayout>
</LinearLayout>

我可以写入方法displayAllUser(class = userManager)a for loop,以获取每个实例及其字段。但是之后?我必须把它们还给MainActivity?如何将这样的大量数据返回到MainActity

1 个答案:

答案 0 :(得分:1)

  

使用适配器构建布局

看看这个: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

并且: https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView

您需要AdapterView类型的视图 您可以通过将AdapterView实例绑定到适配器来填充AdapterView(如ListView或GridView),该适配器从外部源检索数据并创建表示每个数据条目的View。

Android还提供了几个Adapter的子类,可用于检索不同类型的数据和构建AdapterView的视图。

检查以上链接,了解如何实施相同的内容。