0

我正在开发用户输入他朋友的个人详细信息并保存到 SQLite 数据库中的应用程序。然后用户可以在另一个页面(活动)中查看他的朋友的信息。但是当用户需要查看他朋友的信息时,它不会显示在第二个活动中。需要从数据库表中检索信息。我怎么做?

这是代码-
第一个活动-

public class PersonalInformation extends Activity
{
    EditText txtLikes, txtDislikes, txtType, txtDate;
    Button btnView, btnBack;
    Spinner nameSpinner;    

    ArrayAdapter<String> adapter;
    String friends[] = {"Kanak Priya", "Joanne Liew", "Melissa Haiting", "Michelle Lam", "Teo Kin Hua", "David Yeo", "Nur Ashiqin", "Stephanie"};

    final Context context = this;

    private int namesSpinnerId;        

    LikesDBAdapter likeDB = new LikesDBAdapter(this);
    DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this);


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);

        nameSpinner = (Spinner) findViewById(R.id.nameSpinner);

        adapter = new ArrayAdapter<String>(this,
                R.layout.spinner_text, friends);
        nameSpinner.setAdapter(adapter);

        /*BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
    //  buddyDB.open();

        Cursor friendsCursor = buddyDB.getAllNames();
        startManagingCursor(friendsCursor); 

        String[] from = new String[]{BuddyDBAdapter.KEY_NAME};
        int[] to = new int[]{R.id.name};

        SimpleCursorAdapter friendsCursorAdapter = new SimpleCursorAdapter(this, R.layout.spinner_text, friendsCursor, from, to);
        friendsCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        nameSpinner = (Spinner)findViewById(R.id.nameSpinner);
        nameSpinner.setAdapter(friendsCursorAdapter);
        //buddyDB.close();


        nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
            {
                 @Override
                 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                 {
                     Cursor c = (Cursor)parent.getItemAtPosition(pos);
                     namesSpinnerId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
                 }

                @Override
                public void onNothingSelected(AdapterView<?> parent)
                {
                    // TODO Auto-generated method stub

                }
            });
        //buddyDB.close();
*/      
        Button btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    likeDB.open();
                    long like_id;

                    nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                    String NameValue = nameSpinner.getSelectedItem().toString();

                    txtLikes = (EditText) findViewById(R.id.txtLikes);
                    String LikeValue = txtLikes.getText().toString();



                    like_id = likeDB.insertLikes(LikeValue, NameValue);
                    likeDB.close();

                    dlikeDB.open();
                    long dlike_id;

                    nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                    String NamesValue = nameSpinner.getSelectedItem().toString();

                    txtDislikes = (EditText) findViewById(R.id.txtDislikes);
                    String DislikeValue = txtDislikes.getText().toString();

                    dlike_id = dlikeDB.insertDislikes(DislikeValue, NamesValue);
                    dlikeDB.close();

                    txtDate = (EditText) findViewById(R.id.txtDate);
                    txtType = (EditText) findViewById(R.id.txtType);                    
                    txtLikes = (EditText) findViewById(R.id.txtLikes);
                    txtDislikes = (EditText) findViewById(R.id.txtDislikes);

                    if(txtDate.getText().toString().length() == 0 || txtType.getText().toString().length() == 0 || 
                            txtLikes.getText().toString().length() == 0 || txtDislikes.getText().toString().length() == 0)

                    {
                        txtDate.setError("Please key in the date");
                        txtType.setError("Please key in category");
                        txtLikes.setError("Please key in their likes");
                        txtDislikes.setError("Please key in their dislikes");
                    }
                    else
                    {
                        Toast.makeText(getBaseContext(), "Your information is saved successfully! :D", Toast.LENGTH_LONG).show();
                    }                                                   
                }
            });

        btnBack = (Button) findViewById(R.id.btnMain);
        btnBack.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View arg0)
                {
                    finish();

                }
            });

        btnView = (Button) findViewById(R.id.btnView);
        btnView.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                    EditText txtDate = (EditText) findViewById(R.id.txtDate);
                    EditText txtType = (EditText) findViewById(R.id.txtType);
                    EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
                    EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);

                    Intent i = new Intent(PersonalInformation.this, SavedInfo.class);                   
                    Bundle b = new Bundle();
                    b.putString("name", nameSpinner.getSelectedItem().toString());
                    b.putString("date", txtDate.getText().toString());
                    b.putString("category", txtType.getText().toString());
                    b.putString("likes", txtLikes.getText().toString());
                    b.putString("dislikes", txtDislikes.getText().toString());
                    i.putExtras(b);
                    startActivity(i);
                }
            });

        EditText txtDate = (EditText) findViewById(R.id.txtDate);
        txtDate.setHint("birthdate or wedding date");
        txtDate.setFilters(new InputFilter[] {
                new InputFilter() {

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend)
                    {
                        if(source.equals(""))
                        {
                            return source;
                        }
                        if(source.toString().matches("[0-9 -]+"))
                        {
                            return source;
                        }
                        return "";
                    }

                }
            });

        EditText txtType = (EditText) findViewById(R.id.txtType);
        txtType.setHint("birthday or wedding");
        txtType.setFilters(new InputFilter[] {
                new InputFilter() {

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend)
                    {
                        if(source.equals(""))
                        {
                            return source;
                        }
                        if(source.toString().matches("[birthday wedding Birthday Wedding BIRTHDAY WEDDING]+"))
                        {
                            return source;
                        }
                        return "";
                    }

                }
            });

        EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
        txtLikes.setHint("e.g. Skating, Photography separate by comma");
        txtLikes.setFilters(new InputFilter[] {
                new InputFilter() {

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend)
                    {
                        if(source.equals(""))
                        {
                            return source;
                        }
                        if(source.toString().matches("[a-z A-Z -]+"))
                        {
                            return source;
                        }
                        return "";
                    }

                }
            });

        EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
        txtDislikes.setHint("e.g. Skating, Photography separate by comma");
        txtDislikes.setFilters(new InputFilter[] {
                new InputFilter() {

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                            int end, Spanned dest, int dstart, int dend)
                    {
                        if(source.equals(""))
                        {
                            return source;
                        }
                        if(source.toString().matches("[a-z A-Z -]+"))
                        {
                            return source;
                        }
                        return "";
                    }

                }
            });


    }
}

第二次活动

public class SavedInfo extends Activity
{
    final Context context = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saved_info);

        Bundle b = this.getIntent().getExtras();
        String name = b.getString("name");
        String date = b.getString("date");
        String category = b.getString("category");
        String likes = b.getString("likes");
        String dislikes = b.getString("dislikes");

        ((TextView)findViewById(R.id.textName)).setText(name);
        ((TextView)findViewById(R.id.textDate)).setText(date);
        ((TextView)findViewById(R.id.textType)).setText(category);
        ((TextView)findViewById(R.id.textLikes)).setText(likes);
        ((TextView)findViewById(R.id.textDislikes)).setText(dislikes);

        Button btnEdit = (Button) findViewById(R.id.btnEdit);
        btnEdit.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Intent backIntent = new Intent(context, PersonalInformation.class);
                    startActivity(backIntent);

                }
            });

        Button btnBack = (Button) findViewById(R.id.btnMain);
        btnBack.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Intent menuIntent = new Intent(context, MainPage.class);
                    startActivity(menuIntent);
                }
            });
    }

}

请帮助我如何将数据库表中的数据作为 TextView 检索到第二个活动中,
我将不胜感激提供的任何帮助。提前致谢!=)

4

1 回答 1

1

如果我理解正确,您需要什么,以下是从 SQLite 数据库中提取结果以进行查询的代码片段。希望这会对您有所帮助:

// Access Database to fetch info
try {
    newDB = myDbHelper.getReadableDatabase();

    String query = "SELECT * FROM tbl_name WHERE...... = ";
    Cursor c = newDB.rawQuery(query,null);

    if (c != null ) {
        if (c.moveToFirst()) {
            do {
                Bean infoBean = new infoBean();
                infoBean.firstName = c.getString(c.getColumnIndex("firstName"));
                infoBean.lastName = c.getInt(c.getColumnIndex("lastName"));
                infoBean.id = c.getInt(c.getColumnIndex("_id"));

                // Do something here to display information in TextView

            } while (c.moveToNext());
        } 
    }           
} catch (SQLiteException se) {
    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} 

myDbHelper.close();
于 2012-09-27T15:45:29.477 回答