Android ListView AdddAll problem - android

hi i am Using ArrayAdapter in ListView with custom Class object,HERE IS MY CODE
private static class NewsDetailAdapter extends ArrayAdapter<clsNewsItem>
{
private final Activity context;
List<clsNewsItem> newsList = null;
public NewsDetailAdapter(Activity context, ArrayList<clsNewsItem> clsNewsObjects) {
super(context, R.layout.listview_cell, clsNewsObjects);
this.context = context;
this.newsList = clsNewsObjects;
}
public void clear()
{
newsList.clear();
}
while i am working with this code AdapterObj.NotifyDatasetchanged() not working Due to i have not implemented addAll() method for this class,i cant understand how to write this Method so how can i Write Add All method for this ArrayAdaper class..can Any one help me please

Do you have specific need for subclassing? If you need to have some logic better have one ArrayAdapter as member instance and proxy its methods. Because the original ArrayAdapter already has all these methods readily available

Related

What does "this" contain when using the "implements"?

I am creating an interface in the android studio. I am doing the onClick functionality for my RecyclerView.
Here you can see my HomeFragment, which contains the RecyclerView and thus implements the method onEventListener.
public class HomeFragment extends Fragment implements HomeAdapter.onEventListener
I also had to change my constructor in the adapter.
public HomeAdapter(Activity ctx, ArrayList<EventPost> Arr, onEventListener onEventListener) {
this.ctx = ctx;
this.items = Arr;
this.mOnEventListener = onEventListener;
}
For this reason, in my HomeFragment.java I had to change the arguments when initializing the HomeAdapter.
homeAdapter = new HomeAdapter(getActivity(),event_list, this);
Here I was able to use this, to pass the onEventListener. However, I also initialize the HomeAdapter later in the app, but this initialization lies in the method, which has "this" assigned to different things than the one before. For this reason, it does not work with this and requires onEventListener type of argument.
How would I change "this" in the second initialization, so that it works globally?
use HomeFragment.this in your second method.
homeAdapter = new HomeAdapter(getActivity(),event_list, HomeFragment.this);
Try passing this with classname like
onEventListener.this
instead of "this".
homeAdapter = new HomeAdapter(getActivity(),event_list, onEventListener.this);

Passing an argument from a regular class to an activity

I would like to call a method in an activity and pass an argument to it from a non activity regular class in android.
As i understand, i cant simple use the following code, plus it does not work:
int mySound = 0;
SoundsActivity soundsActivity = new SoundsActivity();
soundsActivity.playSound(mySound);
That code is located in a regular class called "MyAdapter".
There are a few ways you can do this. I can't be specific since you didn't really show any code.
You can't do what you're trying to do though. Activities can't be instantiated like that (as well as anything extending Context), and it won't do what you want.
Use a broadcast.
This will require that you have a Context object passed into your Adapter, which you can do simply by modifying the constructor and adding a global variable:
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
Then you can use that Context to send a local broadcast with your own action:
Intent intent = new Intent("my_custom_action");
intent.putExtra("sound_type", 0);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
And receive that action in your Activity to call your method: See Context-registered Receivers
When you construct the Adapter, pass a Context object into it. If you're constructing from an Activity (hopefully SoundsActivity), use this:
MyAdapter adapter = new MyAdapter(this);
Use a callback.
Delcare an interface somewhere:
public interface AdapterCallback {
void onRequestPlaySound(int type);
}
Implement that interface in your Activity:
public class SoundsActivity extends Activity implements AdapterCallback {
//...
#Override
public void onRequestPlaySound(int type) {
playSound(type);
}
//...
}
Add the interface as a parameter in your Adapter's constructor:
private AdapterCallback callback;
public MyAdapter(AdapterCallback callback) {
this.callback = callback;
}
And then use callback.onRequestPlaySound(0); from wherever you need.
When you construct the Adapter, pass your SoundsActivity instance into it. This will only work if you're constructing the Adapter from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Pass SoundsActivity directly.
This isn't the cleanest way, nor is it the recommended way, but it will work. In your Adapter:
private SoundsActivity activity;
public MyAdapter(SoundsActivity activity) {
this.activity = activity;
}
And from SoundsActivity:
MyAdapter adapter = new MyAdapter(this);
Then just call activity.playSound(0); where you need to.

Adjust RealmResult without querying

I'm using a simple instance of RealmRecyclerViewAdapter to handle displaying MyObjects:
public class MyObjectsAdapter extends RealmRecyclerViewAdapter<MyObject, MyObjectsAdapter.ViewHolder> {
public interface Listener {
void onMyObjectClicked(Long myObjectId);
}
private Context context;
private Listener listener;
public MyObjectsAdapter(Context context, OrderedRealmCollection<MyObject> data, Listener listener) {
super(context, data, true);
this.context = context;
this.listener = listener;
}
... // Rest of the code.
Although this works for simple queries, I have one filter that is too complex for a realm query, so I'll have to filter myself by looping the results. But since I cannot "remove" an object from RealmResults (as it removes it from the realm), I would need to work with a List, which means I would have to rewrite the adapter.
My question is, is there a way to adjust RealmResult in a way that is not based on a realm query?
Write out what #EpicPandaForce suggests.
Add an extra field (hour) to your model class:
class MyObject extends RealmObject {
private Date timestamp;
private int hour;
public setTimestamp(Date timestamp) {
setHour(timestamp.getHours());
this.timestamp = timestamp;
}
// ...
}
You can now use hour in your queries.

add list in popupwindow

I have to make an application where I have to show a list of names in popup.
I have used array-list to fetch the values from database, but I cannot put it in array-adapter.
here is my code:
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
... // rest of the code
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(Calculator_new_Pop.this,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
... // rest of the code
}
the error is
"The constructor ArrayAdapter(Calculator_new_Pop, int, ArrayList) is undefined"
Can anyone help me out?
Use activity context
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,wallAreas);
You can pass the activity context to the constructor of Calculator_new_Pop and use the same instead of Calculator_new_Pop.this
Edit
Context mContext;
public Calculator_new_Pop(Context context)
{
mContext = context;
}
Then
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
Have a look at the constructor of ArrayAdapter.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Refer the link
link
ArrayAdapter needs a context as a parameter, you are supplying Dialog instance which is not the Context type, this is the reason why an error is shown. Instead of Dialog instance get the activity context.
You can pass the activity context in the constructor of the dialog, and hence supply that context in the ArrayAdapter.
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
Context mContext =null;
public Calculator_new_Pop(Context c ){
this.mContext = c;}
.............
.............
.............
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
.......
}
When instantiating dialog from activity, pass this.
i am getting 'null' in "mContext"
so i tried this..
public Calculator_new_Pop(Activity parent) {
// TODO Auto-generated constructor stub
super(parent);
this._act = parent;
//
}
_ltvw.setAdapter(new ArrayAdapter<String>(_act,android.R.layout.simple_list_item_multiple_choice,data));
it worked..

Best practice to pass Context to non-activity classes?

So, my first major application is almost coded and I'm doing optimizations on my code. The app works fine, but I'm not sure about my way of passing the context to other classes. I don't want to do it the wrong way. I stumbled upon articles and questions here in Stackoverflow about contexts and which is the right way to pass it to non-activity classes. I read the documentation as well, but being a Finn makes complicated tech speak even harder to understand.
So, a simple question. Is my way of passing my main activity's context to other (helper) classes correct? If not, where can I read more about better practice on these situations.
For example:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle sis){
super(sis);
new Helper(MyActivity.this).makeMyAppAwesome();
}
}
Helper.java
public class Helper {
Context context;
Helper(Context ctx){
this.context = ctx;
}
public void makeMyAppAwesome(){
makeBaconAndEggsWithMeltedCheese(context);
}
}
Is this OK? It would be nice if someone could provide an easy to read article with examples on this subject.
You can do that using ContextWrapper, as described here.
For example:
public class MyContextWrapper extends ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public void makeMyAppAwesome(){
makeBaconAndEggsWithMeltedCheese(this);
}
}
And call the non activity class like this from an Activity
new MyContextWrapper(this);
It is usually in your best interest to just pass the current context at the moment it is needed. Storing it in a member variable will likely lead to leaked memory, and start causing issues as you build out more Activities and Services in your app.
public void iNeedContext(Context context) {...
Also, in any class that has context, I'd recommend making a member variable for readability and searchability, rather than directly passing or (ClassName.)this. For example in MainActivity.java:
Context mContext = MainActivity.this;
Activity mActivity = MainActivity.this;
I have passed context like this which solved my problem:
public class Utils extends ContextWrapper {
private final Context context;
public Utils(Context context) {
super(context);
this.context = context;
}
public void mymethod(){}
}
super(context); with ContextWrapper helped to make getBaseContext() and getApplicationContext() valid and this.context = context; captured context in variable which I can use wherever needed in methods.
Maybe alternatively you can just opt for using a constructor with this.context = context; and replace all occurrences of getApplicationContext() and getBaseContext().
Well, an even better way is to pass context directly to the method if using only few from a class for avoiding memory leaks.
You could also create a static instance reference to your MainActivity initialized in the onCreate() method
public class MainActivity extends AppCompatActivity {
public static MainActivity mMainActivity;
#Override
private onCreate(Bundle savedInstanceState){
//...
mMainActivity = this;
}
}
and call the context like this:
MainActivity.mMainActivity;
or write a method getInstanceOf() if it's clearer and/or you prefer using an accessor
MainActivity.getInstanceOf();
This strategy might provide you with some flexibility if you decide later that you would like to call an instance method contained in your main activity like so:
MainActivity.mMainActivity.myInstanceMethod();
Just a suggestion. Criticism is welcome and encouraged.

Categories

Resources