2013-11-03 73 views
0

嗨,我目前使用自定義適配器來填充listView。 listView中的每個項目都有一個圖像的標題和背景顏色。我想要實現的是當我點擊一個項目來傳遞標題和顏色到下一個活動。 1.)這可能嗎?和2)有沒有人知道如何?android將自定義適配器的項目傳遞到意圖

她我的適配器

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列表視圖(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

首先,這是可能的。

您可以將onItemClickListener添加到您的ListView並處理對該項目的點擊。項目在列表中的位置傳遞給此方法,您可以從用於構建ListView項目的對象列表中找到原始項目。

接下來,當您創建新的Intent以啓動新的活動時,您可以將任何要發送到新活動的值添加爲此Intent的鍵值對。

在新的活動的onCreate方法,你可以使用

getIntent();

這將返回起始意圖。您將能夠使用相同的鍵檢索您傳遞給此活動的所有值。

希望這會有所幫助。

相關問題