Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

android - Problem with populating textviews from DB rowid

I am trying to make a program that has 2 views. The first view 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...