嗨,大家我需要幫助我有一個列表中的聯繫人的應用程序,我們可以選擇,我想在列表視圖中選擇de值,並將其發送到名爲View_2的第二個活動中.xml我把代碼放在最後(道歉我的英語我是法語)。 我知道我必須使用PutExtra和GetExtra,但我需要使用get(position)方法獲取值,但我不知道如何。如何獲得listview值併發送其他活動
這裏是我的mainactivity.java
package com.example.messagegroupe2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
private String[] mStrings = {
"Thomas", "Audrey", "Pierre", "Paul", "Jacques",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mStrings);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
final Button okButton = (Button) findViewById(R.id.Mybutton);
okButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, View_2.class);
intent.putExtra(mStrings[position],);
startActivity(intent);
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
她是我的第一個佈局activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ue236_grpE.messagegroupe.MainActivity"
android:background="#2ECC71" >
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:orientation="horizontal"
android:scrollbarStyle="insideOverlay"
android:choiceMode="multipleChoice" />
</LinearLayout>
<Button
android:id="@+id/Mybutton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp"
android:text="@string/value_button"
android:background="#3498db"
android:textColor="#FFFFFF" />
後,我有另一種觀點,我想要獲得列表視圖的值並將其設置在TextView中的這個視圖中:
<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:background="#2ECC71"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ue236_grpE.messagegroupe.MainActivity" >
<Button
android:id="@+id/button_valid"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp"
android:layout_marginRight="10dp"
android:text="@string/valid_button"
android:background="#3498db"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/button_cancel"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="300dp"
android:layout_toRightOf="@id/button_valid"
android:text="@string/cancel_button"
android:background="#3498db"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
謝謝zouzou我嘗試 – sidi 2014-12-06 20:51:45
你能解釋我更多嗎? – sidi 2014-12-06 21:17:40
@sidi看到我的編輯,它應該看起來像這樣(未經測試) – 2014-12-06 21:22:52