其他分享
首页 > 其他分享> > 作业六

作业六

作者:互联网

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_marginTop="40dp"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <ListView
            android:id="@+id/lv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

    </ListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="30sp"
              android:orientation="horizontal" >

    <ImageView
            android:id="@+id/iv"
            android:layout_width="74dp"
            android:layout_height="64dp"
            android:src="@drawable/qq" />

    <TextView
            android:id="@+id/TV"
            android:layout_width="match_parent"
            android:layout_height="74dp"
            android:gravity="center_horizontal|bottom"
            android:text=""
            android:textSize="20sp" />

</LinearLayout>
package com.example.myapplication5;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

class MainActivity extends Activity {
    private ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        https://www.cnblogs.com/tinyphp/p/3880511.html
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) this.findViewById(R.id.lv);
        int[] im = {R.drawable.qq,R.drawable.wx,
                R.drawable.jd,R.drawable.xl,R.drawable.tm,R.drawable.ddz};
        String[] title = {"qq","微信","京东","新浪","天猫","斗地主"};

        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        // 将上述资源转化为list集合
        for (int i = 0; i < title.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", im[i]);
            map.put("title", title[i]);
            listitem.add(map);
        }

        myAdapter adapter = new myAdapter(this, listitem);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "正在下载", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
class myAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String, Object>> listitem;

    public myAdapter(Context context, List<Map<String, Object>> listitem) {
        this.context = context;
        this.listitem = listitem;
    }

    @Override
    public int getCount() {
        return listitem.size();
    }

    @Override
    public Object getItem(int position) {
        return listitem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout,null);
        }

        ImageView imageView = (ImageView) convertView.findViewById(R.id.iv);
        TextView textView = (TextView) convertView.findViewById(R.id.TV);

        Map<String, Object> map = listitem.get(position);
        imageView.setImageResource((Integer) map.get("image"));
        textView.setText(map.get("title") + "");
        return convertView;
    }
}

 

标签:map,int,作业,listitem,position,import,android
来源: https://www.cnblogs.com/liyongliang0728/p/11641070.html