您好我正在使用自定义适配器来填充listView。 listView中的每个项目都有一个标题和背景颜色的图像。我想要实现的是当我点击一个项目将标题和bg颜色传递给下一个活动时。 1.)这可能吗? 2.)有谁知道怎么做?
她的适配器
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<MenuItemsSetup>{
Context context;
int layoutResourceId;
MenuItemsSetup data[] = null;
String color[] = null;
public CustomArrayAdapter(Context context, int layoutResourceId, MenuItemsSetup[] data, String[] color) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.color = color;
this.color = color;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
String[] backgroundColor = color;
row.setTag(holder);
}
else
{
holder = (ItemHolder)row.getTag();
}
MenuItemsSetup menuItem = data[position];
holder.txtTitle.setText(menuItem.title);
holder.imgIcon.setImageResource(menuItem.icon);
row.setBackgroundColor(Color.parseColor(color[position]));
return row;
}
static class ItemHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
继承我的MenuItemsSetup.java
public class MenuItemsSetup {
public int icon;
public String color;
public String title;
public MenuItemsSetup(){
super();
}
public MenuItemsSetup(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
继承人我在Home.java中形成listView(ps我也使用ActionBarSherlock,如果这是任何用途)
MenuItemsSetup menuData[] = new MenuItemsSetup[]
{
new MenuItemsSetup(R.drawable.ic_menu_icon1, menuItem1),
new MenuItemsSetup(R.drawable.ic_menu_icon2, menuItem2),
new MenuItemsSetup(R.drawable.ic_menu_icon3, menuItem3),
new MenuItemsSetup(R.drawable.ic_menu_icon4, menuItem4),
new MenuItemsSetup(R.drawable.ic_menu_icon5, menuItem5),
new MenuItemsSetup(R.drawable.ic_menu_icon6, menuItem6),
new MenuItemsSetup(R.drawable.ic_menu_icon7, menuItem7),
new MenuItemsSetup(R.drawable.ic_menu_icon8, menuItem8),
new MenuItemsSetup(R.drawable.ic_menu_icon9, menuItem9)
};
String[] menuBgColours = {menuColor1,menuColor2,menuColor3,menuColor4, menuColor5,.menuColor6
,menuColor7, menuColor8, menuColor9};
menuListAdapter = new CustomArrayAdapter(this, R.layout.drawer_list_item, menuData, menuBgColours);
menuGridAdapter = new CustomArrayAdapter(this, R.layout.grid_item, menuData, menuBgColours);
menuList.setAdapter(menuListAdapter);
menuList.setOnItemClickListener(new DrawerItemClickListener());
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("Homepage", "menItem clicked = " + menuData[position]);
}
}
答案 0 :(得分:0)
首先,这是可能的。
您可以将onItemClickListener添加到ListView并处理项目上的点击。列表中项目的位置将传递给此方法,您可以从用于构建ListView项目的对象列表中找到原始项目。
接下来,当您创建新的Intent以启动新活动时,您可以将要发送到新活动的任何值添加为此Intent的键值对。
在新活动的onCreate方法中,您可以使用
getIntent();
将返回起始Intent。您将能够使用相同的密钥检索传递给此活动的所有值。
希望这有帮助。