使Listview可在Android中编辑

时间:2016-01-31 03:58:48

标签: java android listview

我正在制作一个FuelLog应用程序,记录加油站的燃料填充情况。我在onItemClick上遇到麻烦,要修改每个项目的ArrayList中的对象属性。换句话说,我想要做的就是能够点击列表中的其中一个燃料日志并进行编辑。

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {


     }

我坚持在onItemClick函数中写什么。 FuelLog是一个具有多种属性的对象,包括:里程表读数,燃料类型,燃料成本等。

以下代码是ListView和ArrayList的声明,用于保存FuelLogs。

private Button button;
private ListView oldTweetsList;
private static final String FILENAME = "FuelTracker.sav";
private ArrayList<FuelLog> FuelLogs = new ArrayList<FuelLog>();
ArrayAdapter<FuelLog> adapter;

信息通过alertDialogBu​​ilder输入。

            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // get user input and set it to result
                            //editTextMainScreen.setText(input.getText());
                            setResult(RESULT_OK);
                            String station = input.getText().toString();
                            String odometer = input1.getText().toString();
                            String fuelGrade = input2.getText().toString();
                            String fuelAmount = input3.getText().toString();
                            String fuelUnitCost = input4.getText().toString();
                            String fuelCost = input5.getText().toString();
                            String date = input6.getText().toString(); //Date

                            FuelLog log = new FuelLog(date, station, odometer, fuelGrade, fuelAmount, fuelUnitCost, fuelCost);

                            FuelLogs.add(log);
                            adapter.notifyDataSetChanged();

                            saveInFile();
                            finish();
                        }
                    })

这是FuelLog类。

public class FuelLog {

public String date;
public String station;
public String odometer;
public String fuelGrade;
public String fuelAmount;
public String fuelUnitCost;
public String fuelCost;

public FuelLog (String date, String station, String odometer, String fuelGrade, String fuelAmount, String fuelUnitCost, String fuelCost) {
    this.date = date;
    this.station = station;
    this.odometer = odometer;
    this.fuelGrade = fuelGrade;
    this.fuelAmount = fuelAmount;
    this.fuelUnitCost = fuelUnitCost;
    this.fuelCost = fuelCost;

}

@Override
public String toString(){
    //return date.toString() + " | " + message;
    return "Date: " + date + "\nStation: " + station + "\nOdometer: " + odometer + "\nFuel Grade: " + fuelGrade + "\nFuel Amount: " + fuelAmount + "\nFuel Unit Cost: " + fuelUnitCost + "\nFuel Cost: " + fuelCost;
}

}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

其实你应该以其他方式做到这一点。在列表视图中使用edittext,这样您的列表就已经可以编辑了。但是,要在单击按钮之前限制编辑,请禁用编辑框,然后启用编辑框。

希望这会有所帮助。

答案 1 :(得分:0)

您必须获取要编辑的arraylist数据的位置,而不是放入alertdialog或新的活动中。

int arrayListposition;
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        arrayListposition = position;
}

现在替换alertdialog中的代码

FuelLog log = new FuelLog(date, station, odometer, fuelGrade, fuelAmount, fuelUnitCost, fuelCost);
FuelLogs.add(arrayListposition,log);
adapter.notifyDataSetChanged();