Monday, September 9, 2013

[android help] Eclipse Android (ADT) How do I use methods from other classes in MainActivity class?


Eclipse Android (ADT) How do I use methods from other classes in MainActivity class?



Classes A, B and C is located only in src and the MainActivity.class in source/com/example/TestApplication.


MainActivity doesnt seem to see the other classes because they are not located in the same location/package. How can I call the other classes' methods to work in MainActivity.


Here is an example of my code: I want to run UseActivity methods in MainActivity



public class UseActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public String sEntries(View view)
{
DatabaseHandler db = new DatabaseHandler(this);
String sfinal = "";

// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
switch (view.getId())
{
case R.id.button1:
EditText dbText = (EditText) findViewById(R.id.editText1);

// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List contacts = db.getAllContacts();
for (Contact cn : contacts)
{
sfinal = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to sfinal
Log.d("Name: ", sfinal);
dbText.setText(sfinal);
}
break;
}
return sfinal;
}
}



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

UseActivity u = new UseActivity();
{
u.sEntries(View view);
} /////////////////////////// this doesnt work, program underlines UseActivity in red giving error and doesnt see it as another class (maybe because of package)


Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...