选择的项目为什么在调用时不返回任何内容?

时间:2012-11-21 17:44:22

标签: android listview

我正在尝试创建一个Request Responses活动并获取itemSelected以使用下一个活动,该活动将它们存储到用户友好列表中。但是当单击“接受”按钮时,itemSelected不返回任何内容。(它应该在toast对话框中显示itemSelected)任何人都知道它发生了什么?另外,我想问为什么listView中的项目位于不准确的位置?对于第二项而言,它似乎更常见。如何正确调整?

下图是NotificationView.java的布局:

The result when the "Accept" button is clicked.

NotificationView.java

public class NotificationView extends ListActivity{

String[] people;
ListView lv;
Button btn1,btn2;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);  
    NotificationManager mnotis =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    mnotis.cancel(getIntent().getExtras().getInt("notificationID"));
    String i = getIntent().getExtras().getString("name");
    people = i.split("[,]");
    Log.d("how",i);

    lv= (ListView) findViewById(android.R.id.list);
    btn1 = (Button)findViewById(R.id.acceptbtn);
    btn2 = (Button)findViewById(R.id.closebtn);
    ArrayAdapter<String> list = new ArrayAdapter<String>(NotificationView.this,R.layout.friendlist_item, R.id.friend_name,people);
    lv.setAdapter(list); 

    btn1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            lv = getListView();
            int i = lv.getCount();
            Log.d("count",String.valueOf(i));
            runOnUiThread(new Runnable(){
                public void run(){
                    String itemSelected = "Selected items: \n";
                    for(int i=0;i<lv.getCount();i++){
                        if(lv.isItemChecked(i)){
                            itemSelected += lv.getItemAtPosition(i) + "\n";
                        }
                    }
                    Toast.makeText(NotificationView.this,itemSelected,Toast.LENGTH_SHORT).show();
                }               
            });

        }
    });
}}      

view.xml用

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff"
  >

<ListView android:id="@android:id/list" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_weight="1">
</ListView>

<FrameLayout android:id="@+id/FrameLayout01" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content">


  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:background="#ffffff"
      android:orientation="horizontal" >


      <Button
          android:id="@+id/acceptbtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:layout_weight="0.46"
          android:text="Accept" />



      <Button
          android:id="@+id/closebtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0.49"
          android:text="Close" />

  </LinearLayout>

</FrameLayout>

friendlist_item.xml

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

<CheckBox android:id="@+id/friend_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight = "88dip"
android:textSize="20dip"
android:textStyle="bold"/>  

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的复选框与您调用的方法无关,对于包含多项选项的列表视图,请参阅下一个指南:http://dj-android.blogspot.com/2012/04/milti-selection-listview-android-with.html