ListView里面数据加载不全

刁俊 发布于 2014/03/20 14:39
阅读 1K+
收藏 0
hashmap里面明明是3对数据,显示出来只有一对 package com.example.which; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class ClothActivity extends Activity { private List> mData; public ListView list1; public int actiondownX,actionmoveX,actiondownY,actionmoveY; public ImageButton addbutton; public EditText editText; public String imessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cloth); mData = getData(); MyAdapter adapter = new MyAdapter(this); list1=(ListView)findViewById(R.id.clothlistview) ; list1.setAdapter(adapter); addbutton=(ImageButton)findViewById(R.id.addcloth); editText=(EditText)findViewById(R.id.edittextcloth); addbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 imessage=editText.getText().toString(); setTitle(imessage); } }); } //getData private List> getData() { List> list = new ArrayList>(); Map map = new HashMap(); map.put("title", "G1"); map.put("img", R.drawable.button_delete); list.add(map); map = new HashMap(); map.put("title", "G2"); map.put("img", R.drawable.button_delete); list.add(map); map = new HashMap(); map.put("title", "G3"); map.put("img", R.drawable.button_delete); list.add(map); return list; } //显示 public final class ViewHolder{ public ImageView img; public TextView title; } public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; public MyAdapter(Context context){ this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.listitem, null); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.title = (TextView)convertView.findViewById(R.id.title); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)mData.get(position).get("img")); holder.title.setText((String)mData.get(position).get("title")); return convertView; } //点击添加、删除 /* class ClickEvent implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()) { case R.id.addcloth: imessage=editText.getText().toString(); setTitle(imessage); Toast.makeText(ClothActivity.this,"1", Toast.LENGTH_LONG).show(); break; } } } */ //滑动退出 } public boolean onTouchEvent(MotionEvent event){ int action=event.getAction(); switch (action) { case (MotionEvent.ACTION_DOWN): actiondownX=(int)event.getX(); Toast.makeText(ClothActivity.this,"1", Toast.LENGTH_LONG).show(); break; case (MotionEvent.ACTION_MOVE): actionmoveX=(int)event.getX(); break; case (MotionEvent.ACTION_UP): if((actiondownX>10*actionmoveX)&&((actiondownY-actionmoveY)<100)) { Intent intent = new Intent(); intent.setClass( ClothActivity.this,MainActivity.class); startActivity(intent); ClothActivity.this.finish(); } break; } return true; } }
加载中
0
刁俊
刁俊
package com.example.which;

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

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ClothActivity extends Activity {
     
	private List<Map<String, Object>> mData;
	public ListView list1;
	
	
	public int actiondownX,actionmoveX,actiondownY,actionmoveY;
	public ImageButton addbutton;
	public EditText editText;
	public String imessage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cloth);
		
		mData = getData();
		MyAdapter adapter = new MyAdapter(this);
		list1=(ListView)findViewById(R.id.clothlistview) ;
		list1.setAdapter(adapter);
		
		addbutton=(ImageButton)findViewById(R.id.addcloth);
		editText=(EditText)findViewById(R.id.edittextcloth);
		
		addbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				imessage=editText.getText().toString();
				setTitle(imessage);
				
				
			}
		});
		
	}
	
	//getData
	
	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", "G1");
		map.put("img", R.drawable.button_delete);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("title", "G2");
		map.put("img", R.drawable.button_delete);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("title", "G3");
		map.put("img", R.drawable.button_delete);
		list.add(map);
		
		return list;
	}
	
	//显示
	
	
	
	

	public final class ViewHolder{
		public ImageView img;
		public TextView title;
		
	}
	
	
	public class MyAdapter extends BaseAdapter{

		private LayoutInflater mInflater;
		
		
		public MyAdapter(Context context){
			this.mInflater = LayoutInflater.from(context);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ViewHolder holder = null;
			if (convertView == null) {
				
				holder=new ViewHolder();  
				
				convertView = mInflater.inflate(R.layout.listitem, null);
				holder.img = (ImageView)convertView.findViewById(R.id.img);
				holder.title = (TextView)convertView.findViewById(R.id.title);
				
				convertView.setTag(holder);
				
			}else {
				
				holder = (ViewHolder)convertView.getTag();
			}
			
			
			holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
			holder.title.setText((String)mData.get(position).get("title"));
			
			
			
			
			return convertView;
		}
	
	//点击添加、删除
/*
		class ClickEvent implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.addcloth:
				imessage=editText.getText().toString();
				setTitle(imessage);
				Toast.makeText(ClothActivity.this,"1", Toast.LENGTH_LONG).show();
				break;

			}
			
		}
	
	}
	*/
	//滑动退出


	}
	
	public boolean onTouchEvent(MotionEvent event){
		int action=event.getAction();
		switch (action) {
		case (MotionEvent.ACTION_DOWN):
			actiondownX=(int)event.getX();
		    Toast.makeText(ClothActivity.this,"1", Toast.LENGTH_LONG).show();
			break;
		case (MotionEvent.ACTION_MOVE):
			actionmoveX=(int)event.getX();
			break;
		case (MotionEvent.ACTION_UP):

			if((actiondownX>10*actionmoveX)&&((actiondownY-actionmoveY)<100))
			{

				Intent intent = new Intent();  
				intent.setClass( ClothActivity.this,MainActivity.class); 
				startActivity(intent);
				ClothActivity.this.finish();

			}
			break;		
		
		}
		
		return true;
		
	}	
}



0
刁俊
刁俊

上面的看不清楚,效果图


返回顶部
顶部