ListView

本文提供了两个 Android 开发中使用 ListView 的示例。第一个示例展示了如何使用 ArrayAdapter 显示简单的字符串列表并处理点击事件;第二个示例则更进一步,通过自定义适配器显示包含图片和多个文本视图的复杂项。
摘要由CSDN通过智能技术生成

 

那么简单  每次都记不住。

package com.example.hejingzhou.listviewbuttondemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private String TAG = getClass().getSimpleName();
    private ListView lvButton;
    private ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvButton = (ListView)this.findViewById(R.id.listview);
        arrayList = new ArrayList<String>();
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
        lvButton.setAdapter(arrayAdapter);
        for(int line=0;line<=8;line++){
            arrayList.add("LineTable"+line);
        }
        lvButton.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, arrayList.get(position));
                Log.i(TAG, String.valueOf(arrayAdapter.getItemId(position)));//获取行号
                String LineNumber = String.valueOf(arrayAdapter.getItemId(position)+1);
                Toast.makeText(getApplicationContext(), "你点击的是:" + arrayList.get(position)+"     行号:"+LineNumber, Toast.LENGTH_SHORT).show();
            }
        });
    }

}



第二种类型

package com.example.hejingzhou.listviewimagebutton;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends ListActivity {

    private String TAG= getClass().getSimpleName();
    private List<Map<String,Object>> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mData = getData();
        MyAdapter adapter = new MyAdapter(this);
        Log.i(TAG, "onCreate创建适配器还没有出错");
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("title", "我的名片");
        map.put("twoTitle", "副标题");
        map.put("image", R.drawable.mymessage);
        list.add(map);
        Log.i(TAG, "getData()方法没有出错");

        map = new HashMap<String, Object>();
        map.put("title", "定时关机");
        map.put("twoTitle", "");
        map.put("image", R.drawable.time);
        list.add(map);
        Log.i(TAG, "getData()方法没有出错");

        map = new HashMap<String, Object>();
        map.put("title", "搜索数据");
        map.put("twoTitle", "");
        map.put("image", R.drawable.index);
        list.add(map);
        Log.i(TAG, "getData()方法没有出错");


        map = new HashMap<String, Object>();
        map.put("title", "附近景点");
        map.put("twoTitle", "");
        map.put("image", R.drawable.view);
        list.add(map);
        Log.i(TAG,"getData()方法没有出错");
        /**
         * 如果这样加载多了 就出现OOM了   ⊙﹏⊙‖∣°   ⊙﹏⊙‖∣°
         * 问题需要解决
         */
        /*map = new HashMap<String, Object>();
        map.put("title", "地图指南");
        map.put("twoTitle", "");
        map.put("image", R.drawable.guide);
        list.add(map);
        Log.i(TAG,"getData()方法没有出错");

        map = new HashMap<String, Object>();
        map.put("title", "驾车路线");
        map.put("twoTitle", "");
        map.put("image", R.drawable.cat);
        list.add(map);
        Log.i(TAG,"getData()方法没有出错");*/

        return list;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.v("我的单击监听", (String) mData.get(position).get("title"));
    }

    private void showButton() {
        new AlertDialog.Builder(this)
                .setTitle("标题")
                .setMessage("我的ListView")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
    }


    public final class ViewHolder{
        public ImageView image;
        public TextView title;
        public TextView twoTitle;
        public Button button;
    }

    public class MyAdapter extends BaseAdapter{

        private LayoutInflater inflater;

        public MyAdapter(Context context){
            this.inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();

                convertView = inflater.inflate(R.layout.activity_main,null);
                holder.image = (ImageView)convertView.findViewById(R.id.image_);
                holder.title = (TextView)convertView.findViewById(R.id.title_);
                holder.twoTitle = (TextView)convertView.findViewById(R.id.twoTitle_);
                holder.button = (Button)convertView.findViewById(R.id.button_);
                convertView.setTag(holder);
                Log.i(TAG,"关联布局没有出错");
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            Log.i(TAG, "关联布局后获取posititon值之前还没有报错");

            holder.image.setBackgroundResource((Integer) mData.get(position).get("image"));
            holder.title.setText((String) mData.get(position).get("title"));
            holder.twoTitle.setText((String)mData.get(position).get("twoTitle"));

            Log.i(TAG,"获取position值之后没有错误");
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showButton();
                }
            });
            return convertView;
        }
    }

}


源码 :http://download.csdn.net/detail/csdnhejingzhou/9456651

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值