【问题标题】:Problem with populating textviews from DB rowid从 DB rowid 填充文本视图的问题
【发布时间】:2011-09-25 19:15:41
【问题描述】:

我正在尝试制作一个有 2 个视图的程序。第一个视图是从数据库填充的列表。我想要的是,当我单击列表中的一个项目时,我希望程序打开第二个视图并根据我单击的项目用数据库中的数据填充一些文本视图。我现在所做的看起来像这样:

第一个视图:

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);
}

这应该打开这个视图,但它没有:

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)));

            }
    }

谁能帮我找出问题所在?

【问题讨论】:

  • 您的数据库中有 RowID 列吗?

标签: android database sqlite list textview


【解决方案1】:

您的 onListItemClick() 应该如下所示..

    @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);
}

在下一个活动中,只需检索 ID。

还放一条日志消息来记录 ID 以确保它被传递

这里给传递的activity检索它。

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

//使用这个来加载带有游标的数据

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

在您的数据库中应该有一个 getById() 方法。像这样..

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));

只需用你的返回参数代替我的。

【讨论】:

  • ID_EXTRA 是我的 rowid 吗?我认为问题在于id的检索,你能看看我上面发布的代码吗?
  • 最好的路线是我在上面发布的。我已经编辑了。
猜你喜欢
  • 1970-01-01
  • 2015-03-30
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多