gpt4 book ai didi

android - 处理没有任何java文件的布局资源文件的点击

转载 作者:行者123 更新时间:2023-11-29 19:03:58 25 4
gpt4 key购买 nike

我正在使用 google places api json 在 ListView 中检索附近的酒店,我已经成功地做到了,但我也想点击 ListView itms 中的按钮转到谷歌地图。作为 ListView 项目从没有任何 java 代码的布局资源文件中显示,我应该如何处理该布局资源文件中按钮的 onclick 事件?我试图单独创建一个 Activity 来处理该布局资源文件的事件,但它不起作用,它显示->

而我的布局资源文件是->

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


<LinearLayout

android:padding="10dp"
android:id="@+id/linlayout"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageView
android:id="@+id/map"
android:src="@drawable/map"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="right"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value"
android:textSize="20sp"
android:id="@+id/nm"/>
</LinearLayout>
</LinearLayout>

和onclick habdler java代码是->

public class ListItem extends AppCompatActivity {
Double lat=22.2254,lang=53.2354;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);

LayoutInflater layoutInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View vw=layoutInflater.inflate(R.layout.list_item,null);
final Context context=vw.getContext();

ImageView imgv=(ImageView)findViewById(R.id.map);
imgv.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
Log.i("ListItem","This Onclick worked ok");
Intent intent=new Intent(context,MapView.class);
intent.putExtra("lat",lat);
intent.putExtra("lang",lang);
startActivity(intent);
}
});
}

}

它不工作..请帮我找到一种方法,这样如果我单击 ID 为“map”的 ImageView ,它就会带我去另一个 Activity !!

这是 ListView 适配器->

public class hotelAdapter extends ArrayAdapter<Hotels> {
public hotelAdapter(@NonNull Context context, ArrayList<Hotels> hotelsList) {
super(context,0,hotelsList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Hotels hotels=getItem(position);
Context context;

if(convertView==null){
convertView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);

}

// context=convertView.getContext();

TextView namevw=convertView.findViewById(R.id.nm);
TextView rtingvw=convertView.findViewById(R.id.rtng);
TextView adrsvw=convertView.findViewById(R.id.adrs);

namevw.setText(hotels.getName());
rtingvw.setText(hotels.getRating());
adrsvw.setText(hotels.getVicinity());

return convertView;
// return super.getView(position, convertView, parent);
}
}

和MainActivity.java的OnCreate->

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView=(ListView)findViewById(R.id.lvw);

Intent intent=getIntent();



LayoutInflater layoutInflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View vw=layoutInflater.inflate(R.layout.list_item,null);
ImageView imgv=(ImageView) vw.findViewById(R.id.map);

imgv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("ListItem","This Onclick worked ok");
Toast.makeText(getApplicationContext(),"tHIS WORKED YEAH!",Toast.LENGTH_SHORT).show();
}
});

}

最佳答案

你好,你可以用这样简单的方法解决你的问题..

主 Activity

    hotelAdapter hotel;
ArrayList<Hotels> hotelsList;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hotelsList=new ArrayList<>();
listView=(ListView)findViewById(R.id.lvw);
hotel = new hotelAdapter(MainActivity.this, hotelsList);
listView.setAdapter(hotel);

}

适配器类

      public class hotelAdapter extends ArrayAdapter<Hotels> {

Double lat = 22.2254, lang = 53.2354;
Context context=null;

public hotelAdapter(Context context, ArrayList<Hotels> hotelsList) {
super(context, 0, hotelsList);
}

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
Hotels hotels = getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}

context=convertView.getContext(); //ReEdited to make the code work
TextView namevw = (TextView) convertView.findViewById(R.id.nm);
TextView rtingvw =(TextView) convertView.findViewById(R.id.rtng);
TextView adrsvw =(TextView) convertView.findViewById(R.id.adrs);
ImageView map = (ImageView) convertView.findViewById(R.id.map); // edited
namevw.setText(hotels.getName());
rtingvw.setText(hotels.getRating());
adrsvw.setText(hotels.getVicinity());

map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MapView.class);
intent.putExtra("lat", lat);
intent.putExtra("lang", lang);
context.startActivity(intent);
}
});
return convertView;
// return super.getView(position, convertView, parent);
}
}

关于android - 处理没有任何java文件的布局资源文件的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899194/

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