点击列表它将打开另一个活动

时间:2014-04-23 06:59:40

标签: android android-listview

嗨在我的应用程序中我正在使用listview如果我点击列表它将转到另一个activity.its不会去另一个活动这是我的问题 java代码:

    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;

    public class Facilities extends ListActivity{

     String[] listItems={"Educational Facilities", "Medical/Health", "Food", "Dormitory", 
             "Transpotation", "Sports And Co-Circular", "Added Infrastructure", "Secutity"};
     boolean[] listImages= {true, true, true, true, true,true, true, true};

     //{true, true, true, true, true};
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fecilities);
            setListAdapter(new ImageAdapter(this, R.layout.fecilities, R.id.text1, R.id.image1, listItems, listImages ));


        }
    }

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layercontainer"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff">
   <ListView
   android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
   <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000000"   
    android:typeface="sans"/>
   <ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"/>
</RelativeLayout>

我添加一个带箭头标记的图像符号如果我点击箭头它将打开另一个活动这个我的问题可以任何人请帮助我。 感谢

7 个答案:

答案 0 :(得分:0)

在设置适配器

后写下此内容
    @Override
     public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fecilities);
            setListAdapter(new ImageAdapter(this, R.layout.fecilities, R.id.text1, R.id.image1, listItems, listImages ));
    getListView().setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
            //code specific to first list item    
            Intent myIntent = new Intent(this, NextActivity.class);
            startActivity(myIntent);      
        }
    });
}

和您的Manifest.xml

<activity  android:name=".NextActivity" />

答案 1 :(得分:0)

试试这个..

setListAdapter(new ImageAdapter(this, R.layout.fecilities, R.id.text1, R.id.image1, listItems, listImages ));
getListView().setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
           Intent intent = new Intent(Facilities.this, NextActivity.class);
           startActivity(intent);
      }
});

确保NextActivity应在清单中声明。

答案 2 :(得分:0)

  

如果我点击列表,它将转到另一个活动

要启动ListView项目上的其他活动,您需要为当前ListView添加setOnItemClickListener,然后使用onItemClick打开下一个活动:

this.getListView().setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            final int pos, long id) {
        //Open next Activity here
    }
});

答案 3 :(得分:0)

试试这个: - 将onclick监听器设置为列表视图。在里面开始其他活动,如下面

 Intent intent = new Intent(this, otherAct.class);
 startActivity(intent, 0);

另外,请不要忘记将其他活动放入清单文件中。

答案 4 :(得分:0)

getListView.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
      Intent i=new Intent(Current_Activity.this,Next_Activity.class);
      startActivity(i);'                    
 }
});

答案 5 :(得分:0)

你可以试试这个:

getListView().setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        if (pos == 0) {
            Intent intent = new Intent(getApplicationContext(), FirstAct.class);
            startActivity(intent);
        }
        if (pos == 1) {
            Intent intent = new Intent(getApplicationContext(), SecondAct.class);
            startActivity(intent);
        }
    }
});

答案 6 :(得分:0)

您需要实施onClickListener

public class Facilities extends ListActivity implements OnItemClickListener

然后实现相应的方法,它看起来像这样:

@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id )
{
    // Start that new activity with some extra info from the clicked view

    // In this case the getItem() method returns an object, but your ImageAdapter
    // class may return any type you specified, of course
    Object o = this.getListAdapter().getItem( position );

    Intent intent = new Intent(Facilities.this, SomeOtherActivity.class);
    intent.putExtra( "key", o.toString() );
    startActivity(intent);
}

当然需要倾听一些内容,所以在你的onCreate()中,你最后会提到以下内容:

getListView().setOnItemClickListener(this);

现在您的其他活动应该正常启动,如果您在那里拨打getIntent(),则可以获得您从ListActivity添加的额外信息。