Usage of Android Studio’s ListView

Hits: 0

First, let’s take a look at the effect (in simple terms, it’s a bit like, a list of qq or WeChat chats)

(java version)

First create a new project for ListViewText, then create an avd and write the code in activity_main

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv" />

</LinearLayout>

Then create a new [list_item.xml] ,

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

    <TextView
        android:id="@+id/tv"
        android:textSize="30sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

The activity_main file is the entire screen, and list_item.xml is a small list of the screen (I don’t know if I explained it clearly), and then create a new Bean class in java and two methods
in the MyAdapter class Bean

package com.example.listviewtext;

public class Bean {
    String name;

    public String getName() {
        return name;
    }

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

Inside MyAdapter

package com.example.listviewtext;

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.List;

public class MyAdapter extends BaseAdapter {

    private List<Bean> data; //Create data of private Bean class 
    private Context context;

    public MyAdapter(List<Bean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override 
    public  int  getCount ()  {
         return data.size(); //Get the length of data
    }

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

    @Override
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
        }
        TextView textView = view.findViewById(R.id.tv);
        textView.setText(data.get(i).getName()); //The system will go to the R file to find the value of type type that matches the String value 
        return view;
    }
}

Then the code in MainActivity

package com.example.listviewtext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean> data = new ArrayList<>(); //The ArrayList class is an array that can be dynamically modified. The difference from ordinary arrays is that it has no fixed size limit, and we can add or delete elements

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

        for (int i = 1; i <= 100; i++) {
            Bean bean = new Bean();
            bean.setName( "th" + i + "th" );
            data.add(bean); //Add data to data through add
        }
        ListView listView = findViewById(R.id.lv); //Get the id, which is the id of the list in the screen mentioned above 
        listView.setAdapter( new MyAdapter(data, this )); //Adapter functions to match various data to appropriate The form is displayed in the View for the user to see
    }
}

Then click Run to see the effect.
If you don’t want to pass values ​​through for, you can also use the new method to create them one by one.

package com.example.listviewtext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Bean> data = new ArrayList<>();

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

        Bean bean = new Bean();
        bean.setName("a");
        data.add(bean);
        Bean bean1 = new Bean();
        bean1.setName("b");
        data.add(bean1);

        ListView listView = findViewById(R.id.lv);
        listView.setAdapter(new MyAdapter(data,this));


    }
}

see the effect

(kotlin version)

First create a new project for ListViewText, then create an avd and write the code in activity_main

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Then the code in MainActivity

package com.example.listview2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val data = listOf("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15")


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,data)
        listView.adapter = adapter

    }
}

The effect is as follows:

I am a newbie, please correct me if I am wrong.

hhhh (from Xiaomeng’s new sharing)
(seeking attention)

(Continuously updated…)

You may also like...

Leave a Reply

Your email address will not be published.