将数据库中的结果显示为新的黑色活动

时间:2013-03-06 21:04:41

标签: android database

我创建了一个数据库并运行一个查询,该结果将一些元素添加到结果中。 我想要我拥有的按钮,点击回应并将结果显示给新的黑色活动。我将发布我目前的课程。

对不起,但是对于android编程我很新。 :)

 package com.example.pota;

 import java.util.ArrayList;

 import android.app.ListActivity;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;

    public class createDatabase extends ListActivity{

private final String DATABASE_NAME = "potaDB";
private final String DATABASE_TABLE = "olaPota";
private final String DATABASE_TABLE2 = "favorites";
private final int DATABASE_VERSION = 1;
private SQLiteDatabase potaDB=null;;


//Kaleitai otan dimiourgeitai to activiy

public void onCreate(Bundle icicle, Context ctx){


    super.onCreate(icicle);
    ArrayList <String> results = new ArrayList<String>();

    try {

        //Dhmiourgei ti vasi an den uparxei alliws tin anoigei

        this.openOrCreateDatabase(DATABASE_NAME, DATABASE_VERSION, null);

        //Dhmiourgei ena table sti vasi me ta ekseis paidia 

        potaDB.execSQL("CREATE TABLE IF NOT EXISTS" + DATABASE_TABLE + "(id INT PRIMARY KEY AUTOINCREMENT, name VARCHAR, category VARCHAR, info TEXT, difficulty INT);", null); 

        //Vazei eggrafes ston pinaka

        potaDB.execSQL("INSERT INTO" + DATABASE_TABLE + "(name, category, info, difficulty)" + "VALUES ('Screwdriver', 'Pota', 'i klassiki vodka lemoni', 5);");

        //Ena query pou tha mas gurisei ta stoixeia tou pinaka

        Cursor c = potaDB.rawQuery("SELECT *" + "FROM" + DATABASE_TABLE,null);

        //Pernoume ta dedomena tou pinaka pou xreiazomaste

        int name1=c.getColumnIndex("name");
        int category1=c.getColumnIndex("category");
        int info1=c.getColumnIndex("info");
        int difficulty1=c.getColumnIndex("difficulty");





        //Pernei ta dedomena ekei pou deixnei o Cursor
        String name = c.getString(name1);
        String category = c.getString(category1);
        String info = c.getString(info1);
        int difficulty = c.getInt(difficulty1);

        //Prosthetei ta trexontai dedomena sto results
        results.add("Onoma Potou: " + name + "Katigoria Potou: " + category + "Suntagi: "+ info + "Duskolia: " + difficulty);












    } catch(Exception e) {
        System.out.println(e);
    }finally {
            if (potaDB != null)
                potaDB.close();
        }


        }
 }

主要活动:

 package com.example.pota;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;

 public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



  }

1 个答案:

答案 0 :(得分:0)

好的,我不确定您要使用哪个Activity按钮,但执行以下操作:

public class MainActivity extends Activity {
 Button myBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBtn = (Button) findViewById(R.id.my_button);
mBtn.setOnclickListener(new OnClickListener() {         
    @Override
    public void onClick(View v)
    {
          Intent intent = new Intent(MainActivity.this, ReceivingActivity.class);
          intent.putExtra("key", value); //name key whatever you want then value is whatever you are sending 
          startActivity(intent); //this starts the next activity through an intent
      }

    }

使用onClick()有不同的方法,但这可能是最受欢迎的,但这取决于您的需求。 Here is one of my answers that shows how to do it using xml

您还需要Read about Intents并了解如何使用它们以及它们的功能。

要获取其他Activity中的数据,您将执行类似

的操作
public void onCreate()
{
    ...
    Intent recIntent = getIntent();
    String variableName = recIntent.getStringExtra("key");
}