从数据库rowid填充textviews的问题 [英] Problem with populating textviews from DB rowid

查看:45
本文介绍了从数据库rowid填充textviews的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

第一个视图:

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?

推荐答案

您的onListItemClick()应该看起来像这样.

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

在下一个活动中,只需获取ID.

In the next activity just retrieve the ID.

还放置一条日志消息以记录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);

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

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.

这篇关于从数据库rowid填充textviews的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆