mardi 29 mars 2016

Listview Fragment missing data from SQLite Database

I am working on a diary style application that uses an SQlite Database and fragments with a listview. I have a listview that displays records from the database with selected fields. When a user clicks a listview item a fragment opens to display all the fields from that record. The problem is the fragment is only displaying the fields which are called by the listview. How can I get my fragment to display all the field data for that record? I have a field called STIME that I am trying to display in the example below. Thanks!

List.java

private DBManager dbManager;
private ListView listView;
private SimpleCursorAdapter adapter;

final String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.SLOC, DatabaseHelper.DSNM, DatabaseHelper.SDATE, DatabaseHelper.STIME};

final int[] to = new int[] { R.id.id, R.id.sloc, R.id.dsnm ,R.id.sdate, R.id.add_time};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_list);

    dbManager = new DBManager(this);
    dbManager.open();
    Cursor cursor = dbManager.fetch();

    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.empty));

    adapter = new SimpleCursorAdapter(this, R.layout.fragment_viewtrip, cursor, columns, to, 0);
    adapter.notifyDataSetChanged();
    Log.i("Listtrip", "STIMEad = " + DatabaseHelper.STIME);

    listView.setAdapter(adapter);
    //String stime = R.id.add_time;

    // OnCLickListener For List Items
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
            TextView idTextView = (TextView) view.findViewById(R.id.id);
            TextView slocTextView = (TextView) view.findViewById(R.id.sloc);
            TextView dsnmTextView = (TextView) view.findViewById(R.id.dsnm);
            TextView sdateTextView = (TextView) view.findViewById(R.id.sdate);



            String id = idTextView.getText().toString();
            String sloc = slocTextView.getText().toString();
            String dsnm = dsnmTextView.getText().toString();
            String sdate = sdateTextView.getText().toString();
            Log.i("Listtrip", "SDate = " + sdate);
            Log.i("Listtrip", "STIME = " + R.id.add_time);

            Intent modify_intent = new Intent(getApplicationContext(), UpdateTrip.class);
            modify_intent.putExtra("sloc", sloc);
            modify_intent.putExtra("dsnm", dsnm);
            modify_intent.putExtra("sdate", sdate);
            modify_intent.putExtra("id", id);

            startActivity(modify_intent);
        }
    });
}

DBmanager Fetch

    public Cursor fetch() {
    String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.SLOC, DatabaseHelper.FLOC, DatabaseHelper.DSNM, DatabaseHelper.SDATE, DatabaseHelper.STIME };
    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToLast();
    }
    return cursor;
}

Update Fragment

public class UpdateTrip extends Activity implements View.OnClickListener {

private Button modButton;
private Button rmButton;
private EditText slocEditText;
private EditText flocEditText;
private EditText dsnmEditText;
private EditText descEditText;
private Button add_time;
private Button add_date;

private long _id;

private DBManager dbManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Modify Trip Details");

    setContentView(R.layout.fragment_update);
    dbManager = new DBManager(this);
    dbManager.open();
    Cursor cursor = dbManager.fetch();

    slocEditText = (EditText) findViewById(R.id.slocadd_edittext);
    flocEditText = (EditText) findViewById(R.id.flocadd_edittext);
    dsnmEditText = (EditText) findViewById(R.id.dsnmadd_edittext);
    descEditText = (EditText) findViewById(R.id.descadd_edittext);
    add_time = (Button) findViewById(R.id.add_time);
    add_date = (Button) findViewById(R.id.add_date);

    modButton = (Button) findViewById(R.id.mod_button);
    rmButton = (Button) findViewById(R.id.rm_button);

    //String stime = getItem();

    Intent intent = getIntent();
    String id = intent.getStringExtra("id");
    String sloc = intent.getStringExtra("sloc");
    String floc = intent.getStringExtra("floc");
    String desc = intent.getStringExtra("desc");
    String dsnm = intent.getStringExtra("dsnm");
    String stime = intent.getStringExtra("stime");
    String sdate = intent.getStringExtra("sdate");

    _id = Long.parseLong(id);

    slocEditText.setText(sloc);
    flocEditText.setText(floc);
    dsnmEditText.setText(dsnm);
    descEditText.setText(desc);
    add_time.setText(stime);
    add_date.setText(sdate);

    modButton.setOnClickListener(this);
    rmButton.setOnClickListener(this);
}

Aucun commentaire:

Enregistrer un commentaire