0

您好我目前正在使用自定义适配器来填充 listView。listView 中的每个项目都有一个图像、一个标题和一个背景颜色。我想要实现的是当我单击一个项目以将标题和背景颜色传递给下一个活动时。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 的地方(如果有任何用处,我也在使用 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]);

      }
   }
4

1 回答 1

0

First of all, it is possible.

You can add onItemClickListener to your ListView and handle the click on the item. The position of the item in the list is passed to this method which you can you find the original item from the list of objects which you use to build you ListView items.

Next, when you create a new Intent to start new activity, you can add whatever values you want to send to the new activity as key-value pairs to this Intent.

In the new activity's onCreate method, you can use

getIntent();

which will return the starting Intent. You will be able to retrieve all the values you passed to this activity using the same keys.

Hope this helps.

于 2013-11-03T11:43:17.467 回答