2012-07-20 151 views
0

此問題已被修改爲顯示工作代碼。從列表視圖中選擇的項目創建一個新列表

當他們點擊牛排等物品時,我希望將該物品放入另一個列表中。如果他們然後選擇土豆,那麼也應該添加到這個新列表中,以便最後我將有一個列表顯示用戶選擇的所有項目。這是我到目前爲止。

LunchListMenu.java

package com.mycompany.lunch; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class LunchListMenu extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.maincoarse); 
     final ListView lv=(ListView)findViewById(R.id.listView1);    
     ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.lunch_menu,android.R.layout.simple_list_item_1); 
     lv.setAdapter(adapter); 
     final ArrayList<String> myNewList = new ArrayList<String>(); 
     lv.setOnItemClickListener(new OnItemClickListener() { 



      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       String item=lv.getItemAtPosition(arg2).toString(); 
       String itemordered; 
       itemordered = item + " added to list"; 
       Toast.makeText(getApplicationContext(), itemordered, Toast.LENGTH_SHORT).show(); 
       myNewList.add(item);     
      } 
     }); 

       // List View Button 
       Button btnLunchList = (Button) findViewById(R.id.lrList); 
       btnLunchList.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) {      
         setContentView(R.layout.selecteditems); 
         ListView selecteditems = (ListView) findViewById(android.R.id.list); 
         ArrayAdapter<String> newadapter = new ArrayAdapter<String>(LunchListMenu.this, android.R.layout.simple_list_item_1, myNewList);    
         selecteditems.setAdapter(newadapter); 
        } 
       }); 
    } 
        public void shareMyList(View v){ 
         // Share Selected Items Button 
         Button btnShareItems = (Button) findViewById(R.id.shareMyList); 
         btnShareItems.setOnClickListener(new View.OnClickListener() { 

          @Override 
          public void onClick(View v) { 
           // TODO Auto-generated method stub 

           Intent share = new Intent(Intent.ACTION_SEND); 
           share.setType("text/plain"); 
           share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!"); 
           startActivity(Intent.createChooser(share, "Share Text")); 
          } 


         }); 
        } 
} 

我還創建了一個新的佈局叫做selecteditems.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="@drawable/main_background" 
    android:paddingLeft="10.0dip" 
    android:paddingTop="0.0dip" 
    android:paddingRight="10.0dip" 
    android:paddingBottom="10.0dip" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="100.0dip" 
      android:background="@drawable/title" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:background="@drawable/head" 
      android:layout_width="fill_parent" 
      android:layout_height="10.0dip" />  

      <ListView 
       android:id="@android:id/list" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="25dip" 
       android:textStyle="bold" 
       android:padding="10dip" 
       android:textColor="#ffffff"/> 

</LinearLayout> 

現在添加項目到新selecteditems。

回答

1

只需創建一個arrayList並將其放入其中,然後找到您的「其他列表」並使用此arrayList提供該列表。

myNewList.add(item); 

,然後每當你想設置列表:

ListView newList = (ListView) findViewById(R.id.your_new_list); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, myNewList, android.R.layout.simple_list_item_1); 
newList.setAdapter(adapter); 
+0

1件事我忘了告訴大家...我是Java/Android的新手。無論如何。我嘗試了上面的建議。我添加了最終的ArrayList myNewList = new ArrayList ();只是在最終ListView後lv =(ListView)findViewById(R.id.listView1);在onCreate。然後我將剩餘的代碼添加到我的onItemClick的末尾。我得到一個紅色下劃線的錯誤「new ArrayAdapter (this,myNewList,android.R.layout.simple_list_item_1);」 ...錯誤:構造函數ArrayAdapter (new AdapterView.OnItemClickListener(){},ArrayList ,int)未定義 – Sobo 2012-07-20 12:33:28

+0

因此,問題解決了嗎? – yahya 2012-07-20 12:34:39

+0

這是因爲你在OnItemClickListener中......你應該發送「ListMenu.this」而不是「this」。但是你不應該在那裏做這些代碼,你只需要將你的項目添加到myNewList onItemClick方法。然後,你應該找到並setAdapter您的列表,無論你想它來顯示;) – yahya 2012-07-20 12:51:59

0

以列表:

public class arbysListMenu extends ListActivity { 
    List<String> itemordered = null; // As global 
/** Called when the activity is first created. */ 
. 
. 
. 
. 

然後添加項目

ArrayList<String> myNewList = new ArrayList<String>(); 
你的項目單擊

時間順序列表點擊:

@Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

      //String item=lv.getItemAtPosition(arg2).toString(); 

      itemordered.add(lv.getItemAtPosition(arg2).toString()); 

      //// Now FILL another ListView, adapter using this itemordered list 

     } 
     }); 
+0

我很抱歉,我沒有接近知識淵博足夠知道如何完成代碼(意思是如何填充ListView),我可以看看代碼和類型,看看它做了什麼,並按照它來做足我想做的事情,但還沒有從頭開始寫任何東西。 – Sobo 2012-07-20 12:47:51

相關問題