Customize list view on Android

  1. Create an object to have in 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;
    }
  1. Create a layout file to use for ListView rows



    

    

    

  1. Create a custom Adapter Create CustomAdapter.java, add the following
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 Set ListView in Activity or Fragment




    


5 Set Adapter to ListView

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 list = new ArrayList<>();
        CustomAdapter adapter = new CustomAdapter(Main2Activity.this);
        adapter.setTweetList(list);
        listView.setAdapter(adapter);

        Student student = new Student();
        student.setNumber("1");
 student.setName ("Wada");
 student.setStatus ("attendance");
        list.add(student);

        adapter.notifyDataSetChanged();



    }
}

Recommended Posts

Customize list view on Android
Android view
How to "hollow" View on Android
[Android] List all setting items on the setting screen
Browse an instance of Tab's View on Android
[Android] Notes on xml
I saw the list view of Android development collectively
Multipart transmission library on Android
How to use OpenCV 4 on Android and view camera live view
Use serial communication on Android
ROS app development on Android
Use native code on Android
Android ripple effect (Custom View)
Event handling with RxBus on Android
Bridge commentary on React Native Android
[Android] Get the date on Monday
Get JUnit code coverage on Android.
Watson Assistant (formerly Conversation) on Android
Notes on calling Installer on Android App
Save ArrayList using GSON on Android
[Android] Simple Date Format format pattern list
Notes on Android (java) thread processing
[Android] Receive intent on Broadcast Reciever
[Kotlin / Android] Create a custom view
Implement ripple representation on images on Android
Speed up location acquisition on Android