首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将选定项的数据传递给另一个活动- android

问将选定项的数据传递给另一个活动- android
EN

Stack Overflow用户
提问于 2013-06-18 11:26:01
回答 4查看 4.7K关注 0票数 1

我有一个菜单(fishcombovaluemeals),,当用户选择超过一个项目时,我希望这些项目出现在另一个活动(购物车)的列表中,我尝试了很多,但是数据。

代码语言:javascript
复制
 never appears in the shopping cart ! what is wrong in the `onitemclick` !? ,      I have 
代码语言:javascript
复制
fishcombovaluemeal.java  
RowItem.java  
CustomListViewAdapter.java  

渔业综合评估活动代码:

代码语言:javascript
复制
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);
        }


}
EN

回答 4

Stack Overflow用户

发布于 2013-06-18 11:41:13

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

代码语言:javascript
复制
@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);
}

并在其他活动中获取值。

代码语言:javascript
复制
Bundle bundle=getIntent().getExtras();
String value=bundle.getString("EXTRA");
票数 1
EN

Stack Overflow用户

发布于 2013-06-18 11:34:35

您可以使用一个非常简单的类来存储数据。我警告您,这个类只是一个简单的命题,如何解决您的问题。它有许多缺点,但它有效;)您可以改进它:)只需在应用程序类中插入它。

下面是一个例子:

代码语言:javascript
复制
/**
 * 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;
    }
}

只需在开始活动之前简单地放置一个表/数组或任何其他容器,并从第二个容器中收集;)

票数 0
EN

Stack Overflow用户

发布于 2013-06-18 11:34:38

你可以试试这个

代码语言:javascript
复制
@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类中的值。

代码语言:javascript
复制
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray("EXTRA");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17167674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档