ListView yes Android One of the most commonly used controls in .

When there's too much data to show ,ListView That comes in handy . It allows users to slide their fingers , Slide data in and out of the interface .

One 、 The simplest ListView Realization

1、 Modify layout file . We are activity_main.xml Add space to ListView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lkb.listviewtest.MainActivity"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView> </LinearLayout>

2、 go back to MainActivity.java in . First, we create the data we need to display .

private String[] fruit = {"apple","watermelon","mango","strawberry","pineapple","jujube","banana",
"apple","watermelon","mango","strawberry","pineapple","jujube","banana"};

3、 Finally, it is implemented in the code ListView Add data to . Data in an array cannot be passed directly to ListView Of , We need adapters .

Common adapters are ArrayAdapter,SimpleAdapter and SimpleCursorAdapter. Use here ArrayAdapter.

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,fruit);

Here we get one ArrayAdapter object .ArrayAdapter There are six constructors for . This corresponds to

/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
this(context, resource, 0, Arrays.asList(objects));
}
android.R.layout.simple_list_item_1 yes Android Built in a layout file of id. Here we use it as a ListView Layout of subitems .
Finally through ListView Of setAdapter Method , Pass in the constructed adapter object , such ListView The connection with the data is established .
 ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

Go through the three steps above , We have achieved ListView.


Android And ListView(1) More articles about

  1. Android— universal ListView Adapter

    ListView Is the most commonly used control in development , But it's always written in duplicate code , There's no point in wasting time . Recently, I refer to some materials , Find a panacea ListView Adapter , Less code , Save time , Summarize and share with you . First, there is a custom ...

  2. Android in ListView Realize the image and text parallel and custom split line ( Perfect imitation wechat APP)

    Yesterday's ( Early this morning ) 's blog post <Android in Fragment and ViewPager That thing > in , We use Fragment and ViewPager Imitated and realized the Layout Framework of wechat . Today we're going to use Li ...

  3. Android And ListView performance optimization —— One line of code binds data —— Universal adapter

    Here's the picture , Now there is a demand graph like this , What would you do ? As a beginner , I used to use it directly before SimpleAdapter Combine one Item The layout of the , I think it's very convenient to realize this ( It's basically a line of code ), And no ...

  4. Android in ListView Several common optimization methods of

    Android Medium ListView It should be one of the most common components in layout , It's also very convenient to use , The following is an introduction to ListView Several common optimization methods : First of all, let's give an example without any optimization Listview Of Adapte ...

  5. android—— Customize listView

    We all know the wechat mainframe There's an interface where you can chat line by line , That effect can be used listview To achieve ( Of course, this is just one of them ) listView It's a common component that shows the columns view, It shows the number according to the length of the data ...

  6. Share a just written android Of ListView Load classes dynamically , Full function and less code .

    ( Reprint statement from :http://www.cnblogs.com/linguanh/) brief introduction :      The ListView Realize dynamic loading data , In order to facilitate users to fully customize their own data sources . Click event , And other core operations , ...

  7. Android UI ListView Use

    One .ListView The understanding of the   1. what ListView?    One is used to display multiple slidable items (Item) Of the list ViewGroup Need to use Adapter The set data and each Item The corresponding layout is dynamically adapted to Li ...

  8. Android in Listview Click on item Constant colors and settings listselector Invalid

    Android in Listview Click on item Constant colors and settings listselector Invalid This is the same problem ,Listview Click on the item It changes color , because listview Default is set listsele ...

  9. Android in ListView Load data dynamically

    1. introduction : In order to improve the ListView Efficiency and application performance , stay Android Applications should not be loaded all at once ListView All the information to be displayed , It's a batch loading strategy , As users slide , Dynamically load the required... From the background ...

  10. Android Customize ListView

    This article talks about the implementation of a custom list Android Program , The program will implement a custom adapter (Adapter) binding   data , adopt contextView.setTag Binding data has buttons ListView.  The system displays a list (L ...

Random recommendation

  1. Asp.net Core On ( Release and deploy Linux)

    Preface As the saying goes, I don't study for three days , Can't catch up with Liu Shaoqi .Asp.net Core Update has been waiting for so long , DIDU drizzle at weekend , Stay at home and have a look Core Web App, By the way, I built a HelloWorld Taste the environment , First time to see .Ne ...

  2. views of postgresql user password and encrypted or unencrypted

    password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...

  3. php Multi process workman Server framework

    Search today php socket, Awesome. php Write socket Framework workman, Use it when you have a chance . Awesome force , It turns out that tadpole chat room was developed with this . As if a new continent had been discovered .

  4. C# Gets the extension of a file

    C# Gets the extension of a file System.IO.Path.GetExtension( " file name ");ChangeExtension   Change the extension of the path string . Combine   ...

  5. 2 Of N Power

    /** Programming, precise calculation 2 Of N Power .(N Is between 100 and 1000 Integer between )*//* Problem code :#include<stdio.h>#include<math.h>int main(){ ...

  6. ortp The use of, 1

    One : About oRTP oRTP It's an open source software , Realized RTP And RTCP agreement . Currently in use oRTP The main software of the library is linphone( Based on a IP Software for video and voice calls ). oRTP As lin ...

  7. java Interface understanding

    The main function of interface is to achieve unified access , That is to create objects with interfaces ,[ The interface name ] [ Object name ]=new [ The class that implements the interface ], In this way, you can use any kind of object new Which object , No need to change the original code , Just like yours USB Interface ...

  8. [ Face the ] nanjing SAP interview ( On )

    background Blogger 985 A small master from a weak school , Worked in a foreign enterprise in Nanjing for two years , Today's company is not bad , Treatment is OK. , It's also interesting to make things . It's in Nanjing , There are not many awesome companies. , Just talk to my relatives and friends about what I do in Nanjing IT, All will ask without exception &qu ...

  9. ARP The basics of the agreement

          About ARP The basics of the agreement 1.ARP How it works Originally, I didn't want to repeat that all over the place about ARP Basic knowledge of , But in order to keep the integrity of the article and take care of beginners , I'll just say more words , Senior readers can skip this section directly ...

  10. The mutual conversion of string and integer array (JAVA Program )

    package te; public class StringConvert { static int[] a = {0,1,1,0,1,1,0,2}; static String s = " ...