0

此问题已被修改以显示工作代码。

当他们单击 Steak 之类的项目时,我希望将该项目放入另一个列表中。如果他们随后选择了 Potatoes,那么它也应该添加到这个新列表中,这样最后我就会有一个列表,显示用户选择的所有项目。这是我到目前为止所拥有的。

午餐列表菜单.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>

它现在将项目添加到新的选定项目中。

4

2 回答 2

1

Just create an arrayList and put your items into it, then find your "other list" and feed that list with this arrayList.

 ArrayList<String> myNewList = new ArrayList<String>();

on your ItemClick:

 myNewList.add(item);

and then whenever you want to set your list:

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);
于 2012-07-20T11:45:32.770 回答
0

列个清单:

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

然后将项目添加到项目排序列表中单击:

 @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

        }
        });
于 2012-07-20T11:50:55.517 回答