java - 如何根据另一个数组列表从一个数组列表填充 EditText

标签 java android arraylist

我开始制作一个简单的换油应用程序,但我遇到了困难。我不确定这是否是我的逻辑或代码的错误。您将一辆汽车添加到应用程序中,其详细信息将进入数组列表,汽车将显示在主屏幕上的卡片 View 上,显示名称、品牌、型号等。当您从主屏幕上单击汽车时,它会显示在主屏幕上的卡片 View 上。打开另一个屏幕,其中包含汽车和详细信息,其中包含现有机油更换列表和用于添加新机油更换的按钮。添加机油更换后,它会创建另一个列表,其中包含机油更换的详细信息,包括汽车名称。我有它,因此无论您从主屏幕中选择哪辆车,都会显示其自己的机油更换历史记录。我无法弄清楚的问题是,我进行了一个简单的计算,以根据里程显示下一次换油的时间,我希望这个值显示在主屏幕汽车卡上。我可以将一个值传递给卡,但相同的值仍然传递给我列出的每辆车。我不知道如何将里程应得值与正确的汽车联系起来。

添加汽车

 EditText addName;
 EditText addMake;  //from add_car.xml
 EditText addModel, addMiles, addOilType, addOilQuarts, addOilFilter, addAirFilter, addPreferredChange;

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

     addName =  findViewById (R.id.txtAddName);
     addMake =  findViewById (R.id.txtAddMake);  //gets whats in txtAddMake from add_car.xml
     addModel = findViewById(R.id.txtAddModel);
     addMiles = findViewById(R.id.txtAddMiles);
     addOilType = findViewById(R.id.txtOilType);
     addOilQuarts = findViewById(R.id.txtOilQuarts);
     addOilFilter = findViewById(R.id.txtOilFilter);
     addAirFilter = findViewById(R.id.txtAirFilter);
     addPreferredChange = findViewById(R.id.txtPreferredChange);

    Button btnAddCar = findViewById(R.id.btnAddCar);
    btnAddCar.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId())
    {
        case R.id.btnAddCar:

             saveCars();

           break;
    }
}

//calling Cars.java
private void saveCars(){
    Cars cars= new Cars(addName.getText().toString(), addMake.getText().toString(),   //System.currentTimeMillis() is time of phone, using as name of file
            addModel.getText().toString(), addMiles.getText().toString(),addOilType.getText().toString(),
            addOilQuarts.getText().toString(),addOilFilter.getText().toString(),addAirFilter.getText().toString(),addPreferredChange.getText().toString());
   if( Utilities.saveCars(this,cars)){    //runs savesCars from utilises class
       Toast.makeText(this, "Car is saved", Toast.LENGTH_SHORT).show();
       finish(); //finish go back to previous actity
   }else{
       Toast.makeText(this, "error, check storage", Toast.LENGTH_SHORT).show();
   }

}

添加换油

   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_oil_change);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    addCurMiles = findViewById(R.id.addOilCurrentMiles);
    addCurDate = findViewById(R.id.addOilCurrentDate);
    addDueMiles = findViewById(R.id.addOilDueMiles);
    addDueDate = findViewById(R.id.addOilDueDate);
    addCarName = findViewById(R.id.txtCarName);  //passing the filename 
    btnAddOilChange = findViewById(R.id.btnAddOilChange);
    btnAddOilChange.setOnClickListener(this);

    btnNxtOilChange = findViewById(R.id.btnNxtOilChange);
    btnNxtOilChange.setOnClickListener(this);

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    addCurDate.setText(dateFormat.format(new Date()));

    Intent i = getIntent();
    carFileName = i.getExtras().getString("Car Name");
    addCarName.setText(carFileName);


}


@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btnAddOilChange:
            saveOilChange();
            break;

        case R.id.btnNxtOilChange:
            nextOilChange ();
            break;
    }
}

public void nextOilChange(){
   if(addCurMiles.getText().toString() != null) {
       int num1 = Integer.parseInt(addCurMiles.getText().toString());
       int sum = num1 + 3000;
       addDueMiles.setText(Integer.toString(sum));
       changeOilMiles = addDueMiles.getText().toString();
   }else{

   }
}


 private void saveOilChange(){OilChange oilChange = new 
 OilChange(System.currentTimeMillis(),addCurMiles.getText().toString(), 
 addCurDate.getText().toString(),addDueMiles.getText().toString(), 
 addDueDate.getText().toString(), addCarName.getText().toString());  
 //using System.currentTimeMillis(), for file name using .oil as extension 
    //Write next oil change to main card here!

    if( Utilities.saveOilChange(this,oilChange)){             
        setResult(RESULT_OK);
        finish(); //finish go back to previous activity
    }else{
        Toast.makeText(this, "error, check storage", Toast.LENGTH_SHORT).show();
    }

}

我在汽车卡上填写换油信息,但它适用于所有汽车

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
    txtOilChangeDue.setText(AddOilChange.changeOilMiles);
}

点击汽车时显示汽车卡并打开新 Activity

//this populates the list view
@Override
protected void onResume() {
    super.onResume();
    main_car_list.setAdapter(null);
    cars = Utilities.getAllSavedCars(this);
//for context menu
    registerForContextMenu(main_car_list);

    if(cars == null || cars.size() == 0){
        Toast.makeText(this,"you have no cars added", 
    Toast.LENGTH_SHORT).show();
        return;
    }else{

     ca = new CarAdapter(this, R.layout.car_list, cars); 
        main_car_list.setAdapter(ca);

//when click item(car) opens new activity
        main_car_list.setOnItemClickListener(new 
  AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, 
  int position, long l) { //tabbed automatically filled in this
                String fileName = 
((Cars)main_car_list.getItemAtPosition(position)).getName() + 
 Utilities.FILE_EXTENSION; //gets filename
                Intent viewCar = new 
 Intent(getApplicationContext(),CarDetails.class); //when clicked on item 
 in list opens deails
                //this is how pass data between activities
              //   this passes car name and model to CarDetails
                viewCar.putExtra("Car_File", fileName);  

                startActivity(viewCar);

            }
        });

        }

    }

**请添加您的适配器类

汽车适配器类

public class CarAdapter extends ArrayAdapter<Cars>  {

public CarAdapter(Context context, int resource, ArrayList<Cars> cars) {
    super(context, resource, cars);
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
  //  return super.getDropDownView(position, convertView, parent);

    if(convertView == null){
        convertView = LayoutInflater.from(getContext())
                .inflate(R.layout.car_list, null); //car_list xml
    }

    Cars cars = getItem(position);
    if(cars != null){
        TextView txtName = convertView.findViewById(R.id.txtName);
        TextView txtMake = convertView.findViewById(R.id.txtMake);  //from car_list reads what's there (i think)
        TextView txtModel =convertView.findViewById(R.id.txtModel);
        TextView txtMiles = convertView.findViewById(R.id.txtMiles);
        TextView txtOilChangeDue = convertView.findViewById(R.id.txtOilChangeDue);
       // TextView txtOilChangeDueMiles = convertView.findViewById(R.id.txtOilChangeDueMileage);

        txtName.setText(cars.getName());
        txtMake.setText(cars.getMake());
        txtModel.setText(cars.getModel());
        txtMiles.setText(cars.getMiles());
        txtOilChangeDue.setText(AddOilChange.changeOilMiles);  //replaces value each time and on both/all cars
        //txtOilChangeDue.setText("hi"); works
   //     txtDueMileage.setText(cars.getDueMileage());
       // txtDate.setText(cars.getDateTimeFormatted(getContext())); //not sure what for

    }

    return convertView;
}
}

油适配器类

public class OilAdapter extends ArrayAdapter <OilChange> {




public OilAdapter(Context context, int resource, List<OilChange> oilchange) {  //use ctrl+insert to add this change object to oilchange
    super(context, resource, oilchange);                            //had list changed to ArrayList (didnt seem to make a difference)
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {  //use ctrl+o to add overide choose getView
 //   return super.getView(position, convertView, parent);  //not sure why i have to comment out

    if (convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.oil_change_records,null);  //layout should (in this case) be card view list
    }                                                                                                       //this just calls list below will populate it

    OilChange oc = getItem(position); //oc - any name u want , position - gets items out of list(i think)
    if(oc != null){
        TextView cDate = convertView.findViewById(R.id.txtOilRecDate);  //gets text views from oil change records list
        TextView cMiles = convertView.findViewById(R.id.txtOilRecMiles);

        cDate.setText(oc.getCurDate());  //this sets the current date and miles from info just added for the oil change
        cMiles.setText(oc.getDueMiles());
    }
    //getting last record
    if(position==getCount()-1)
    {

    }

  return convertView;
  }


 }

最佳答案

我之前已经经历过这个,并且从上面的代码中,在 CarAdapter 中,您提供了一个常量 AddOilChange.changeOilMiles 这就是为什么值是相同的

txtOilChangeDue.setText(AddOilChange.changeOilMiles);

解决方案:

1) 在 Car 模型中创建一个额外字段

private String changeOil;
........
//create setter and getter for changeOil

然后获取当前需要更新的汽车,更新汽车的changeOil,同时保存添加换油中的oilChange

2) 将 OilChange 数组列表传递给 CarAdapter

ArrayList<OilChange> oliChanges;

public CarAdapter(Context context, int resource, ArrayList<Cars> cars, ArrayList<OilChange> oilChanges) {
    super(context, resource, cars);
     this.oilChanges = oilChanges;
}

..........

 Cars cars = getItem(position);
 OilChange oilChange = oilChanges.get(position);
    if(cars != null){
        TextView txtName = convertView.findViewById(R.id.txtName);
        TextView txtMake = convertView.findViewById(R.id.txtMake);  //from car_list reads what's there (i think)
        TextView txtModel =convertView.findViewById(R.id.txtModel);
        TextView txtMiles = convertView.findViewById(R.id.txtMiles);
        TextView txtOilChangeDue = convertView.findViewById(R.id.txtOilChangeDue);
       // TextView txtOilChangeDueMiles = convertView.findViewById(R.id.txtOilChangeDueMileage);

        txtName.setText(cars.getName());
        txtMake.setText(cars.getMake());
        txtModel.setText(cars.getModel());
        txtMiles.setText(cars.getMiles());

//obtain the oilchange from the model ensure you have created a getOilChange() method in OilChange class to obtain the oilchange
        txtOilChangeDue.setText(oilChange.getOilChange());  //replaces value each time and on both/all cars

关于java - 如何根据另一个数组列表从一个数组列表填充 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57714111/

相关文章:

java - 如何比较Java中三个数组列表的大小?

java - 在返回 Cursor 的函数中关闭 Cursor

Java JNI编程: Actual usage of the Global References

java - 如何将字符串转换为组件以读取值

android - 使用 Gradle 构建一个 fat aar,它将包含其他子模块作为 jar/aar

java - Android:在 Activity 之间传递数据 - 多个intent.putExtra不起作用?

android - 如何在 Android App 中从服务器接收 OPT

java - 数组列表删除索引

java - 多线程ArrayList迭代

java - 无法从 tomcat 项目连接 OpenShift MySql