Listview onclick如何调用另一个活动?

时间:2011-09-06 09:18:29

标签: android listview

  mListUsers = getUsers();
    lvUsers = (ListView) findViewById(R.id.lv_user);
    lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));    
    lvUsers.setClickable(true);
    lvUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
@Override
       public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
        {
            // TODO Auto-generated method stub
             Object o = lvUsers.getItemAtPosition(position);
                UserBO obj = (UserBO) o;
                Toast.makeText(Select.this, "Record Selected= "+obj.getId()+" "+obj.getName()+" "+obj.getEmail()+" "+obj.getContact(), Toast.LENGTH_LONG).show();               

                 Intent intent = new Intent(Select.this,Update.class);
                 startActivity(intent);
        }
    });

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:padding="6px"
android:orientation="vertical"
android:layout_height="67px" android:id="@+id/rlt_main"
android:background="#E0FFFF" android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:orientation="horizontal"
android:layout_height="16px" android:id="@+id/rlt_main"
android:background="#E0FFFF">

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/tv_pid"
    android:text="12" 
    android:layout_centerVertical="true" 
    android:textColor="#E0FFFF" >
 </TextView>
</LinearLayout>

select.xml

<ListView 
android:layout_height="wrap_content" 
android:id="@+id/lv_user" 
android:layout_width="fill_parent"
android:clickable="true"
>

嗨,上面的代码是onclick listview项目选择。但是当我点击listview中的项目时没有发生任何事情而不是我想调用更新活动。我的代码中的错误是什么?如果有人想看到XML的代码布局然后让我知道..

2 个答案:

答案 0 :(得分:3)

ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

          Toast.makeText(Context,"Selected item is "+ position, Toast.LENGTH_SHORT).show();    
          Intent i = new Intent(ListActivity.this, SecondActivity.class);     
          ListActivity.this.startActivity(i);

       }
  }

答案 1 :(得分:0)

嗨,我想你应该这样试试,

Intent intent = new Intent();
intent.setClassName(Select.this, <packagename>.Update.class.getName());
startActivityForResult(intent,0);