简体   繁体   中英

Making a Listview editable in Android

I am making a FuelLog app keeps a log of fuel fill-ups at gas stations. I am having troubles with onItemClick to modify object attributes in an ArrayList of each item. In other words what I am trying to do is be able to click one of those fuel logs in the list and edit them.

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


     }

I'm stuck at what to write in the onItemClick function. FuelLog is an object with several attributes including: Odometer Reading, Fuel Type, Cost of Fuel, etc.

The following code is declarations for the ListView and the ArrayList that saves the FuelLogs.

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

The information is inputted through an alertDialogBuilder.

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

Here is the class 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;
}

}

Any help would be greatly appreciated!

Actually you should do it other way round. Use edittext in your listview, that way your list is already editable. However to restrict it from editing before clicking on button, you disable the editbox and onitemclick enable the editbox.

Hope this helps.

You must get the position of your arraylist data which you want to edit than put in your alertdialog or new activty like.

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

now Replace your code in alertdialog

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM