zoukankan      html  css  js  c++  java
  • Android中SimpleAdapter的使用—自定义列表

    本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试

    首先我们新建一个layout列表布局文件,具体布局可以自己设定。

    下面贴上我的自定义布局文件代码

     

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="horizontal"
     4     android:layout_width="match_parent"
     5     android:background="@drawable/list"
     6     android:layout_height="wrap_content">
     7 
     8     <ImageView
     9         android:id="@+id/ico"
    10         android:layout_width="64dp"
    11         android:layout_height="64dp"
    12         android:background="@mipmap/ic_launcher"/>
    13     <LinearLayout
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:orientation="vertical">
    17         <TextView
    18             android:id="@+id/biaoti"
    19             android:layout_width="match_parent"
    20             android:layout_height="wrap_content"
    21             android:text="我是标题"
    22             android:layout_marginTop="5dp"
    23             android:textSize="22sp"/>
    24         <TextView
    25             android:id="@+id/content"
    26             android:layout_marginTop="5dp"
    27             android:layout_width="match_parent"
    28             android:layout_height="wrap_content"
    29             android:textSize="16sp"
    30             android:text="我是项目信息"/>
    31     </LinearLayout>
    32 </LinearLayout>

     

    上面代码的效果图如下,整体用的是一个Image,以及两个TextView

     

    不好看就先凑合吧,测试用

    接下来我们开始MainActivity.java

     1 package yuntu.com.yuntu;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.AdapterView;
     7 import android.widget.ListView;
     8 import android.widget.SimpleAdapter;
     9 import android.widget.TextView;
    10 import android.widget.Toast;
    11 import java.util.ArrayList;
    12 import java.util.HashMap;
    13 import java.util.List;
    14 import java.util.Map;
    15 
    16 public class MainActivity extends AppCompatActivity {
    17     private ListView listView;
    18 //声明标题
    19     private String[] title = new String[]{
    20             "我是第1个Title", "我是第2个Title", "我是第3个Title", "我是第4个Title"
    21     };
    22 //声明内容
    23     private String[] content = new String[]{
    24             "我是第1个content", "我是第2个content", "我是第3个content", "我是第4个content"
    25     };
    26 //声明图标
    27     private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher};
    28     @Override
    29     protected void onCreate(Bundle savedInstanceState) {
    30         super.onCreate(savedInstanceState);
    31         setContentView(R.layout.activity_main);
    32         listView = (ListView) findViewById(R.id.list_item01);
    33         List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
    34         for (int i=0;i<content.length;i++){
    35             Map<String, Object> map = new HashMap<String, Object>();
    36             map.put("ico",imgIds[i]);
    37             map.put("title",title[i]);
    38             map.put("content",content[i]);
    39             listitem.add(map);
    40         }
    41         SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitem,R.layout.main_list,new String[]{"title","content","ico"},new int[]{R.id.biaoti,R.id.content,R.id.ico});
    42         listView.setAdapter(simpleAdapter);
    43 
    44         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    45             @Override
    46             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    47                 TextView bt = (TextView) view.findViewById(R.id.biaoti);
    48                 TextView nr = (TextView) view.findViewById(R.id.content);
    49                 Toast.makeText(MainActivity.this, bt.getText() + "|" + nr.getText(), Toast.LENGTH_SHORT).show();
    50             }
    51         });
    52     }
    53 }      



    //本篇文章记录日常代码,希望也可以帮到需要的人
                        ————鲨哒哒
  • 相关阅读:
    Pascal's Triangle II
    Pascal's Triangle
    Path Sum II
    Path Sum
    plusOne
    Divide Two Integers
    64. ZigZag Conversion
    63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
    62. Divide Two Integers
    61. Unique Paths && Unique Paths II
  • 原文地址:https://www.cnblogs.com/shadada/p/6903637.html
Copyright © 2011-2022 走看看