【问题标题】:passing the data of the selected items to another activity - android将所选项目的数据传递给另一个活动 - android
【发布时间】:2013-06-18 11:26:01
【问题描述】:

我有一个菜单 (fishcombovaluemeals),,当用户选择超过 1 个项目时,我想要那些 项目出现在另一个活动的列表中(购物车),我已经尝试了很多,但数据
永远不会出现在购物车中! onitemclick 有什么问题!? , 我有

fishcombovaluemeal.java  
RowItem.java  
CustomListViewAdapter.java  

fishcombovaluemeals活动代码:

package com.example.newlist;

  public class FishComboValueMeals extends Activity  implements
  OnItemClickListener {
          ImageButton  Ib2;
          public static final String[] titles = new String[] {
" Fillet-O-Fish (Medium Value     Meals) ",
"Fillet-O-Fish (Large Value Meals)  ",
"Double Fillet-O-Fish (Medium     Value Meals)",
" Double Fillet-O-Fish (Large Value     Meals)  ",

};




public static final String[] descriptions = new String[] {
        "Light, flaky filet of white fish    topped with tangy tartar   ",
        "Light, flaky filet of white fish     topped with tangy tartar   ",
        "2 patties of tender fish filet over a       layer of cheese,    ",
        " 2 patties of tender fish filet over a       layer of "

};
public static final Integer[] images = { 
                           R.drawable.imfc1,
               R.drawable.imfc2,        
                           R.drawable.imfc3, 
                           R.drawable.imfc4  };



public static final Double[] prices = { 20.0, 22.73, 24.77,  27.04 };

ListView listView;
List<RowItem> rowItems;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breakfast);


    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem    (images[i], titles[i], descriptions[i],
                prices[i]);
        rowItems.add(item);
    }
    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter = new     CustomListViewAdapter(this,
            R.layout.list_item,     rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);


    Ib2 = (ImageButton) findViewById    (R.id.Button02);


    Ib2.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View arg0) {
// TODO Auto-generatedmethod stub
Intent openStartingPoint = new Intent (getApplicationContext(),ShoppingCart.class);


            startActivity (openStartingPoint);

        }
    });}    



@Override
public void onItemClick(AdapterView<?> parent, View   view, int position,
        long id) {

Intent i = new Intent(getApplicationContext(), ShoppingCart.class);

i.putExtra("EXTRA", "Item " + (position + 1) + ": " + rowItems.get(position));
    startActivity(i);
        }


}

【问题讨论】:

  • 你如何在下一个活动中检索值?
  • Intent in = this.getIntent(); CharSequence text = in.getCharSequenceExtra("EXTRA");我不知道字符序列是否正确!
  • 尝试像这样检索它:String item; Bundle data = getIntent().getExtras(); item = (String) data.get("EXTRA");。在您的 onCreate() 方法中。我认为这更准确。

标签: android android-arrayadapter custom-lists


【解决方案1】:

获取列表视图的选定项并通过意图发送值

@Override
public void onItemClick(AdapterView<?> parent, View   view, int position,
        long id) {
String value = lv1.getItemAtPosition(position).toString();
Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
i.putExtra("EXTRA", value);
    startActivity(i);
}

并在其他活动中获取价值

Bundle bundle=getIntent().getExtras();
String value=bundle.getString("EXTRA");

【讨论】:

  • 我尝试了该代码,但购物车中仍然没有任何东西,只是一个空页面
  • 感谢您的帮助
【解决方案2】:

您可以使用一个非常简单的类将您的数据存储到其中。 我警告你,这门课只是一个简单的命题,如何解决你的问题。 它有很多弱点,但它有效; ) 你可以改进它:) 只需在应用程序类中初始化它。

示例如下:

/**
 * Manager to handle passing data between Activities
 * 
 * @author Klawikowski
 * 
 */
public class ManagerBundle implements IConstants
{
    public static final String TAG = "ManagerBundle";

    private static Hashtable< Integer , Object > sDataTable;

    public static void init()
    {
        Log.i( TAG , "[ManagerBundle] Initializing ..." );
        sDataTable = new Hashtable< Integer , Object >();
    }

    public static void addBundle( int pKey , Object pObjectToPass )
    {
        Log.i( TAG , "[ManagerBundle] Adding Object to Manager using key: " + pKey );
        sDataTable.put( pKey , pObjectToPass );
    }

    public static Object getBundle( int pKey )
    {
        Log.i( TAG , "[ManagerBundle] Getting Object from Manager using key: " + pKey );
        Object lData = sDataTable.get( pKey );
        sDataTable.remove( pKey );
        return lData;
    }
}

只需在启动 Activity 之前放置一个 Table / Array 或任何其他容器,然后从第二个容器中收集它; )

【讨论】:

  • 感谢您的帮助,但说实话,我看不懂代码,我还是个初学者
  • 好吧,你所要做的就是将你选择的项目放入数组或类似的东西中。然后,您只需通过 addBundle 方法将此集合放入 ManagerBundle 中。如您所见,我们将数据保存在哈希表中 - 这是一个集合,其中数据存储在唯一键下。因此,例如,您可以制作 ManagerBundle.addBundle("MyKey","42"); .通过这个调用,您已经在键“MyKey”下存储了带有值的字符串。回到你的问题。在存储数据时,在您的情况下,在新活动“ShoppingCart”中,您只需调用 ManagerBundle.getBundle("MyKey");你必须投射你得到的对象
【解决方案3】:

您需要创建一个菜单项的单例数组。当用户执行多选时 jst 将 selectedindex 存储到一个数组中,然后将该数组传递给意图。

这里是多选列表视图的教程

http://www.vogella.com/articles/AndroidListView/article.html

这里是发送数组的代码

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

【讨论】:

    【解决方案4】:

    你可以试试这个

    @Override
    public void onItemClick(AdapterView<?> parent, View   view, int position,
            long id) {
        RowItem selItem =  rowItems.get(position);//get the selected RowItem from the list
        String[] selValues= selItem.getMethodName();//your method name in RowItem.java class
                                                    //which returns array  
        //if you have getter and setters you can access them and put them in array
        //String[] selArray = new String[size];//size = number of value you want to supply
        //String[0]= selItem.getTitle();//for title
        //String[1] = selItem.getDescription();//for description and so on
        //then send the array as parameter 
    
        // remaining is same as answered by the Jay Gajjar
    
        Bundle b=new Bundle();
        b.putStringArray("EXTRA", selArray);
        Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
        i.putExtras(b);
        startActivity(i);
    }
    

    并检索ShoppingCart 类中的值

    Bundle b=this.getIntent().getExtras();
    String[] array=b.getStringArray("EXTRA");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多