开发者

Problem with populating textviews from DB rowid

I am trying to make a program that has 2 views. The first vi开发者_如何学JAVAew is a list populated from a database. What i want is, when i click an item of the list then i want the program to open the secound view and fill some textviews with data from the database according to the item i clicked. What i have done for now is looking like this:

The first view:

public class MainActivity extends ListActivity {


private static final int ACTIVITY_VIEW = 1;
private DbAdapter mDbHelper;


@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);   
      setContentView(R.layout.main);

      mDbHelper = new DbAdapter(this);
      mDbHelper.open();
      fillData();      

}

private void fillData() 
{
     Cursor contactCursor = mDbHelper.fetchAllContact();
     startManagingCursor(contactCursor);

     String[] from = new String[]{DbAdapter.KEY_FIRST};

     int[] to = new int[]{R.id.contactlist};

     SimpleCursorAdapter contactsfirst = new SimpleCursorAdapter(this, R.layout.list, contactCursor, from, to);

     setListAdapter(contactsfirst);


}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, PersonPage.class);
    i.putExtra(DbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_VIEW);
}

That should open this view, but it doesent:

public class PersonPage extends Activity
{   

private EditText mFirstText;
private EditText mLastText;
private EditText mPhoneText;
private EditText mMPhoneText;
private EditText mRoomText;
private EditText mInitialText;
private Button mBackButton;

private Long mRowId;

String back = new String("Back");
String na = new String("N/A");

private DbAdapter mDbHelper;


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

            mDbHelper = new DbAdapter(this);
            mDbHelper.open();

            setContentView(R.layout.personpage);

            mFirstText = (EditText) findViewById(R.id.first);
            mLastText = (EditText) findViewById(R.id.last);
            mPhoneText = (EditText) findViewById(R.id.phone);
            mMPhoneText = (EditText) findViewById(R.id.mphone);
            mRoomText = (EditText) findViewById(R.id.room);
            mInitialText = (EditText) findViewById(R.id.initial);
            mBackButton = (Button) findViewById(R.id.back);

            mRowId = (savedInstanceState == null) ? null :
                (Long) savedInstanceState.getSerializable(DbAdapter.KEY_ROWID);
            if (mRowId == null) {
                Bundle extras = getIntent().getExtras();
                mRowId = extras != null ? extras.getLong(DbAdapter.KEY_ROWID)
                                        : null;


            populateFields();

            mBackButton.setOnClickListener(new TextView.OnClickListener() {
                public void onClick(View arg0) {
                    Intent i = new Intent(PersonPage.this, MainActivity.class);
                    PersonPage.this.startActivity(i);
                }
            });

            }           
    }


    private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchContact(mRowId);
            startManagingCursor(note);
            mFirstText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_FIRST)));
            mLastText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_LAST)));
            mPhoneText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_PHONE)));
            mMPhoneText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_MPHONE)));
            mRoomText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_ROOM)));
            mInitialText.setText(note.getString(
                    note.getColumnIndexOrThrow(DbAdapter.KEY_INITIAL)));

            }
    }

Can anyone of you help me find the problem?


Your onListItemClick() should look like this..

    @Override
public void onListItemClick(ListView list, View view, int position, long id){
    Intent i = new Intent(meeting_list.this, ACTIVITY.class);


    i.putExtra(ID_EXTRA, String.valueOf(id));


    startActivity(i);
}

In the next activity just retrieve the ID.

Also put a log message to log the ID to insure it is being passed

Here to the passing activity retrieve it.

ID = getIntent().getStringExtra(ACTIVITY.ID_EXTRA);

//USe this to load the data with a cursor

public void load(){
    Cursor c = helper.getByID(meetingId);

There should be a method in your database to getById(). Like this..

public Cursor getByID(String id){
    String [] args={id};

    return(getReadableDatabase().rawQuery("SELECT _id, meetingTitle, meetingDescrip, meetingAdress, meetingDateTime, lat, lon, type FROM meetings WHERE _ID=?", args));

Just substitute your return arg's for mine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜