Androidでリストビューをカスタムする

1. Listに持たせるオブジェクトを作成
<pre>
 public class Student {
    long id;
    String number;
    String name;
    String status;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getStatus() {
        return status;
    }

    public void setStatus(String tweet) {
        this.status = tweet;
    }
</pre>

2. ListViewの行に使うレイアウトファイルを作成する
<pre>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/id"
        android:layout_marginStart="139dp"
        android:layout_marginTop="50dp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="279dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/status"
        android:layout_marginStart="50dp" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/name"
        android:layout_marginTop="23dp" />
</RelativeLayout>
</pre>

3. カスタムしたAdapterを作成する
CustomAdapter.javaを作成、下記を追加

<pre>
package com.example.honka_1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter{
    Context context;
    LayoutInflater layoutInflater = null;
    ArrayList<Student> studentList;

    public CustomAdapter(Context context) {
        this.context = context;
        this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setTweetList(ArrayList<Student> tweetList) {
        this.studentList = tweetList;
    }

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

    @Override
    public Object getItem(int position) {
        return studentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return studentList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.studentrow,parent,false);
        ((TextView)convertView.findViewById(R.id.id)).setText(studentList.get(position).getNumber());
        ((TextView)convertView.findViewById(R.id.name)).setText(studentList.get(position).getName());
        ((TextView)convertView.findViewById(R.id.status)).setText(studentList.get(position).getStatus());

        return convertView;
    }
}

</pre>

4 ActivityあるいはFragmentにListViewを設置
<pre>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
        android:id="@+id/listView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</android.support.constraint.ConstraintLayout>

</pre>

5 ListViewにAdapterをセット
<pre>
public class Main2Activity extends AppCompatActivity {

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

        Intent intent = this.getIntent();
        Integer selectedDay = intent.getIntExtra("selectedDay",0);


        Log.i("test", selectedDay.toString());

        ListView listView = (ListView)findViewById(R.id.listView);

        ArrayList<Student> list = new ArrayList<>();
        CustomAdapter adapter = new CustomAdapter(Main2Activity.this);
        adapter.setTweetList(list);
        listView.setAdapter(adapter);

        Student student = new Student();
        student.setNumber("1");
        student.setName("和田");
        student.setStatus("出席");
        list.add(student);

        adapter.notifyDataSetChanged();



    }
}
</pre>