2013-07-02 56 views
0

我想保存一個列表並將其存儲在一個字符串中,並在另一個活動中獲取該字符串以顯示所有這些項目。當我使用字符串people它沒有問題,但當我使用字符串finalp它不起作用。 請告訴我做錯了什麼? 這個是我的MainActivity獲取項在列表中檢查到另一個活動

public class MainActivity extends ListActivity { 

String finalp; 
String[] people = { 
     "Mike Strong", 
     "Jennifer Anniston", 
     "Tom Bennet", 
     "Leander Paes", 
     "Liam Nesson", 
     "George Clooney", 
     "Barack Obama", 
     "Steve Jobs", 
     "Larry Page", 
     "Sergey Brin", 
     "Steve Wozniak" 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ListView lstView = getListView(); 
    lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    setListAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_checked, people)); 
} 

public void onListItemClick(ListView parent, View v, int position, long id){ 
    CheckedTextView item = (CheckedTextView) v; 
    if(item.isChecked()){ 
     finalp.add(position); 
    }else if(!item.isChecked()){ 
     finalp.remove(position); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch (item.getItemId()) { 
    case R.id.action_settings: 
     Intent myIntent = new Intent("com.vyprnoch.myapp1.CHECK"); 
     startActivity(myIntent); 
     break; 
    } 
    return false; 


} 

這裏就是我的CheckedNames活動

public class CheckedNames extends MainActivity{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ListView lstView = getListView(); 

    setListAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, finalp)); 

    } 

} 
+0

每次這樣的項目被點擊的時候,應該得到保存的SharedPreferences? –

回答

2

有一個ArrayList的

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

然後

public void onListItemClick(ListView parent, View v, int position, long id){ 
    CheckedTextView item = (CheckedTextView) v; 
    if(item.isChecked()){ 
     finalp.add(people[position]); 
    }else if(!item.isChecked()){ 
     finalp.remove(position); 
    } 
} 

然後

public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch (item.getItemId()) { 
    case R.id.menu_settings : 

     Intent myIntent = new Intent(this,SecondActivity.class); 
     myIntent.putExtra("key",finalp); 
     startActivity(myIntent); 
     break; 
    } 
    return false; 
} 

要收到

public class CheckedNames extends ListActivity{ 

ArrayList<String> a; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 

    Bundle extras = getIntent().getExtras(); 
    if(extras!=null) 
    { 
     a = extras.getStringArrayList("key"); 
     for(int i=0;i<a.size();i++) 
     { 
      Log.i("...........",a.get(i)); 
     } 

    }  
      ListView lstView = getListView(); 
      setListAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, a)); 
      // do whatever is needed with arraylist a 

    } 
} 

定義second.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp"> 

<ListView android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

</LinearLayout> 
+0

第二個活動是完全空白的。什麼都沒顯示 –

+0

你應該有一個該活動的佈局。 – Raghunandan

+0

檢查日誌如果數組列表傳遞給第二個活動,您應該至少在日誌中看到arraylist的內容。 – Raghunandan

相關問題