学习安卓开发第八天【列表图实例-微信通讯录朋友列表】

package com.example.liebiaoshitu;

import androidx.appcompat.app.AppCompatActivity;

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

import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int []imageid=new int[]{
                R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d
                ,R.drawable.h,R.drawable.g,R.drawable.f,R.drawable.e
                ,R.drawable.i
        };
String []title=new String[]{
        "张三","李四","王五","赵六","孙琦","刘八","陈九","陈1","哈哈"
};
        List>listitem=new ArrayList>();
        for(int i=0;imap=new HashMap();
            map.put("image",imageid[i]);
            map.put("name",title[i]);
            listitem.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this,listitem,R.layout.main,new String[]{
                "name","image"},new int[]{R.id.title,R.id.image}
         );

        ListView listView=findViewById(R.id.listView);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Map map=(Map) parent.getItemAtPosition(position);
                Toast.makeText(MainActivity.this, map.get("name").toString(), Toast.LENGTH_LONG).show();
            }
        });
    }
}



    

    
    





    


学习安卓开发第八天【列表图实例-微信通讯录朋友列表】_第1张图片

 今天上课挺累的,忙了一天啦,所以决定,不学太多内容啦

你可能感兴趣的:(android,android-studio)