I am trying to start an activity from a normal class and I can't figure out how it is done, if it can be done. On an itemClick I want to start an activity that extends the ListView class to show a list of options.
Also the class that receives the onItemClick is not an activity. I will post the code to try to visualize what i mean.
This is my onClick method in the class that wants to start a an activity.
public void onClick(View v) {
if (v.equals(this)) {
notifyObservers(this.getId());
} else if(v.equals(editButton) || v.equals(deleteButton)) {
This is where I want to start the activity to show my ListView...
}
}
This is my class that extends the ListView class.
public class ProfileSettings extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] mainSettings = getResources().getStringArray(R.array.mainSettings);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mainSettings));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do something
}
});
}
}
Thanks in advance!
I think this may help you:
"Pass the context of the activity via constructor to your class or make a static context in your activity.
With the context you can start activities like you would start them within the activity class."
class First extends Activity {
...
Second test = new Second(this);
test.start();
...
}
class Second {
private Context mContext;
...
public Second(Context c) { this.mContext = c; }
...
public start() { mContext.startActivity(...); }
}
for more detail check
http://www.anddev.org/view-layout-resource-problems-f27/starting-an-activity-from-a-non-activity-class-t14483.html
Try this in your onClick
Intent i = new Intent(this, ProfileSettings.class);
startActivity(i);
EDIT:
Also dont forget to add the activity to your manifest.
Related
I have a list view which show list of task, on selecting task it shows details about task, when I delete the particular task it returns to the previous activity by finish(). but it does not update the list.
I want to know how and where to use notifyDataSetChanged method and add adapter method is never used.
Other than notifyDataSetChanged() solution is also accepted :) i just want to update the list when it returns to the previous activity.
Do it with startActivityForResult(). When you create intent to open new activity open it for result. The task being deleted is your result. So when it's marked as deleted and you return to your previous activity, the result triggers and you can delete the marked item + call the notify.
More info here : http://developer.android.com/training/basics/intents/result.html
you can use the notifyDataSetChanged on the onResume method, this always update the data itself when the Activity shows
You can also notify your parent activity before finishing your current activity. You just have to register a Receiver in your main activity and all others activities will be able to notify that activity. You can even send data!
As my english is not that good, sample code :
The MainActivity class
public class MainActivity extends AppCompatActivity {
// you can define your name for your receiver as a constant,
// so you can access it from other activities if you want
public static final String MY_SUPER_INTERNAL_NOTIFICATION = "MY_SUPER_INTERNAL_NOTIFICATION";
public static final String MY_OBJECT = "my_object";
public static final String MY_OBJECT_POSITION = "my_object_position" ;
private MyCustomAdapter adapter;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
// set up your list with your adapter and data...
adapter = new MyCustomAdapter(this);
// [...] I suppose you know how to do that, I dont write everything
listView.setAdapter(adapter);
// when you click on a row from your list,
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(
MainActivity.this,
SecondActivity.class
);
// you can for example put data in a Bundle and pass it to the other activity
// then on the onCreate you can use that data as you want
Bundle bundle = new Bundle();
// IMPORTANT be sure that the object you are putting in the bundle is Serializable (implements Serializable)
bundle.putSerializable(MY_OBJECT, adapter.getItem(position));
// you can also for example send the position of the row you clicked
bundle.putInt(MY_OBJECT_POSITION, position);
// put your bundle in the intent
intent.putExtras(bundle);
startActivity(intent);
}
});
// register your Receiver! don't forget to do that or you will never be notified
LocalBroadcastManager
.getInstance(this)
.registerReceiver(
broadcastReceiver,
new IntentFilter(MY_SUPER_INTERNAL_NOTIFICATION)
);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
// get the position sent by the other activity
int position = intent
.getExtras()
.getInt(SecondActivity.MY_EXAMPLE_KEY);
adapter.deleteItem(position);
}
}
};
// adapter class...
private class MyCustomAdapter extends BaseAdapter {
ArrayList<MyObject> data;
public MyCustomAdapter(Context context) {
}
public void deleteItem(int position){
// delete your item from the list of data
data.remove(position);
// dont forget to notify
notifyDataSetChanged();
}
#Override
public int getCount() {
return 0;
}
#Override
public MyObject getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
// adapter class, you can extend your favorite type of adapter
}
}
The SecondActivity class :
public class SecondActivity extends AppCompatActivity {
public static final String MY_EXAMPLE_KEY = "EXAMPLE";
private MyObject myObject;
private int myObjectPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
myObject = (MyObject) getIntent()
.getExtras()
.getSerializable(MainActivity.MY_OBJECT);
myObjectPosition = getIntent()
.getExtras()
.getInt(MainActivity.MY_OBJECT_POSITION);
// do all your stuff with your object
// before calling finish do this
beforeFinishDoThisStuff();
}
private void beforeFinishDoThisStuff() {
sendBroadcastToMainActivity();
finish();
}
private void sendBroadcastToMainActivity() {
// create an intent and put your Receiver name as action name
// like you defined in your MainActivity
Intent intent = new Intent(MainActivity.MY_SUPER_INTERNAL_NOTIFICATION);
Bundle bundle = new Bundle();
// put whatever you want, here I put just the previous position of the object in the list
bundle.putInt(MY_EXAMPLE_KEY, myObjectPosition);
intent.putExtras(bundle);
// notify your MainActivity
LocalBroadcastManager
.getInstance(this)
.sendBroadcast(intent);
// after this finish !
}
}
Hope you understand how to notify activities with BroadcastManager. It's very powerful and simple to use.
Have fun coding !
I have deveolped a method to call methods from another calss.I have created an adapter and inside that I have an override getView(). What I need to do is to get values from a listview and display as a report. I have tried this exact code inside another activity in the same project. It works perfectly. But when ever I moved this into another activity it doesn't give any error.But the overrided method is not calling. What should I do to make it run?
public class ViewList extends ActionBarActivity {
ArrayAdapter<Units> unitsAdapterView;
ListView ViewAll;
List<Units> Unit = new ArrayList<Units>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_list);
ViewAll = (ListView) findViewById(R.id.viewList);
populateListView();
viewView();
}
private void viewView(){
Button m=(Button)findViewById(R.id.tryBtn);
m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new Intent(getApplicationContext(),ViewList.class));
populateListView();
}
});
}
private void populateListView(){
unitsAdapterView =new UnitListAdaptorView();
ViewAll.setAdapter(unitsAdapterView);
// unitsAdapterView.getView();
}
private class UnitListAdaptorView extends ArrayAdapter<Units> {
public UnitListAdaptorView() {
super (ViewList.this, R.layout.list,Unit);
Log.i("","A");
// A();
}
#override
public View getView(int position, View view, ViewGroup parent) {
Log.i("","Resue connceted bulb 5");
if (view == null) {
view = getLayoutInflater().inflate(R.layout.list,parent ,false);
}
Units currentUnit = Unit.get(position);
TextView name = (TextView)view.findViewById(R.id.lblViewName);
name.setText(currentUnit.getName());
TextView bulb=(TextView) view.findViewById(R.id.lblViewBulb);
bulb.setText(currentUnit.getBulbNo());
TextView fan=(TextView) view.findViewById(R.id.lblViewFan);
fan.setText(currentUnit.getFanNo());
return view;
}
}
}
The problem was solved after adding the codes to get all the existing data from database. After adding the below codes to the onCreate method the function is working now!
DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
if (dbHandler.getUnitsCount() != 0)
Unit.addAll(dbHandler.getAllUnit());
In my MasterDetail Flow I call this function to create my list:
public class Ansicht extends SherlockFragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
//...
ListItems.setupMainItems(this);
//...
}
}
public class ListItems
{
public static List<DetailListItem> ITEMS = new ArrayList<DetailListItem>();
public static Map<String, DetailListItem> ITEM_MAP = new HashMap<String, DetailListItem>();
public static void addItem(String value, Typ t)
{
DetailListItem item = new DetailListItem(value, t);
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
//...
}
public class ListControl extends SherlockListFragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter<ListItems.DetailListItem>(getSherlockActivity(), R.layout.simple_listitem, android.R.id.text1,ListItems.ITEMS);
setListAdapter(adapter);
}
//...
}
But if I close my app and start it again the items are insert a second time. I think I do not have to call this function in onCreate but this image does not really help me because onCreate is the first method with is called.
In my setupMainItems method I only call the addItem method. The onPause and onResume are never overriden. If I close my app with the homebutton all works great but if I close it with the back button the items are insert a second time.
You could do something like
if(ListItems.ITEMS == null || ListItems.ITEMS.size() == 0)
ListItems.setupMainItems(this);
That way it'll only add the items if they haven't been added already
I am working on Base adapter in Android and i want to know that which overriden method gets call in BaseAdapter Class if i press onBackPress in some activity. Please help me, I have searched and didnt find any solution.
You probably have listview and you have a custom adapter set for the listview. Listview is in your activity.
class MyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstancestate)
{
setContentView(R.layout.main);
ListView lv= (ListView) findViewById(R.id.listview);
CustomAdapter cus= new CustomAdapter(MyActivity.this);
lv.setAdapter(cus);
}
}
class CustomAdapter extends BaseAdapter
{
....................
}
So when you press back button the current activity is popped form the back stack, destroyed and previous activity in the back stack takes focus.This the default behaviour.
http://developer.android.com/guide/components/tasks-and-back-stack.html
You can override onKeyDown(params) in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
//do something
finish();
return;
}
Overide onBackPressed in your activity and Change data to adapter accordingly then call notifyDataSetChanged
The onBackPressed will be activated in your Activity class. It is not an event of BaseAdapter class. So override your onBackPressed method in the Activity class.
you can pass activity reference to adapter via constructor like:
CustomAdapter customAdapter=CustomAdapter(getApplicationContext(),arrayList,HomeActivity.this);
and in Your Custom Adapter
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<CategoryModel> arrCategoryModel;
AppCompatActivity activity;
public CustomAdapter(Context context, ArrayList<CategoryModel> arrCategoryModel, AppCompatActivity activity)
{
this.context=context;
this.arrCategoryModel=arrCategoryModel;
this.activity=activity;
} }
Now you can call activity.onBackPressed(); wherever you want.
My Activity has multiple lists so I have defined MyClickListener as below:
My question is how I should instantiate this class:
MyClickListener mMyClickListener = new MyClickListener();
Or maybe it is better to instantiate inside the onCreate(Bundle) and just define above. Whats considered the better way? I don't want too much in onCreate() its already full of stuff. Any thoughts on the declaration and instatiation? Whats the best way?
private class MyClickListener implements OnClickListener
{
#Override
public void onClick(View view) {
}
}
I use same kind of class mechanism as you mentioned in the question.
this is the way i use,
public class myActivity extends Activity
{
private MyListener listener = null;
private Button cmdButton = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cmdButton = (Button) findViewById(R.id.cmdButton);
cmdButton.setOnClickListener(getListener());
}
// method to fetch the listener object
private MyListener getListener()
{
if (listener == null)
{
listener = new MyListener();
}
return listener;
}
private class MyListener implements Button.OnClickListener
{
public void onClick(View v)
{
}
}
}
Why are you instantiating a listener like that in the first place? Just create a new one when you assign it to your listView.
listView.setOnClickListener( new MyListener());