gpt4 book ai didi

android - 如何在 Listview 中设置 OnClickListener 以发送到另一个 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:30 26 4
gpt4 key购买 nike

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

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 文件中添加项目以显示在菜单中我在这里添加 settings

例如:

ma​​in.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>

然后在您的 Activity 代码中:

 //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 以发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com