Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何在android中的其他活动中使用任何其他布局(setContentView()中定义的除外)的组件_Android_Listview_Android Activity_Layout - Fatal编程技术网

我如何在android中的其他活动中使用任何其他布局(setContentView()中定义的除外)的组件

我如何在android中的其他活动中使用任何其他布局(setContentView()中定义的除外)的组件,android,listview,android-activity,layout,Android,Listview,Android Activity,Layout,我正在开发一个应用程序,其中我需要在listview中显示一些数据,因为我已经创建了一个连接到包含listview的布局的活动,我已经为该listview创建了自定义listitem。现在我需要从活动中的自定义列表项访问组件(Imageview)。谁能告诉我如何做到这一点 这就是活动 package com.example.manishnegi.sharemyride; public class RideMatched extends Activity { int commentCount

我正在开发一个应用程序,其中我需要在listview中显示一些数据,因为我已经创建了一个连接到包含listview的布局的活动,我已经为该listview创建了自定义listitem。现在我需要从活动中的自定义列表项访问组件(Imageview)。谁能告诉我如何做到这一点

这就是活动

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);

    }

}
}
package com.example.manishnegi.sharemyride;
公共类RideMatched扩展活动{
int-commentCount=0;
private List oslist=new ArrayList();
ListView乘坐匹配的ListView;
图像视图hrate1、hrate2、hrate3、hrate4、hrate5;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、骑乘匹配);
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;
字符串arr;
JSONArray数组=新的JSONArray();
Bundle b=getIntent().getExtras();
如果(b!=null)
{
l=b.getInt(“数组长度”);
arr=b.getString(“rides_数组”);
试一试{
数组=新的JSONArray(arr);
Log.e(“字符串到数组”,array.toString());
}捕获(JSONException e){
e、 printStackTrace();
e(“字符串到数组”,e.getMessage());
}
Log.e(“匹配的数量”,l+);
}
rides\u matchedListview=(ListView)findViewById(R.id.rides\u matchedListview);
第二个scnd=新的第二个();
List detailsofrides=scnd.getallcomments(l,数组);
for(GetRidesSummaryDetails值:detailsofrides){
字符串驱动程序_imageget=values.getDriverimage();
字符串驱动程序_nameget=values.getDrivername();
字符串拾取_get=values.getPickuptime();
字符串ride_idget=values.getRideId();
字符串评级_get=values.getRating();
字符串vehicle_imageget=values.getVehicleImage();
GetRidesSummaryDetails vehi=新建GetRidesSummaryDetails();
vehi.setDriverimage(驾驶员图像获取);
vehi.setDrivername(司机姓名);
设置拾取时间(拾取时间);
vehi.setRideId(骑自行车);
vehi.设置额定值(额定值);
setRating(Integer.parseInt(rating_get));
车辆设置车辆图像(车辆图像获取);
oslist.add(vehi);
commentCount++;
rides_matchedListview.setAdapter(新的RidesMatchedAdapter(RideMatched.this,0,oslist));
新的RidesMatchedAdapter(RideMatched.this,0,oslist);
}
}
公共无效设置率(整数率)
{
如果(比率==1){
hrate1.setImageResource(R.drawable.star);
}
如果(比率==2){
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
}
如果(费率==3){
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
hrate3.setImageResource(R.drawable.star);
}
如果(比率==4){
hrate1.setImageResource(R.drawable.star);
hrate2.setImageResource(R.drawable.star);
hrate3.setImageResource(R.drawable.star);
hrate4.setImageResource(R.drawable.star);
}
如果(费率==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)

这是自定义列表项

使用任何适配器(根据需要)在ListView中为数据充气。在适配器本身中,您可以访问自定义listitem的组件。
无法访问活动中自定义listitem的组件。它们只能在适配器内部通过ListView中的数据进行访问。

我们需要将适配器传递到ListView。适配器包含要在列表中显示的模型对象列表。您可以创建扩展基本适配器的自定义适配器。您可以按照下面的教程进行操作

请注意适配器中的getView方法,并根据get view下一行中的自定义布局更改布局名称 vi=充气机充气(R.layout.tabitem,空)

创建一个holder类,其中包含列表中需要显示的所有视图项。
您不需要从“活动”中访问图像视图,因为它可以在getView方法中为每一行迭代访问。

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

您可以发布代码吗?是否要访问该子视图(列表项)当你点击它??我已经添加了所需的代码和图像,现在你能帮我吗?我会这样做,但在此之前,我需要根据数据更改评级图像。我如何更改这些图像?你想在点击它时更改评级图像,还是只想绘制它??