notify activity that checkbox in listview is clicked - android

I have an activity that contains listview, inside listview rows i have a checkbox in each row. every time i check or uncheck the checkbox the activity should listen immediately and knows how many rows is checked or unchecked, how do I implement it in android? thanks before

You should create Listener like below.
interface CheckBoxCleckListener{
void OnCheckboxClicked();
}
Define above code in your adapter class or other.
Now whenever you want to call it, write following code in your Adapter class.
//this will create object of listener
public static CheckBoxCleckListener checkBoxCleckListener;
You need to initialized it by your caller class which is your Activity in your case.
public static void addListener(
CheckBoxCleckListener listener) {
checkBoxCleckListener= listener;
}
Now, whenever your check box clicked, write following code inside,
checkBoxCleckListener.OnCheckboxClicked();
In your activity class, write following code,
public class YourActivity extends Activity implements CheckBoxCleckListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//rest of code
YOUR_CLASS_WHERE_INTERFACE_IMPLEMENTED.addListener(this);
}
#Override
public void OnCheckboxClicked() {
//do your coding
}
}

Related

Communication between two adapters in android?

I have two views In a single activity StackView and Gridview and 2 corresponding adapters .when I clicked on StackView item it should flip the grid item in Gridview and vice versa?
// activity classs
public class SampleActivity extends Activity implements
OnGridChangeListener {
public void onCreate(bundle){
// replace this with your adapter class.
Adapter adapter = new adapter(this);
}
#override
public void OnGridChange(){
//here you go.
// write code to do what you want.
}
// interface to communicate with activity
public interface OnGridChangeListener {
public void OnGridChange()
}
// adaptor class
public class Adaptor extends "you apapter class to extend"{
OnGridChangeListener onGridChangeListener ;
public Adapter(OnGridChangeListener listener){
onGridChangeListener =listener
}
public getView(){
public void onclick(){
onGridChangeListener.OnGridChange("pass you data");
}
}
}
As per your question this is what i get -
You have a Activity with two independent views which has their own adapters. So when there is changes in one of the adapter you want it to be reflected into another adapter.
The simple solution for your query would be -
When there is a change in first adapter you reflect the change to the activity. after that call the function in the second adapter to reflect the change you want in second adapter.
For this you have to define an interface in first adapter and implement this is activity.
When the first adapter changes call the interface method and this will reflect in activity.
Then call method in second adapter to do the changes you want.
Code example -
MainActivity.Java
public class MainActivity extends AppCompatActivity implements FirstAdapter.callBackMethods {
FirstAdapter firstAdapter;
SecondAdapter secondAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstAdapter = new FirstAdapter(MainAtivity.this.getApplicationContext());
//do all the declaration and operation of first adapter. Pass context along with your required params.
secondAdapter = new SecondAdapter();
//do all the declaration and operation of second adapter.
}
//callback method of first adapter
#override
public void callback(){
//changes have been done in FirstAdapter and this methos is fired.
//now do do the changes in SecondAdapter as per req.
if(secondAdapter != null){
secondAdapter.reflectChanges();
}
}
}
FirstAdapter.class
I am taking example of recylerview adapter.
public class FirstAdapter extends RecyclerView.Adapter<FirstAdapter.ViewHolder>{
public FirstAdapter(Context context){
this.context=context;
}
/*
all the boilerplate codes and business logic
*/
//when you want to reflect the changes
callBackMethods callBackMethods = (callBackMethods) context;
callBackMethods.callback();
//this will fireup the implementation in the MainActivity.
public interface callBackMethods{
public void callback();
}
}
SecondAdapter.class
This is where the changes will be reflected.
public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{
/*
all the boilerplate codes and business logic
*/
public void reflectChanges(){
/*
This method will be called from the MainActivity. pass whatever params you want to pass from Activity and do the changes you want to do in the SecondAdapter.
*/
}
}
I hope this solves your problem. Happy coding...

How to pass a custom listview from one activity to another activity in Android without using intent

I need a sample program where I have a custom listview with adapter and when I click on a item on the list view it must be selected and it should be visible in another activity. This should be done without Intent.
You can use Shared Preference, When you click on any of the item store that data in prefrence and use in another activity.
Make your arraylist global in one activity as below:
public class ActivityOne extends Activity{
public static ListView list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT);
list=(ListView)findViewById(R.id.YOUR_LIST_ID);
..................
..................
}
}
And in other activity fetch it as below:
public class ActivityTwo extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list=ActivityOne.list.getListView();
..................
..................
}
}
If you dont want to use untent extends the application class and make property that holds object of what you have selected in list view
//pseudocode
//Application class
class GlobalApp extends Application {
//you need to overirde this method
public void oncreate(parameter) {
super.onCreate(parameter);
}
public MyClass myObject; // replace MyClass with datatype need
}
// In activity where you are handling listview make class variable of type GlobalApp
GloabalApp app = (GlobalApp) getApplication();
//in your item selected method of your list view
app.myobject = selectedValue
// In another activity on activity method
GloabalApp app = (GlobalApp) getApplication();
Myclass Selectedobj = app.myobject
In manifest file in your application tag add android:name attribute with value="yourpackagename.GlobalApp"

What is the best way to organize android development code?

I know that every situation will be different, but I just wanted see if there was a general recommendation.
Currently, I have my activities (screens) dynamically creating custom button objects and custom edit text objects. Each of these objects have listeners to see if their state has changed. These object classes have all the logic for the screen. The activity's only job is to assign objects to the widgets I created in XML.
Part of me thinks it should be opposite, where the activity contains all the logic for all the widgets on the screen and simply waits for the objects to notify it when the listeners go off.
Which way is more "standard" ?
I use the following way. I have a common EventHandler sub class in every activity or fragment and I add a single instance belonging to activity to each UI item. EventHandler implements OnClickListener, OnChanged.., and so on.
I would also recommend looking at this library, if you are familiar with DI concept:
https://github.com/roboguice/roboguice/
Here is an example of code of mine:
package com.x.y;
public class DashboardActivity extends FragmentActivity {
private EventHandler eventHandler = new EventHandler();
#SomeAnnotationForInit(R.id.some_id)
private Button feedButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
initGui();
}
private void initGui() {
feedButton.setOnClickListener(eventHandler);
}
private class EventHandler implements View.OnClickListener {
#Override
public void onClick(View view) {
if(view.equals(...)) {
//TODO:
}
}
}
}

Write a method just once and access it in "android:onClick" everywhere in the application

I have a title bar in my application. And the title bar has one button. On click of that button I display info activity. Now, as far as I know, android:onClick needs a reference of a public method inside the activity which has the xml set in setContentView(). Now, as the logic for that buttons click will be the same throughout the application, so what I want is, I will the method just once say showInfoScreen(View view) and put it in that buttons onClick attribute. And I need not write the same method everywhere. Is it possible?
Of course it's possible. Write an Activity class, then have all of your Activitys extend it. For example:
public abstract class BaseActivity extends Activity {
#Override
public void setContentView( int layoutResID ) {
super.setContentView( layoutResID );
findViewById(R.id.button).setOnClickListener(new OnTitleBarButtonClickListener());
}
private void showInfoScreen() {
// Show the info screen
}
private class OnTitleBarButtonClickListener implements OnClickListener {
#Override
public void onClick(View v) {
showInfoScreen();
}
}
}
Then all of your derived Activitys would extend BaseActivity instead of Activity.
The beauty of doing it this way is that any Activity that extends this class automatically gets this feature. No coding is required in the derived classes, just in BaseActivity. The only contract all of your Activitys will have will be to have R.id.button or whatever id you name it within its content.
I think you have to write onclick in every Activity where you want to display infoscreen.
But OnClick you just call A method showInfoScreen(View view) in every Activity....
And you should create class Like...ShowInfo and there are one static method...
public class ShowInfo{
public static void showInfoScreen(View view,Context c){
//now dispay info here
}
}
Write ShowInfo.showInfoScreen(v,YourClassName.this) in your onClick() Method....
An example of what Vinayak.B suggested is like this:
public class yourAppUtils {
public static void yourMethod() {
// Do stuff
}
}

Best practice to access Activity from Views

I have a question that seems simple but I cannot figure out what is the best practice for that :)
What is the best practice to access from a View, a method on the Activity that launched the View?
For example, I have an Activity with a layout that contains a Button and a Textfield. I want when I click on the Button, to call a method on my Activity that update the Textfield with some value. I come with multiple solutions:
1 - Inner class for the OnClickListener directly on the Activity so I can the method of the Activity with MyActivity.this.updateTextField() on onClick method
2 - Outer class for the OnClickListener, on my onClick method I can do: ((MyActivity)getContext()).updateTextField()
3 - Reference the Activity on my OnClickListener class when I instantiate it:
myButton.setOnClickListener(new MyOnclickListener(MyActivity));
I don´t want solution 1 because I don´t like that much inner class and I want reusable code. Solution 2 seems good but can produce error on runtime if my context is not an activity. Solution 3 seems good also but "heavy".
What is the best practice on Android to tell from the View to its Actitity that something needs to be done on the Activity?
Thanks!
implement activity with onclickListener and add unimplemented method onclick
just check for the view to see which button is clicked incase you are using multiple buttons
Although I mostly find myself end up with inner classes, there are other options.
You can create an interface like the following and let your activity implement it:
public interface UpdateableTextField {
public void updateTextField();
}
Now let the Activities that you want implement this interface.
Now, create a class that implements View.OnclickListener and set the constructor to get UpdateableTextField as a parameter:
public class MyListener implements View.OnclickListener {
UpdateableTextField updatable;
public MyListener(UpdateableTextField updatable) {
this.updateable = updatable;
}
#Override public void onClick(View v) {
// do some stuff
updateable.updateTextField();
}
}
And last, in the Activity:
public class MyActivity extends Activity implementes UpdateableTextField{
#Override public void onCreate(Bundle savedInstanceState) {
// usuall stuff
MyListener listener = new MyListener(this);
someView.setOnClickListener(listener);
// other stuff
}
#Override public void updateTextField() {
// well, update the text field :)
}
}

Categories

Resources