LoginSignup
2
0

More than 5 years have passed since last update.

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

Posted at
  1. Listに持たせるオブジェクトを作成

    
    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;
    }
    
  2. ListViewの行に使うレイアウトファイルを作成する

    
    <?xml version="1.0" encoding="utf-8"?>
    
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    
        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" />
    
    
        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" />
    
    
        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" />
    
    
  3. カスタムしたAdapterを作成する
    CustomAdapter.javaを作成、下記を追加


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 studentList;

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

    public void setTweetList(ArrayList 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;
    }
}

4 ActivityあるいはFragmentにListViewを設置


<?xml version="1.0" encoding="utf-8"?>

    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" />




5 ListViewにAdapterをセット


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();



}


}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0