1

我正在尝试动态添加汽车列表,请参见下文,包括年份、品牌和型号等。

在此处输入图像描述

然后,当任何一辆车被点击时,它会打开一个视图。它们都应该打开相同的视图,但是将为每辆车动态加载内容。

这是我的addCar()它有点长,但是我在添加click 事件时遇到了问题。

public void addCar(String carYear, String carMake, String carModel) {
    //setup
    Context context = this;
    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, getResources().getDisplayMetrics());
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());

    //get parent layout
    LinearLayout parent = this.findViewById(R.id.parentLayout);

    //create row layout (whole row inc margin)
    LinearLayout rowLayout = new LinearLayout(context);
    LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    rowLayout.setOrientation(LinearLayout.VERTICAL);
    rowParams.setMargins(0,10,0,0);
    rowLayout.setLayoutParams(rowParams);
    //todo add click event to open incoming transfer page
    rowLayout.callOnClick(); //<-------------------

    //create car layout (just the white part)
    LinearLayout carLayout = new LinearLayout(context);
    LinearLayout.LayoutParams carLayoutParams = new LinearLayout.LayoutParams(width, height);
    carLayout.setOrientation(LinearLayout.HORIZONTAL);
    carLayoutParams.gravity = Gravity.CENTER;
    carLayoutParams.setMargins(0,10,0,0);
    carLayout.setLayoutParams(carLayoutParams);
    carLayout.setBackgroundColor(Color.LTGRAY);

    //Add to layout
    parent.addView(rowLayout);
    rowLayout.addView(carLayout);

    //set car year
    TextView tvCaryear = new TextView(context);
    tvCaryear.setText(carYear);
    carLayout.addView(tvCaryear);

    //set car make
    TextView tvCarMake = new TextView(context);
    tvCarMake.setText(carMake);
    carLayout.addView(tvCarMake);

    //set car model
    TextView tvCarModel = new TextView(context);
    tvCarModel.setText(carModel);
    carLayout.addView(tvCarModel);
}

我试图打开的视图是R.layout.activity_incoming_transfer并且类是IncomingTransfer.class

它需要一个返回 R.id 视图但不能调用 void 函数的方法,所以我不确定如何设置它以及提供哪个视图?

4

0 回答 0