2014-12-06 98 views
-1

嗨,大家我需要帮助我有一个列表中的联系人的应用程序,我们可以选择,我想在列表视图中选择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" /> 

回答

0

既然你将它定义为一个选择题,请致电getCheckedItemPositions()在你的按钮点击收听。

这将返回一个SparseBooleanArray,它将检查的项目映射到它们在列表视图中的位置。现在只需浏览一下这张地图,就可以查看到第一个位置。

你似乎很奇怪你只想发送一个值。如果是这种情况(即您一次只需要检查一个项目),请将您的列表视图的选择模式设置为CHOICE_MODE_SINGLE,并且只需拨打getCheckedItemPosition()即可检查项目的位置。

@Override 
public void onClick(View v) { 
    Intent intent = new Intent(MainActivity.this, View_2.class); 
    SparseBooleanArray sparseArray = lv.getCheckedItemPositions(); 
    for(int i = 0; i < sparseArray.size(); i++) { 
     if(sparseArray.get(i)) { //the item at position i is checked 
      intent.putExtra("item"+i, mStrings[i]); 
     } 
    } 
    startActivity(intent); 
} 
+0

谢谢zouzou我尝试 – sidi 2014-12-06 20:51:45

+0

你能解释我更多吗? – sidi 2014-12-06 21:17:40

+0

@sidi看到我的编辑,它应该看起来像这样(未经测试) – 2014-12-06 21:22:52

1

您需要实现OnItemClickListener()与听者设置为ListView如下

ListView listView = (ListView) findViewById(R.id.listview); 
listView.setAdapter(adapter); 
listView.setOnItemClickListener(this); 

在产品按

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
    adapterView.getSelectedItem().toString(); //String on OnClick 
    // Create an Intent and send 
} 

编辑 在按钮单击

试试这个按钮点击

SparseBooleanArray checked = listview.getCheckedItemPositions();// Get Lists of all checked Items 

for (int i = 0; i < listview.getAdapter().getCount(); i++) {//Loop it to get the values 
if (checked.get(i)) { 
    // Call the Intent Here 
     Intent intent = new Intent(MainActivity.this, View_2.class); 
     intent.putExtra(yourkey,mStrings[i]); 
     startActivity(intent); 
    } 
} 
+0

OP希望发送按钮的点击中选中的值,而不是在列表视图中单击项目时。 – 2014-12-06 20:55:07

+0

如何使用它?因为当我添加你的建议我得到错误消息:( – sidi 2014-12-06 20:58:10

+0

@Zouzou哦是的。抱歉没有正确地阅读这个问题..需要删除我的答案然后 – GoCrazy 2014-12-06 20:58:48

相关问题