0

i have created a database and run a query which add some elements to results. I want the button that i have, to respond on click and show the results to a new black activity. I will post my current classes.

Sorry for everything but im very new to android programming. :)

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


        }
 }

And the Main Activity:

 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
    You need to be specific about your problem. Are you getting the data that you want to send to the other `Activity`? Don't know how to send it? I don't even see a `Button` in there – codeMagic Mar 06 '13 at 21:15
  • that is my problem. i dont how know how to code the button response. i want to display the "results" in a new black activity, but the android tutorials didn't give me much of a help i have created a button in the xml form called button1. – user2141751 Mar 06 '13 at 21:25

1 Answers1

0

Ok, I'm not sure which Activity you want to use the button in but do something like:

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
      }

    }

There are different ways to use onClick() but this is probably the most popular but it just depends on what you need. Here is one of my answers that shows how to do it using xml

You also need to Read about Intents and understand how to use them and what they are capable of.

To get the data in your other Activity you will do something like

public void onCreate()
{
    ...
    Intent recIntent = getIntent();
    String variableName = recIntent.getStringExtra("key");
}
Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • cant make this work. i just want to display the results ArrayList from the first class when the button is clicked. – user2141751 Mar 06 '13 at 22:40
  • Display them where? If you want to display them in a different `Activity` then you need to send the `ArrayList` through an `Intent`. If you want to display them in the same `Activity` then you need a `View` such as an `EditText`. If you tell me exactly what you are trying to do and show me what you have then I can help you – codeMagic Mar 06 '13 at 22:48
  • ok first of all. In my GUI there is One button called button1. I want when this button is pressed to display the ArrayList called results from the first class that i have posted to a new blank GUI which contains the ArrayList "results" the first class as you can see creates a database, adds some stuff, and then runs a simple query which adds the contents in the ArrayList results. Im terribly sorry for the incovinience but im veeeery noob :/ – user2141751 Mar 06 '13 at 22:53