Question

I am facing an issue: a value is not getting transferred from one class to another.

The value is picked inside a class, but when retrieving it in another class I am getting null.

Below is the class where the value is set.

public class stockmanager extends Activity{

    String getentry=null;
    Database d=new Database(this);
    StockTable st=new StockTable();


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockmanager);

    final Button AddStock=(Button) findViewById(R.id.button1);
        final EditText entry=(EditText) findViewById(R.id.editText1);
        final Button BroDetail=(Button) findViewById(R.id.button2);

        AddStock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getentry=entry.getText().toString();
                d.db.insert(st.tablename, null,st.insert());


            }
        });
                }
}

In this class I am using the value getentry but I am getting null.

public class StockTable {

final String tablename="StockTable";
private String column1="Stock_ID";
private String column2="StockName";

stockmanager sm=new stockmanager();

final String stocktable = "CREATE TABLE " + tablename + 
            " (" + column1+ " INTEGER PRIMARY KEY , " + column2 + " TEXT) ";

public ContentValues insert(){

    ContentValues cvi=new ContentValues();
    for(int i=0;i<=sm.getentry.length();i++)
    {
        cvi.put(column1, 1);
        cvi.put(column2,sm.getentry);
                    }

    return cvi;
}

public void delete(){


}

Please help me to solve this problem

Was it helpful?

Solution

You have

public class stockmanager extends Activity{

And you are instantiating a activity class

stockmanager sm=new stockmanager().

You should not instantiate a activity class. It is wrong. Instead you can pass the value to the method itself or to the constructor of other class.

You can do as

StockTable st=new StockTable();
st.insert(your params);

Then in StockTable have a insert method that takes params

public ContentValues insert(params){

OTHER TIPS

I don't know what you want to do. But constructor new stockmanager() in StockTable will not fire onCreate() method.

You need to pass your Activity to StockTable class like:

public StockTable(stockmanager yourActivity)
{
 this.st = yourActivity;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top