此問題已被修改爲顯示工作代碼。從列表視圖中選擇的項目創建一個新列表
當他們點擊牛排等物品時,我希望將該物品放入另一個列表中。如果他們然後選擇土豆,那麼也應該添加到這個新列表中,以便最後我將有一個列表顯示用戶選擇的所有項目。這是我到目前爲止。
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件事我忘了告訴大家...我是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
因此,問題解決了嗎? – yahya 2012-07-20 12:34:39
這是因爲你在OnItemClickListener中......你應該發送「ListMenu.this」而不是「this」。但是你不應該在那裏做這些代碼,你只需要將你的項目添加到myNewList onItemClick方法。然後,你應該找到並setAdapter您的列表,無論你想它來顯示;) – yahya 2012-07-20 12:51:59