Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Android ListView

Shulou Source: shulou.com Published: 2022-06-03 16:45:37 10月04日 Update

This article introduces you to the example analysis of Android ListView, the content is very detailed, interested friends can refer to, hope to be helpful to you.

I. Preface

In some scenarios, the ListView Item of a single text is no longer suitable for the current needs, so we need to customize the Item layout to meet the needs. Let's implement an Item with icons and text.

Second, code display

1. Define the layout file activity_main.xml,Activity that contains ListView to be loaded when onCreate ().

two。 Defines the Item layout file listview_item.xml, which is used when creating SimpleAdapter objects.

3. Perfect MainActivity.java code.

Package com.example.listviewdemo2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity {private ListView mListView = null; private List mListItems = null; private Map mMap = null; private SimpleAdapter mAdapter = null / * ID array of pictures * / private int [] mImageId = new int [] {R.drawable.num_0, R.drawable.num_1, R.drawable.num_2, R.drawable.num_3, R.drawable.num_4, R.drawable.num_5, R.drawable.num_6, R.drawable.num_7, R.drawable.num_8, R.drawable.num_9,} / * text list array * / private String [] mTitle = new String [] {"number 0", "number 1", "number 2", "number 3", "number 4", "number 5", "number 6", "number 7", "number 8", "number 9",}; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_main); init ();} private void init () {mListView = findViewById (R.id.listview); mListItems = new ArrayList (); for (int I = 0; I < mImageId.length; iTunes +) {mMap = new HashMap (); mMap.put ("image", mImageId [I]) MMap.put ("title", mTitle [I]); mListItems.add (mMap);} mAdapter = new SimpleAdapter (this, mListItems, R.layout.listview_item, new String [] {"title", "image"}, new int [] {R.id.textview, R.id.imageview}); mListView.setAdapter (mAdapter);}} III.

The running effect is as follows:

This is the end of the case analysis of Android ListView. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Numbers layout text analysis code content effects arrays files more requirements help runs examples good interests prefaces icons pictures scenarios Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info vpn Microsoft Docker