directory

What is a ListView?

ListView (ListView

ListView click to respond to the event


Hello, I’m Grey Ape! A super BUG writing program ape!

Today in here to record the Android development of Listview about the use of the tutorial, but also a consolidation of knowledge!

What is a ListView?

ListView is a list box in Android development that displays the information you want to display as a horizontal list.

 

ListView (ListView

In · XML, Listview has the following basic attributes

  • Android: Divider =”#f00″ The color of the dividing line
  • Android :dividerHeight=”2dp” dividerHeight
  • Android :cacheColorHint=”#0fff” When a list uses a background image, there is a problem with either a dropdown or a dropdown: the background image is missing
  • There is a problem with pull-down and pull-up lists when using background images: black blocks appear
  • Android :fadingEdge=” None “When a drop is detected, the top and bottom shadows appear
  • Android :listSelector=”#0000″ remove the background when lTEM is clicked

So how do you use it in real development?

1) Create a new project and add the ListView control to the activity_main.xml file as follows:

Set the width and height of the control to match_parent to fill the entire space


      
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".moveice_view">

    <ListView
        android:id="@+id/moveiceList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="MissingConstraints">

    </ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code

2) Configure the MainActivity code

The ListView uses an array of characters to hold each row, but the parameters set to the ListView are Adapter types. Obviously, we can’t pass in an array of strings, so we need to use an ArrayAdapter class as a bridge. Let’s convert the string array,

This may be a bit logical and not easy to understand, but look at the following code:

package com.example.summarizepj;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class moveice_view extends AppCompatActivity {

    ListView moveiceList;
    String [] moveice_arr = {"Shawshank Redemption."."Forrest Gump"."Tomorrow will be better."."Fast and Furious"."The Founding of an army"."Hello, Li Huanying."};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moveice_view);

        moveiceList = findViewById(R.id.moveiceList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(moveice_view.this,android.R.layout.simple_list_item_1,moveice_arr); moveiceList.setAdapter(adapter); }}Copy the code

Now to explain the ArrayAdapter,

Here I use a TextView layout file included with the system: Android.r.layout. simple_expandable_list_item_1, which is called conveniently and has the following five arguments

  • Android. R.l ayout. Simple_list_item_1 one line of text
  • Android.r.layout. simple_list_item_2 line title, line text
  • Android.r.layout. simple_list_ITEM_singLE_CHOICE radio button
  • Android.r.layout. simple_list_ITEM_multiple_choice Multi-select button
  • android.R.layout.simple_list_item_checked    checkbox

ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, data); It means: Create an array adapter code, there are three parameters, the first parameter is the context, is the current Activity, the second parameter is the Android SDK built-in layout, there is only one TextView, this parameter is to indicate that the layout of each data in our array is this view, Is to display every piece of data on this view; The third parameter is the data we want to display. According to these three parameters, the listView will iterate over each piece of data in the data, read one, and display it in the layout corresponding to the second parameter, thus forming the listView we see.

After running the code above, you get something like this:

ListView click to respond to the event

But the above code does not respond when the corresponding button is clicked, as we usually see in the app after the click is triggered by an event, so let’s add the event response to the ListView control.

The setOnItemClickListener method is used to add an event listener to the ListView control. The setOnItemClickListener method is used as follows:

package com.example.summarizepj;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class moveice_view extends AppCompatActivity {

    ListView moveiceList;
    String [] moveice_arr = {"Shawshank Redemption."."Forrest Gump"."Tomorrow will be better."."Fast and Furious"."The Founding of an army"."Hello, Li Huanying."};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moveice_view);

        moveiceList = findViewById(R.id.moveiceList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(moveice_view.this,android.R.layout.simple_list_item_1,moveice_arr);
        moveiceList.setAdapter(adapter);

        moveiceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<? > parent, View view,int position, long id) {
                String result = parent.getItemAtPosition(position).toString();
                switch (result){
                    case "Shawshank Redemption.":{
                        Toast.makeText(moveice_view.this."You clicked." + result, Toast.LENGTH_SHORT).show();
                    }
                    break;
                    case "Forrest Gump":{
                        Toast.makeText(moveice_view.this."You clicked." + result,Toast.LENGTH_SHORT).show();
                    }
                    break;
                    case "Tomorrow will be better.":{
                        Toast.makeText(moveice_view.this."You clicked." + result,Toast.LENGTH_SHORT).show();
                    }
                    break; }}}); }}Copy the code

When the corresponding list control is clicked, it looks like this:

Now that we’re done using the ListView and clicking on the response,

If you have any questions, please leave them in the comments section!

Feel good remember to like attention yo!