簡體   English   中英

ArrayList轉到另一個活動並顯示在ListView中

[英]ArrayList to another activity and display in a ListView

我試圖在一個活動中創建一個arrayList,並在另一個活動中傳遞它,並在Listview中顯示它。 第一個活動中的第一個意圖代碼在onOptionItemSelected方法內部

else if(id == R.id.History)
    {  
Intent i=new Intent(this,History.class);
 i.putExtra("name",al);
 startActivity(i);

這是其他活動代碼

protected void onCreate(Bundle savedInstanceState)    
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ArrayList<String> str= (ArrayList<String>)getIntent().getParcelableExtra("name");

ListView lv=(ListView)findViewById(R.id.list);
 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                    this,
                    android.R.layout.simple_list_item_1,
                     str);
            lv.setAdapter(arrayAdapter);

這是我在第一個活動中添加到數組列表的方式,ArrayList是全局定義的,n是運行索引的變量。 首先將其定義為0,然后在調用addToStringList方法之前增加2

public void addToStringList(){

    EditText et=(EditText)findViewById(R.id.abcd);
    EditText et1=(EditText)findViewById(R.id.abc);
    String eam1=et.getText().toString();
    String eam2=et1.getText().toString();
    al1.add(n-2,eam1);
    al1.add(n-1,eam2);
}

每當我單擊溢出項目按鈕“ Caused by:java.lang.NullPointerException:引起”,都不會顯示任何內容。 我不知道我要去哪里

看看這篇Intent.putExtra列表,它應該有所幫助。 您需要使用intent.putStringArrayListExtra()方法代替它出現,如果您仍然得到一個空對象,請確保要傳遞的列表不是空對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM