0

我创建了一个数据库并运行了一个查询,该查询向结果中添加了一些元素。我希望我拥有的按钮能够在单击时做出响应并将结果显示给新的黑色活动。我将发布我当前的课程。

对不起,我对 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;
}



  }
4

1 回答 1

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()但这可能是最流行的,但这取决于您的需要。这是我的答案之一,显示了如何使用 xml

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

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

public void onCreate()
{
    ...
    Intent recIntent = getIntent();
    String variableName = recIntent.getStringExtra("key");
}
于 2013-03-06T21:35:28.670 回答