java - 我想要一个包含多个内容的数组列表的自定义 listView

标签 java android listview arraylist

我创建了一个名为 Student 的类,我想将学生的一些对象添加到 arrayList 中。 我在 java Android 中有一个 Activity ,我想通过单击“添加”按钮来创建每个对象。同时我想在该 Activity 上方的自定义 ListView 中显示学生姓名和地址,这意味着我想在布局中列出学生 arrayList。 我应该如何为这个数组列表制作一个适配器? 任何人都可以完成这些代码吗?

Button btnadd = (Button) findViewById(R.id.btnAdd);
        btnadd.setOnClickListener(new OnClickListener(){

            public void onClick(View arg0){
                EditText etname      = (EditText)   findViewById(R.id.etName);
                EditText etaddress   = (EditText)  findViewById(R.id.etAddress);
                RadioGroup typeRadio = (RadioGroup)findViewById(R.id.radioType);
                List<Student> rlist = new ArrayList<Student>();
                rlist.add(new Student(etname.getText().toString(),
                                          etaddress.getText().toString(),
                                          typeRadio.toString()));
        }
        }
        );
    }

    private class MyArrayAdapter<String> extends ArrayAdapter<String>{

        ?
        ?
        ?
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_layout, parent, false);
        ImageView iv = (ImageView) row.findViewById(R.id.resType);
        TextView tv1 = (TextView) row.findViewById(R.id.resName);
        TextView tv2 = (TextView) row.findViewById(R.id.resAddress);
        ?
        ?
        ?
        ?
        return row;
    }

最佳答案

这是工作代码。您可以在 custom_row.xml 中为每一行添加您想要的任何内容。

MainActivity.class

public class MainActivity extends ActionBarActivity {

    ListView listView;
    CustomAdapter adapter;
    ArrayList<Student> studentArrayList;
    EditText ename, eaddress;
    Button enter;

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

        listView = (ListView) findViewById(R.id.listView);
        ename = (EditText)findViewById(R.id.eName);
        eaddress = (EditText)findViewById(R.id.eAddress);
        enter = (Button) findViewById(R.id.enter);

        studentArrayList = new ArrayList<Student>();
        adapter = new CustomAdapter(this,studentArrayList);

        listView.setAdapter(adapter);

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String got_name = ename.getText().toString();
                String got_address = eaddress.getText().toString();

                if (got_name!=null && got_address!=null)
                {

                    Student student = new Student(got_name, got_address);
                    studentArrayList.add(student);
                    ename.setText(" ");
                    eaddress.setText(" ");
                    adapter.notifyDataSetChanged();

                }

            }
        });



    }


       private class CustomAdapter extends BaseAdapter {


        Context context;
        ArrayList<Student> studentArrayList;

        public CustomAdapter(MainActivity activity, ArrayList<Student> studentArrayList) {

            this.context = activity;
            this.studentArrayList = studentArrayList;
        }


        @Override
        public int getCount() {
            return studentArrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View v = view;

            if (v == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService
                        (Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.custom_row, null);

            }

            TextView studentName  = (TextView)v.findViewById(R.id.studentName);
            TextView studentAddress  = (TextView)v.findViewById(R.id.studentAddress);

            studentName.setText(studentArrayList.get(i).getName());
            studentAddress.setText(studentArrayList.get(i).getAddress());

            return v;
        }
    }
}

activity_main.xml

<RelativeLayout 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"  tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:id="@+id/listView"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Enter Name : "
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="22dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Enter Address : "
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="20dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eName"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/listView"
        android:layout_alignEnd="@+id/listView"
        android:layout_toRightOf="@+id/textView2"
        android:layout_toEndOf="@+id/textView2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eAddress"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/enter"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="32dp" />
</RelativeLayout>

学生.类(class)

public class Student {


    String name, address;


    public Student(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="New Text"
        android:id="@+id/studentName"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Medium Text"
        android:id="@+id/studentAddress"
        android:layout_below="@+id/studentName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

输出:

enter image description here

enter image description here

enter image description here

enter image description here

关于java - 我想要一个包含多个内容的数组列表的自定义 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609107/

相关文章:

java - 配置 Gradle 以加速 cordova-cli 构建

listview - 如何在 ListView 中拉伸(stretch) ListView 项

java - 我可以为每种不同类型的继承类拥有不同的静态变量副本吗

java - LinkedHashMap 类型转换

java - 为什么 getIcon() 在 setIcon(...) 之后不返回更新的图标?

java - 树莓派和 Java 安装

android - Google Maps API v2 Android 是否支持离线 map ?

android - 如何在opencv中将一个矩形放置在相机框架的中心

android - ListView Activity OnItemClickListener TextView 的空指针异常

android - fragment 类中 ArrayAdapter 的上下文