我正在制作一个使用 listview 的 android 应用程序,我有问题将 OnClickListener 放在我创建的 listview 中以便转到另一个活动,但我还没有找到解决方案。这是我的代码。
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;
}
}
卡布帕滕现在
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代码
cuacano.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 和其他人。我可以这样实施我的项目吗?或者对我的项目有什么建议?