簡體   English   中英

使用ListView將數據從一個活動發送和檢索到另一個活動

[英]Sending and retrieving data from one activity to another using ListView

我正在制作一個測驗程序來練習listview的工作原理。

然后我聲明字符串。

    final String[] topics= {"History" , "Arts","Civics","Transport","Technology","Science", "Social",
            "Architecture", enter code here"Health","Geography","Computers","Business","Celebrities","Sports","Literature", "Music", };

now i use listview to see the list of items i have declared 

    Lst.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                Bundle bundle = new Bundle();
                bundle.putStringArray("Bundlekey", topics);



                String Topicname = topics[position];

                Intent intent = new Intent(QwizActivity.this,secondActivity.class);
                intent.putExtras(bundle);

                startActivity(intent);


            }

在xml中聲明listview

<ListView
        android:id="@+id/ListVwe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#388E3C"
        android:elevation="2dp"
        android:textColor="#FFFFFF" >
    </ListView>`

我進行了第二項活動,在該活動中我從第一項活動中調用了輸入並操作了我從包中獲取的輸入,但是我沒有任何包值。 它為空,例如,我想比較每個項目,例如我選擇歷史,我想對歷史有一個不同的問題,如果我選擇藝術,我想對藝術有一個問題。 但我無法從第一次活動中獲取任何值,我舉杯了,而舉杯是空的。 我使用的方法不正確。 我正在尋求一些指導,請幫助

我在其中聲明第二次活動的代碼。

TextView Qsn ;
TextView TopicTitle ;   
Button  Ybtn    ;   
Button  Nbtn    ;



@Override protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.history);

        Qsn =   (TextView) findViewById(R.id.Question);
        TopicTitle  = (TextView)findViewById(R.id.Topictxt);
        Ybtn=(Button)findViewById(R.id.Yesbtn);
        Nbtn=(Button)findViewById(R.id.Nobtn);


        Intent intent = getIntent();

        Bundle bundle =intent.getExtras();
        String check=bundle.getString("Bundlekey") ;
        TopicTitle.setText(check);
        Toast.makeText(getApplicationContext(), check, Toast.LENGTH_SHORT)  .show();        
        }

第2個活動的xml

android:id="@+id/Question"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Question"

android:id="@+id/Topictxt"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="History"

我的問題是為什么“ 檢查 ”為空,我如何將“ 檢查 ”與“ 歷史記錄 ”,“藝術”,“藝術”等值進行比較。這是我的第一個問題,如果我問的不是正確的問題,請裸露我格式。 謝謝。

您正在從OnItemClickListener中使用putStringArray來標識一個數組,並在下一個Activity中將其作為aString獲取。 而是發送主題名稱。

String Topicname = topics[position];
bundle.putString("Bundlekey", Topicname);

您也可以將“ Bundlekey”設為公共靜態變量以進行訪問

在第二個Activity中獲取數據時,必須從Bundle獲取String Array。

Bundle b=this.getIntent().getExtras();

String[] array=b.getStringArray(key);

然后從該數組獲取字符串值。

要檢查從意圖中收到了什么字符串,您可能必須使用for循環對該數組進行比較以比較其中的每個值。下面的函數將返回您收到的字符串

字符串compareValues(您的數組名稱,您收到的值){

String temp;
for(int i=0;i<array.length;i++)
{


if(value.equalsignorecase(array[i]))
temp = array[i];
break;

}
}
return temp;
}

暫無
暫無

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

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