2013-02-27 3 views
0

나는 목록에서 체크 박스로 두 개의 액티비티를 가지고 있으며, 체크 박스의 각 값을 다른 액티비티로 전달하고 체크하려고한다. 왜냐하면 두 가지 활동이 함께 있지 않기 때문입니다. 내가 배열에 값을 저장하고 다른 활동에 모든하나의 액티비티에서 다른 액티비티로 체크 박스 값을 저장하는 방법

덕분에 PIN이하는 방법이 경우

, 내가 지금 일을하고 나는 그것을 게시하도록하겠습니다.

  int i = 0; 
     checkBox = new CheckBox[getItemPR.size()]; 
     Constants.INDEX_TEMP_NEW=new int[getItemPR.size()];// stoing in array[] 
     while (i < getItemPR.size()) { 

      ITEM dtItem = getItemPR.get(i); 

      TableLayout table11; 
       table11 = (TableLayout) LayoutInflater.from(this).inflate(
         R.layout.itemoverview2, null); 
       checkBox[i] = (CheckBox) table11.findViewById(R.id.checkBox1); 

       checkBox[i].setId(i); 
       checkBox[i] 
         .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

          @Override 
          public void onCheckedChanged(
            CompoundButton buttonView, 
            boolean isChecked) { 

          onSetcheckboxchanged(buttonView.getId());        
          } 

         }); 
       if (Constants.INDEX_TEMP_NEW[i] == 0) { 
        checkBox[i].setChecked(true); 

       } else { 
        checkBox[i].setChecked(false); 
       } 

public void onSetcheckboxchanged(int k) { 

     if (checkBox[k].isChecked()) {    
      Constants.INDEX_TEMP_NEW[k] = 0; 

     } else { 
      Constants.INDEX_TEMP_NEW[k] = 1; 
     }  
} 
+0

당신이 체크 박스 값을 저장하기위한 공유 환경 설정을 사용합니다 .. –

+0

@Tamilarasi Sivaraj은 내가 체크 박스를 저장해야 id가있는 값 (예 : 다른 활동에서 목록의 세부 정보가 동일한 경우) 및 확인란을 선택하면 여기도 확인해야합니다. – saran

+0

지금까지 시도한 코드를 게시 할 수 있습니까? –

답변

1

나는 보통의 값을 저장하기 위해 당신은 싱글 톤 클래스를 만들 수 있습니다 SharedPreferences

public static class Hal { 
    private static final String PREFS = "com.xxx.xxx.xxx"; 

    public static String getPreferenceString(Context ctx, String key) { 
     SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE); 
     return sharedPreferences.getString(key, ""); 
    } 

    public static void setPreferenceString(Context ctx, String key, String val) { 
     SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE); 
     Editor preferencesEditor = sharedPreferences.edit(); 
     preferencesEditor.putString(key, val); 
     preferencesEditor.commit(); 
     return; 
    } 
} 
1

을 활용, 이런 종류의 일을 위해 정적 클래스/메소드를 사용 정렬. 예. '저장소'클래스에는 배열의 getter, setter가 있습니다. 당신은 수업 시간에 여기에 데이터를 설정할 수 있습니다 활동에

public class Storage { 
private static Strorage strorageData = new Strorage(); 
public static synchronized Strorage getInstance() { 
     if (strorageData == null) { 
      strorageData = new Strorage(); 
     } 
     return strorageData ; 
    } 
    // getter & setter 

} 

당신이에서

 private Storage storageData; 
storageData = Storage .getInstance(); 

이 게터를 사용하여 데이터를 얻을 사용하여이 클래스의 개체에 액세스 할 수 있습니다.

+0

ID가있는 체크 박스 값을 다른 액티비티에 저장해야합니다. 목록의 목록과 동일한 세부 정보가있는 경우 여기에 체크 박스가 선택되어 있으면 체크해야합니다. – saran

+0

Strorage 클래스에서 id (int) 및 value (boolean)를 사용하여 2D arrayList를 만듭니다. 그것은 무엇을 묻고 있습니까? – Vineet

+0

checkBox.isChecked(); – Vineet

1

Application 클래스로 확장되는 하나의 java 클래스를 만들고 App.getInstance()을 호출하여 해당 클래스의 객체를 가져온 다음 모든 활동에서 선택한 목록 항목을 설정하거나 가져올 수 있습니다.

public class App extends android.app.Application { 
private static App app; 
private ArrayList<Integer> checkedItemList; 
private Activity activity; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    app = this; 
} 

public static App getInstance(){ 
    return app; 
} 

public void setCheckedItemList(ArrayList<Integer> checkedItemList){ 
    this.checkedItemList = checkedItemList; 
} 

public ArrayList<Integer> getCheckedItemList(){ 
    return this.checkedItemList; 
} 
} 

지금 응용 프로그램 태그 아래에 안드로이드 Manifest.xml에서 응용 프로그램 클래스를 등록

<application 
    android:name=".App" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".YourActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
+0

고마워요,하지만 그 안에 체크 박스 값을 저장하는 방법, 나는 안드로이드를 처음 사용했습니다 – saran

+0

여러 선택 항목과 함께 ListView를 사용하는 경우 선택한 항목을 가져 와서 그 목록을 해당 클래스로 설정하십시오 ... –

관련 문제