正文  UI设计 > ListView >

GridView,ListView,Gallery动态更新数据

[代码] 主类package com.android.wei.zidingyi.adapter;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;import android.widget.G......

 

[代码] 主类

package com.android.wei.zidingyi.adapter;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Gallery;

import android.widget.GridView;

import java.util.*;

public class MyActivity extends Activity {

private Gallery gallery;

private GridView gridView;

private ImageAdapter listViewAdapter;

public static ImageAdapterView imageAdapterView;

private List> listItems;

private Integer[] imgeIDs = {

R.drawable.niao,R.drawable.niao,R.drawable.rui,R.drawable.rui,

R.drawable.ning,R.drawable.ning

};

private String[] goodsNames={

"土豆丝","土豆块","土豆泥","红烧土豆",

"麻辣土豆","清蒸土豆"

};

private String[] goodsDetails={

"土豆丝:便宜又好吃", "土豆丝:便宜又好吃", "土豆丝:便宜又好吃",

"土豆丝:便宜又好吃", "土豆丝:便宜又好吃", "土豆丝:便宜又好吃",

};

private int[] money={

2,3,3,10,8,13

};

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/**全屏显示**/

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);

gridView=(GridView) this.findViewById(R.id.gridView1);

imageAdapterView = new ImageAdapterView(this);

gridView.setAdapter(imageAdapterView);

gallery = (Gallery) this.findViewById(R.id.gallery1);

listItems = getListItems();

listViewAdapter = new ImageAdapter(this,listItems);

gallery.setAdapter(listViewAdapter);

}

private List> getListItems(){

List> listItems = new ArrayList>();

for(int i=0;i

Map map = new HashMap();

map.put("image", imgeIDs[i]);

map.put("title", goodsNames[i]);

map.put("info", money[i]+"元");

listItems.add(map);

}

return listItems;

}

}

[代码] Gallery

package com.android.wei.zidingyi.adapter;

import android.content.Context;

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.TextView;

import android.widget.Toast;

import java.util.*;

public class ImageAdapter extends BaseAdapter{

private Context context;

private List> listItems;

private LayoutInflater listContainer;

private boolean[] hasChecked;

public final class ListItemView{

public ImageView image;

public TextView title;

public TextView info;

public Button add;

}

public ImageAdapter(Context context,List> listItems){

this.context = context;

listContainer = LayoutInflater.from(context);

this.listItems = listItems;

hasChecked = new boolean[getCount()];

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return listItems.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

final int selectId = position;

ListItemView listItemView = null;

if(convertView ==null){

listItemView = new ListItemView();

convertView = listContainer.inflate(R.layout.list_item, null);

//获得控件对象

listItemView.image =(ImageView) convertView.findViewById(R.id.imageitem);

listItemView.title =(TextView) convertView.findViewById(R.id.textview);

listItemView.info =(TextView) convertView.findViewById(R.id.textview1);

listItemView.add =(Button) convertView.findViewById(R.id.button1);

//设置空间集到convertView

convertView.setTag(listItemView);

}else{

listItemView = (ListItemView) convertView.getTag();

}

//设置文字图片

listItemView.image.setBackgroundResource((Integer)listItems.get(position).get("image"));

listItemView.title.setText((String)listItems.get(position).get("title"));

listItemView.info.setText((String) listItems.get(position).get("info"));

listItemView.add.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Map map = new HashMap();

map.put("image", listItems.get(selectId).get("image"));

ImageAdapterView.menuList.add(map);

MyActivity.imageAdapterView.notifyDataSetChanged();

}

});

return convertView;

}

}

[代码] GridView

package com.android.wei.zidingyi.adapter;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.Toast;

import java.util.*;

public class ImageAdapterView extends BaseAdapter{

private Context context;

private LayoutInflater layoutInflater;

public static List> menuList = new ArrayList>();

public static class ItemListView{

public ImageButton image;

}

public ImageAdapterView(Context context){

this.context = context;

layoutInflater = LayoutInflater.from(context);

}

public int getCount() {

// TODO Auto-generated method stub

return menuList.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ItemListView itemListView = null;

if(convertView==null){

itemListView = new ItemListView();

convertView = layoutInflater.inflate(R.layout.item_list1, null);

itemListView.image=(ImageButton) convertView.findViewById(R.id.menuImage);

convertView.setTag(itemListView);

}else {

itemListView=(ItemListView)convertView.getTag();

}

itemListView.image.setBackgroundResource((Integer)menuList.get(position).get("image"));

itemListView.image.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Toast.makeText(context, "触发成功", Toast.LENGTH_SHORT).show();

}

});

return convertView;

}

}