Android - 从自定义 ArrayList 适配器中更新 SQLite table

Android - Update SQLite table from inside Custom ArrayList Adapter

背景: 我正在尝试为自定义 ListView 创建自定义 ArrayAdapter。我用来形成每一行的列表模板包含几个 TextView 列和一个 Button。 我使用 SQLite table 作为列表的来源。 问题: 我希望 Button 捕获当前时间并将其写入与 Button 出现的行对应的列中的 SQLite table [Results]。我不知道该怎么做。

这是包含 ArrayList

的 class
public class ResultsMenu extends ActionBarActivity {
private static final String LOGTAG = "Logtag: " + Thread.currentThread()
        .getStackTrace()[2].getClassName(); // log tag for records

// sql elements for selecting boats
private String where = DBAdapter.KEY_RACE_ID + " = " + GlobalContent.activeRace.getId()
        + " AND " + DBAdapter.KEY_RESULTS_VISIBLE + " = 1";
private String orderBy = DBAdapter.KEY_BOAT_CLASS + " DESC ";
//instance of data source
RaceDataSource raceDataSource;
ResultDataSource resultDataSource;

// make a listview instance
ListView myList;

// make button instance for capturing finish time
Button buttonCaptureFinishTime;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results_menu);

    //wire data source and open
    raceDataSource = new RaceDataSource(this);
    resultDataSource = new ResultDataSource(this);
    raceDataSource.open();
    resultDataSource.open();

    // wire list view
    myList = (ListView) findViewById(R.id.lvResultList);

    //wire button
    buttonCaptureFinishTime = (Button) findViewById(R.id.btn_finish_time);

    //set onclick listening for listview
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            GlobalContent.setResultsRowID(id);
            Intent intent = new Intent(view.getContext(), ResultsEditForm.class);
            startActivity(intent);
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_results_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
protected void onResume() {
    super.onResume();
    Log.i(LOGTAG, " onResume Now");
    raceDataSource.open(); // reopen the db
    resultDataSource.open(); // reopen the db
    //populateListView(); // need to build this 
}

@Override
protected void onPause() {
    super.onPause();
    Log.i(LOGTAG, " onPause NOW");
    raceDataSource.close(); // close db to reduce data leak
    resultDataSource.close(); // close db to reduce data leak
}


public void populateListView(){

}

}

ArrayAdapter 到目前为止

public class ResultsAdapter extends BaseAdapter {
Context mContext; // add context
LayoutInflater inflater; // instance of inflater

// lists of result
private ArrayList<Result> arraylist;


// instance constructor
public ResultsAdapter(Context context, ResultDataSource resultDataSource) {
    mContext = context;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<Result>();
}

/**
 * How many items are in the data set represented by this Adapter.
 *
 * @return Count of items.
 */
@Override
public int getCount() {
    return arraylist.size();
}


@Override
public Object getItem(int position) {
    return arraylist.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView( int index, View view, final ViewGroup parent) {

    if (view == null) {
        // build inflater to create a new row for each row in the Results table
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.activity_list_template_results, parent, false);
    }

    final Result result = arraylist.get(index);

    Button btn = (Button) view.findViewById(R.id.btn_finish_time); // instance of button

    // wire text views and set the associated text to them.
    TextView tv = (TextView) view.findViewById(R.id.txt_hd_results_ID);
    tv.setText(result.getResultsId() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_race_id);
    tv.setText(result.getResultsRaceId() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_boat_id);
    tv.setText(result.getResultsBoatId() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_Visible);
    tv.setText(result.getResultsVisible() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_Name);
    tv.setText(result.getBoatName() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_Class);
    tv.setText(result.getBoatClass() + "");
    tv = (TextView) view.findViewById(R.id.txt_hd_results_SailNum);
    tv.setText(result.getBoatSailNum() + "");

    // set the function of each finish button
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            LocalTime localTime = new LocalTime();// capture the current time
            // TODO: Add SQLite statement to insert local time into Results table
        }
    });

    return view;
}

}

onClick 方法中,您可以创建数据库实例,打开它并相应地更新数据。

DatabaseConnection database = new DatabaseConnection(context);
database.getWritableDatabase();