gpt4 book ai didi

android - 如何在 android 的另一个 Activity 中使用任何其他布局的组件(setContentView() 中定义的除外)

转载 作者:行者123 更新时间:2023-11-30 01:44:45 25 4
gpt4 key购买 nike

我正在开发一个应用程序,其中我需要在 ListView 中显示一些数据,因为我创建了一个连接到包含 listview 的布局的 Activity ,为此我创建了自定义列表项的 ListView 。现在我需要从 Activity 中的自定义列表项访问组件(Imageview)。谁能告诉我如何实现这一目标?

这是 Activity

package com.example.manishnegi.sharemyride;


public class RideMatched extends Activity {
int commentCount = 0;
private List<GetRidesSummaryDetails> oslist = new ArrayList<GetRidesSummaryDetails>();
ListView rides_matchedListview;
ImageView hrate1,hrate2,hrate3,hrate4,hrate5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ride_matched);
hrate1=(ImageView)findViewById(R.id.hrate1);
hrate2=(ImageView)findViewById(R.id.hrate2);
hrate3=(ImageView)findViewById(R.id.hrate3);
hrate4=(ImageView)findViewById(R.id.hrate4);
hrate5=(ImageView)findViewById(R.id.hrate5);
int l=0;
String arr;
JSONArray array=new JSONArray();
Bundle b=getIntent().getExtras();
if(b!=null)
{
l=b.getInt("array_length");
arr=b.getString("rides_array");
try {
array=new JSONArray(arr);
Log.e("String to array ",array.toString());
} catch (JSONException e) {
e.printStackTrace();
Log.e("String to array",e.getMessage());

}

Log.e("Number of matched",l+"");
}
rides_matchedListview = (ListView)findViewById(R.id.rides_matchedListview);
Second scnd = new Second();
List<GetRidesSummaryDetails> detailsofrides = scnd.getallcomments(l,array);
for (GetRidesSummaryDetails values :detailsofrides){
String driver_imageget = values.getDriverimage();
String driver_nameget = values.getDrivername();
String pickup_get = values.getPickuptime();
String ride_idget= values.getRideId();
String rating_get = values.getRating();
String vehicle_imageget = values.getVehicleImage();

GetRidesSummaryDetails vehi = new GetRidesSummaryDetails();
vehi.setDriverimage(driver_imageget);
vehi.setDrivername(driver_nameget);
vehi.setPickuptime(pickup_get);
vehi.setRideId(ride_idget);
vehi.setRating(rating_get);
setRating(Integer.parseInt(rating_get));
vehi.setVehicleImage(vehicle_imageget);
oslist.add(vehi);
commentCount++;
rides_matchedListview.setAdapter(new RidesMatchedAdapter(RideMatched.this, 0, oslist));
new RidesMatchedAdapter(RideMatched.this ,0, oslist).notifyDataSetChanged();

}
}

public void setRating(int rate)
{
if (rate == 1) {
hrate1.setImageResource(R.drawable.star);


}

if (rate == 2) {
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);

}

if (rate == 3) {
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
hrate3.setImageResource(R.drawable.star);

}

if (rate == 4) {
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
hrate3.setImageResource(R.drawable.star);
hrate4.setImageResource(R.drawable.star);

}

if (rate == 5) {
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
hrate3.setImageResource(R.drawable.star);
hrate4.setImageResource(R.drawable.star);
hrate5.setImageResource(R.drawable.star);

}

}
}

这是 ListView 布局(ride_matched.xml)

I've set this layout in my activity

这是自定义列表项

I want to imageview of this layout in my activity

最佳答案

您必须已经使用该适配器为 ListView 创建了适配器,并在 viewHolder 中声明了它应该工作的图像。

关于android - 如何在 android 的另一个 Activity 中使用任何其他布局的组件(setContentView() 中定义的除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893927/

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