BaseAdapter适配器 添加数据 显示到ListView中

布局界面,这是一个图片加三个文本




    

        

        

        

        
    

适配器与填充的假数据

package com.example.connection_adapter;

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

import com.example.hello.R;

import android.content.Context;
import android.text.NoCopySpan.Concrete;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Connection_adapter extends BaseAdapter {
	//对话窗口适配器
	private Context context;
	private LayoutInflater inflater;
	private List> list;
              //假的数据进行填充
     	public void getData() {
		list = new ArrayList>();
		Map map1 = new HashMap();
		map1.put("IV_head", R.drawable.head);
		map1.put("NatName", "沙皮狗的忧伤");
		map1.put("TVContent", "你好啊");
		map1.put("Time", "19:11:04");
		list.add(map1);

		Map map2 = new HashMap();
		map2.put("IV_head", R.drawable.head);
		map2.put("NatName", "沙皮狗的悲伤");
		map2.put("TVContent", "我不好");
		map2.put("Time", "19:11:04");
		list.add(map2);
	}

	public Connection_adapter(Context context, List> list) {
		getData();
		this.context = context;
		inflater = LayoutInflater.from(context);
		// this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final int selectId = position;
		ViewHolder holder;
		View view;
                 //判断是ListView界面里是否已经创建View视图,优化ListView,防止重复创建         
		if (convertView == null) {
			view = inflater.inflate(R.layout.activity_connection, null);
			holder = new ViewHolder();
			holder.IV_head = (ImageView) view.findViewById(R.id.IV_head);
			holder.NatName = (TextView) view.findViewById(R.id.NatName);
			holder.TVContent = (TextView) view.findViewById(R.id.TVContent);
			holder.Time = (TextView) view.findViewById(R.id.Time);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}

		//list.get(position);
		//System.out.println("查看List中是否有数据::list.get(position)" + position);

		holder.NatName.setText((String) list.get(position).get("NatName"));
		holder.TVContent.setText((String) list.get(position).get("TVContent"));
		holder.Time.setText((String) list.get(position).get("Time"));
		holder.IV_head.setImageResource((Integer) list.get(position).get(
				"IV_head"));

		return view;
	}

	class ViewHolder {
		ImageView IV_head;
		TextView NatName, TVContent, Time;

	}

}

 

Activit界面

package com.example.connection_adapter;


import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.example.chat.Chat_Activity;
import com.example.hello.R;

//对话窗口启动
public class Conection_click extends Activity {
	private ListView connection_list;
	private Connection_adapter adapter;
	private List> list;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview_connection);

		connection_list = (ListView) findViewById(R.id.connection_list);


		System.out.println("list的大小是" + list.size());
		adapter = new Connection_adapter(Conection_click.this, list);
		connection_list.setAdapter(adapter);
                  //item中的监听器,监听到并跳转的下个界面,
		connection_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(Conection_click.this, Chat_Activity.class);
				startActivity(intent);
			}
		});

	}

}



 


 

你可能感兴趣的:(Android,应用)