I'm making a android app that uses listview, I have problem where to put OnClickListener in the listview that I created in order to go to another activity but I haven't found a solution. Here is my code.
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;
}
}
and here is the xml code
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 is work properly, but what I want is when I click on menu Bantul it will go to BantulActivity as well as Wonosari and others. Can I implement my project like that? Or maybe any suggestion for my project?
add this in 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);
}
});
EDIT
First add item in res/menu/main.xml
file to show in menu i am adding settings
here
ex:
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>
then in your activity code:
//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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With