Question

I'm working on an application that takes the rowid of a user selected record in a Sqlite database (shown using TableLayout), and displays the associated record on another fragment (DataEdit) for editing. This works GREAT only on the first time I open DataEdit after selecting a table row. However, if I go back to the table tab and select a different record, then go back to DataEdit tab, the selected row is not shown. Most curiously "selectedTableRecordView(v)" does run, even showing the desired rowid via a Toast message (see below code). Mapping this function to a button on the DataEdit tab and then pressing it after this second iteration causes the correct record to populate fields on this tab. What's up with this? I've tried cursor.close(), as well as database.close() with no relief. Any thoughts on what the matter could be? TIA

public class DataEdit extends Fragment implements OnClickListener {
SQLiteDatabase d, db; 
Cursor queryAll;  
EditText edRowid, edSpecies, edArea, edSampler, edCount;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
    //String szSelectedRow=DataTable.szSelectedRow;     
    View v = inflater.inflate(R.layout.dataedit, container, false);
     return v;          
}
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);

    DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);     
    db=msdb.getWritableDatabase(); 

    //Button listeners on this fragment
    Button btnSaveRecord = (Button)v.findViewById(R.id.btnSaveRecordxml);
    btnSaveRecord.setOnClickListener(this);
    Button btnUpdateRecord = (Button)v.findViewById(R.id.btnUpdateRecordxml);
    btnUpdateRecord.setOnClickListener(this);
    Button btnDeleteRecord = (Button)v.findViewById(R.id.btnDeleteRecordxml);
    btnDeleteRecord.setOnClickListener(this);   
    Button btnResetForm = (Button)v.findViewById(R.id.btnResetFormxml);
    btnResetForm.setOnClickListener(this);
    Button btnFirstRecord = (Button)v.findViewById(R.id.btnFirstRecordxml);
    btnFirstRecord.setOnClickListener(this);
    Button btnPreviousRecord = (Button)v.findViewById(R.id.btnPreviousRecordxml);
    btnPreviousRecord.setOnClickListener(this);
    Button btnNextRecord = (Button)v.findViewById(R.id.btnNextRecordxml);
    btnNextRecord.setOnClickListener(this);
    Button btnLastRecord = (Button)v.findViewById(R.id.btnLastRecordxml);
    btnLastRecord.setOnClickListener(this);


    edRowid=(EditText)v.findViewById(R.id.edRowidxml);
    edSpecies=(EditText)v.findViewById(R.id.edSpeciesxml);
    edArea=(EditText)v.findViewById(R.id.edAreaxml);
    edSampler=(EditText)v.findViewById(R.id.edSamplerxml);
    edCount=(EditText)v.findViewById(R.id.edCountxml);

    selectedTableRecord(v);
}

with selectedTableRecord(v) being,

 public void selectedTableRecord(View v){
    Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE _id ="+DataTable.szSelectedRow,null);
        c.moveToFirst();            
        edRowid.setText(c.getString(0));
        edSpecies.setText(c.getString(1));
        edArea.setText(c.getString(2)); 
        edSampler.setText(c.getString(3));
        edCount.setText(c.getString(4));
        Toast.makeText(getActivity().getApplicationContext(), "Target rowid from within selectedTableRecord is"+DataTable.szSelectedRow+".", Toast.LENGTH_LONG).show(); 
}
Was it helpful?

Solution

Inserting onResume(), and restructuring onCreateView(), and onViewCreated has solved the problem thusly:

public class DataEdit extends Fragment implements OnClickListener {
    SQLiteDatabase d, db; 
    Cursor queryAll;  
    EditText edRowid, edSpecies, edArea, edSampler, edCount;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         
        View v = inflater.inflate(R.layout.dataedit, container, false);
        return v;       
    }
    @Override
    public void onResume(){
        super.onResume();
        selectedTableRecord();
    }   
    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);     
        db=msdb.getWritableDatabase(); 

        //Button listeners on this fragment
        Button btnSaveRecord = (Button)v.findViewById(R.id.btnSaveRecordxml);
        btnSaveRecord.setOnClickListener(this);
        Button btnUpdateRecord = (Button)v.findViewById(R.id.btnUpdateRecordxml);
        btnUpdateRecord.setOnClickListener(this);
        Button btnDeleteRecord = (Button)v.findViewById(R.id.btnDeleteRecordxml);
        btnDeleteRecord.setOnClickListener(this);   
        Button btnResetForm = (Button)v.findViewById(R.id.btnResetFormxml);
        btnResetForm.setOnClickListener(this);
        Button btnFirstRecord = (Button)v.findViewById(R.id.btnFirstRecordxml);
        btnFirstRecord.setOnClickListener(this);
        Button btnPreviousRecord = (Button)v.findViewById(R.id.btnPreviousRecordxml);
        btnPreviousRecord.setOnClickListener(this);
        Button btnNextRecord = (Button)v.findViewById(R.id.btnNextRecordxml);
        btnNextRecord.setOnClickListener(this);
        Button btnLastRecord = (Button)v.findViewById(R.id.btnLastRecordxml);
        btnLastRecord.setOnClickListener(this);

        edRowid=(EditText)v.findViewById(R.id.edRowidxml);
        edSpecies=(EditText)v.findViewById(R.id.edSpeciesxml);
        edArea=(EditText)v.findViewById(R.id.edAreaxml);
        edSampler=(EditText)v.findViewById(R.id.edSamplerxml);
        edCount=(EditText)v.findViewById(R.id.edCountxml);

        selectedTableRecord();
    } 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnSaveRecordxml:
            saveRecord(v);
            break;
        /*omitted*/

        }
    }  
    private void selectedTableRecord(){
        Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE _id ="+DataTable.szSelectedRow,null);
        c.moveToFirst();////a record exists (table is not blank)                    
        edRowid.setText(c.getString(0));
        edSpecies.setText(c.getString(1));
        edArea.setText(c.getString(2)); 
        edSampler.setText(c.getString(3));
        edCount.setText(c.getString(4));
    }

The problem was related to me having EVERYTHING crammed into the onCreateView method with no onViewCreated, or onResume methods. Adding these and parsing out functions appropriately resolved this situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top