2014-02-13 38 views
0

我們想要做的是將Desserts Menu中的選中複選框的信息顯示到最終訂單列表(LOFO)活動中。如何在點擊這些複選框時在另一個活動中顯示多個複選框信息?

這裏是我們的desserts.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:background="#ffff" 
tools:context=".MainMenu" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="@string/dessertsmenu" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="27dp" 
    android:text="@string/meal_ok" 
    android:onClick="onClicdesserts_ok" /> 

<CheckBox 
    android:id="@+id/chkpeach" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/chkpie" 
    android:layout_below="@+id/chkpie" 
    android:defaultValue="false" 
    android:text="@string/peachcrumbbars" 
    android:onClick="onClickpeachcrumbbars_ok" /> 

<CheckBox 
    android:id="@+id/chksquares" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/chkpeach" 
    android:layout_below="@+id/chkpeach" 
    android:defaultValue="false" 
    android:text="@string/snickerysquares" 
    android:onClick="onClicksnicjerysquares_ok" /> 

<CheckBox 
    android:id="@+id/chkpie" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="22dp" 
    android:layout_marginTop="18dp" 
    android:defaultValue="false" 
    android:text="@string/blackberrypiebars" 
    android:onClick="onClickblackberrypiebars_ok" /> 

這裏是我們的DessertsMenu.java:

package net.xadtv.yoursmenu; 


import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View.OnClickListener; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 




public class DessertsMenu extends Activity{ 
Button button1; 
CheckBox chkpie; 
CheckBox chkpeach; 
CheckBox chksquares; 
public static final String MENU_NAME = "MyMenuFile"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.desserts); 
button1 = (Button) findViewById(R.id.button1); 
chkpie = (CheckBox) findViewById(R.id.chkpie); 
chkpeach = (CheckBox) findViewById(R.id.chkpeach); 
chksquares = (CheckBox) findViewById(R.id.chksquares); 
button1.setOnClickListener(new OnClickListener() { 

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

if(chkpie.isChecked()); 
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); 
SharedPreferences.Editor pie = settings1.edit(); 
pie.putString("pietext", "black berry pie bars"); 

pie.commit(); 

if(chkpeach.isChecked()); 
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0); 
SharedPreferences.Editor peach = settings2.edit(); 
peach.putString("peachtext", "peach crumbbars"); 

peach.commit(); 

if(chksquares.isChecked()); 
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0); 
SharedPreferences.Editor squares = settings3.edit(); 
squares.putString("squarestext", "snickery squares"); 

squares.commit(); 

Intent intent = new Intent(getApplicationContext(),LOFO.class); 
startActivity(intent); 
} 
}); 

} 

} 

這是我們希望把點擊複選框的信息,該最終訂單活動清單。

這裏是我們的lofo.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:background="#277c9b" 
tools:context=".MainMenu" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView2" 
    android:layout_below="@+id/textView2" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView3" 
    android:layout_below="@+id/textView3" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView4" 
    android:layout_below="@+id/textView4" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView5" 
    android:layout_below="@+id/textView5" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

這裏是我們的LOFO.java:

package net.xadtv.yoursmenu; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.widget.TextView; 



public class LOFO extends Activity{ 
public static final String MENU_NAME = "MyMenuFile"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setContentView(R.layout.lofo); 

TextView tvpeach = (TextView)findViewById(R.id.textView1); 
TextView tvsquares = (TextView)findViewById(R.id.textView2); 
TextView tvpie = (TextView)findViewById(R.id.textView3); 

SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); 
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0); 
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0); 

tvpeach.setText(settings1.getString("peach", "")); 
tvsquares.setText(settings2.getString("squares", "")); 
tvpie.setText(settings3.getString("pie", "")); 

} 
} 

現在的問題是,一旦我們運行它,然後選一的複選框,會發生錯誤(Source not Found)。

任何幫助,非常感謝。 :)

+0

張貼堆棧跟蹤/ logcat的輸出吧。和行號相關。 – cbrulak

回答

0

幾件事情:

  • 你已經有:如果(chkpie.isChecked());在最後看到這個分號?這意味着if語句無用
  • 您還有兩個FILE_MENU聲明。這是sl and不馴的做法。刪除一個。參考聲明中第二個文件,像這樣:DessertsMenu.FILE_MENU
  • 同樣在LOFO.java的三個聲明共享偏好

我也要去猜測,「源未找到」的錯誤,您正在討論的是一個例外,因爲您試圖在click監聽器中引用您的複選框對象。這不能這樣做。

相反試試這個:

 
button1.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); 

switch v.getId() 
{ 
    case R.id.chkpie: 
    settings1.edit().putString("pietext", "black berry pie bars").commit(); 
    case R.id.chkpeach: 
//.... you see where I'm going with this right? 
} 


Intent intent = new Intent(getApplicationContext(),LOFO.class); 
startActivity(intent); 
} 
}); 

} 


相關問題