如何在Listview中设置OnClickListener以发送到另一个活动

时间:2013-04-19 07:07:07

标签: android listview onclicklistener

我正在制作一个使用listview的Android应用程序,我有问题在我创建的listview中放置OnClickListener以便转到另一个活动,但我还没有找到解决方案。这是我的代码。

CuacaNowActivity.java

public class CuacaNowActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

    ListView listViewKab;

    super.onCreate(savedInstanceState);
    setTitle("Jogja Now");
    setContentView(R.layout.cuacanow);

    KabupatenNow weather_data[] = new KabupatenNow[]{
        new KabupatenNow(R.drawable.ic_bantul, "Bantul"),
        new KabupatenNow(R.drawable.ic_gunungkidul, "Wonosari"),
        new KabupatenNow(R.drawable.ic_kulonprogo, "Wates"),
        new KabupatenNow(R.drawable.ic_sleman, "Sleman"),
        new KabupatenNow(R.drawable.ic_yogyakarta, "Yogyakarta")
    };

    KabupatenAdapter adapter = new KabupatenAdapter(this, R.layout.listkabupaten_row, weather_data);
    listViewKab = (ListView) findViewById(R.id.listViewKab);
    View header = (View) getLayoutInflater().inflate(R.layout.listviewheader, null);
    listViewKab.addHeaderView(header);
    listViewKab.setAdapter(adapter);
}
}

KabupatenAdapter.java

public class KabupatenAdapter extends ArrayAdapter<KabupatenNow>{
Context context;
int layoutResourceId;
    KabupatenNow data[] = null;
    public KabupatenAdapter(Context context, int layoutResourceId, KabupatenNow[] data) {
        super (context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    //Viewing list Kabupaten in Yogyakarta with image in ListView
    public View getView (int position, View convertView, ViewGroup parent){
        View row = convertView;
        WeatherHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgKabupaten = (ImageView) row.findViewById(R.id.imgKabupaten);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }else{
            holder = (WeatherHolder) row.getTag();
        }

        KabupatenNow weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgKabupaten.setImageResource(weather.icon);
        return row;
    }

    static class WeatherHolder {
        ImageView imgKabupaten;
        TextView txtTitle;
    }
}

KabupatenNow

public class KabupatenNow {

        public int icon;
        public String title;
        public KabupatenNow(){
            super();
        }

        public KabupatenNow(int icon, String title) {
            super();
            this.icon = icon;
            this.title = title;
        }
}

这是xml代码

cuacanow.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:background="@drawable/ic_background"
android:orientation="vertical" >

<ListView
    android:id="@+id/listViewKab"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000" >
</ListView>

</LinearLayout>

listviewheader.xml

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

<TextView
    android:id="@+id/txtHeader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:background="#8B0000"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:text="Cuaca Kabupaten"
    android:textColor="#FFFFFF"
    android:textSize="22dp"
    android:textStyle="bold" />

</LinearLayout>

listkabupaten_row.xml

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

<ImageView
    android:id="@+id/imgKabupaten"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical" />

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:textSize="22dp"
    android:textStyle="bold" />

</LinearLayout>

listview工作正常,但我想要的是当我点击Bantul菜单时,它会转到BantulActivity以及Wonosari和其他人。我能这样实现我的项目吗?或者对我的项目有任何建议吗?

3 个答案:

答案 0 :(得分:3)

CuacaNowActivity.java

中添加此内容
listViewKab.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> a, View v, int position,
                                long id) {

                     Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);

                    startActivity(intent);
                            }
                    });

修改

首先在res/menu/main.xml文件中添加项目,以便在菜单中显示我在此处添加settings

例如:

<强> main.xml中

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_settings"/>

</menu>

然后在您的活动代码中:

 //it will show the menu
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    //it will open the new activity (Settings in my case) when selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        boolean ret;
        try {

            if (item.getItemId() == R.id.menu_settings) {
                // Handle Settings
                Intent intent = new Intent(CurrentActivity.this, Settings.class);
                startActivity(intent);

                ret = true;



        } catch (Exception e) {
        }
        ret = super.onOptionsItemSelected(item);
        return ret;

    }

答案 1 :(得分:1)

对于ListViewonClickListener无效,您需要实施OnItemClickListener

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
       // Perform operations based on the position.
}

答案 2 :(得分:0)

使用On项目单击侦听器:在CuacaNowActivity的onCreate()中尝试此操作

listViewKab.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
  Intent in=new Intent(CuacaNowActivity.this,DetailActivity.class);
  startActivity(in)           
            }
        });