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和其他人.我能这样实现我的项目吗?或者对我的项目有什么建议吗?
解决方法 在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文件中添加项目以在菜单中显示我在这里添加设置
例如:
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; }总结
以上是内存溢出为你收集整理的android – 如何在Listview中设置OnClickListener以发送到另一个活动全部内容,希望文章能够帮你解决android – 如何在Listview中设置OnClickListener以发送到另一个活动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)