Android: display BroadcastReceiver in ListView - android

I created a broadcast receiver to follow my Wi-Fi, I'm getting normaly information in LogCat, but I couldn't put the data in a ListView, like I wan't.
I put a ListView example to test it in the onReceive method but I didn't work, and I'm having this error:
The constructor ArrayAdapter(Wifi.Receiver, int, int,
String[]) is undefined
This is my code:
public class Wifi extends Activity implements OnClickListener{
/** Called when the activity is first created. */
WifiManager wifi;
Button enab;
String resultsString ;
String[] myStringArray;
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
}
ListView listView ;
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listView1);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
};
//getting this error : The constructor ArrayAdapter<String>(Wifi.Receiver, int, int, //String[]) is undefined
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
}
PS: when I put the code of the ListView in the onCreate method it worked normaly, but I want it in the onReceive method.

In your case this does not refer to Activity Context.
Use
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Wifi.this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
Look at the constructor
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
Added in API level 1 Constructor
Parameters
context The current context.
resourc The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId The id of the TextView within the layout resource to be populated
objects The objects to represent in the ListView.
So the first param is a valid context
java.lang.Object
↳ android.content.Context // see context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity // see activity
And your class extends Activity
public class Wifi extends Activity
Hence use Wifi.this as a valid context.

Related

How to access variable in a Activity, from childView?

I have a view as following hierarchy.
The main Activity has
ArraylistArrayListArraylist()
I want to access and change the data of the Arraylist from the button of the Card view. I'm using Custom ArrayAdapter. So is there are any way to do this?
You just need to pass your list data from activity to your custom adapter by calling adapter's constructor. See below how you can pass list data and that from adapter.
public TestAdapter extends ArrayAdapter<String>{
private Context mContext;
private List<String> list = new ArrayList<>();
public MovieAdapter(#NonNull Context context, #LayoutRes ArrayList<String> list) {
super(context, 0 , list);
this.mContext = context;
this.list = list;
}
.............//use "list" in your adapter
}
In activity you have below list.
Arraylist a = new Arraylist();
a.add("test");
a.add("test1");
a.add("test2");
a.add("test3");
TestAdapter testadapter=new TestAdapter(this,a);
Now you have that list in adapter and you can use list in your adapter.

Static Application Contex on android

I am Trying to inflate one ListView When clicking a member of another ListView Which is in a ListActivity. So when I try to set Adapter it asks me for getApplicationContext, so the ListView is in another activity and I am trying to call that that applicationContext from another the ListActivity which will inflate the ListView if you click it.
I am Doing this and I dont Know If I am right or wrong
public class FirstList extends ActionBarActivity{
public static ListView firstL;
public static ArrayList<Friend> friendSelected= new ArrayList<>();
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation_list_layout);
context = getApplicationContext();
firstL= (ListView)findViewById(R.id.listView1);
}
}
Second Class
private class FriendSelectorAdapter extends ArrayAdapter<Friend> {
public ChatListAdapter(){
super(FirstList.context,R.layout.list_layout, FirstList.friendSelected);
}
In the second class there is a ListActivity which is displaying all my contacts and this adapter is meant to be called when one of the contacts is clicked so it will populate the listView of the first class.
Am I wrong what i am doing here? I would appreciate any answer, Thanks
Change constructor to some think like this(without any static in activity):
public ChatListAdapter(Context context, List<Friend> friendSelected){
super(context,R.layout.list_layout,friendSelected);
}

ArrayAdapter Causes ClassCastException

I have an AutoCompleteTextView inside a RelativeLayout inside a FrameLayout. I want to populate the completion list using a class declared as follows:
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
...
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
data = new ArrayList<String>();
}
...
}
I attached the adapter as follows:
AutoCompleteTextView tv = (AutoCompleteTextView) v.findViewById(R.id.editTextClient);
AutoCompleteAdapter adapter = new AutoCompleteAdapter(getActivity(), R.layout.fragment_main_right);
tv.setAdapter(adapter);
where R.layout.fragment_main_right is the enclosing FrameLayout mentioned above. When I start to input text, I get a ClassCastException with the following message: "android.widget.FrameLayout cannot be cast to android.widget.TextView". I understand this to mean that the second parameter in the ArrayAdapter constructor should be the id of something derived from a TextView. All the examples show this parameter as being the enclosing layout. Can someone clear up my confusion?
For the second parameter in your adapter constructor, pass in android.R.layout.simple_dropdown_item_1line or android.R.layout.simple_list_item_1 instead.
The hint is given away that it expects a textview by the name of the parameter: textViewResoureceId.

Accessing activity's state in CursorAdapter subclass

I'm writing a fairly complex ListView, which (among other things) requires formatting Views in each list item.
To give me full control over how the views are bound in each list item, I subclassed CursorAdapter in this manner:
public class MyAdapter extends CursorAdapter {
public final LayoutInflater mInflater;
public MyAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
final ToggleButton tButton = (ToggleButton) view.findViewById(R.id.tbutton);
tButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// start activity based on a pending intent
}
});
}
}
The issue is that my ToggleButton click listener should start an activity based on a pending intent. The pending intent is instantiated in the activity which utilises this customized adapter.
I'm aware I could have used a SimpleCursorAdapter in the main Activity with a ViewBinder so that launching the intent would only be necessary from the main Activity. But SimpleCursorAdapter is not quite right since I don't map columns straight to views.
However, the alternative I have here would suggest accessing the main Activity's data from a cursor subclass. I feel that there must be a better way to design the application.
Taking a cue from the API Demos - specifically EfficientAdapter, I have declared the CursorAdapter sublcass as an inner class of my activity.
This avoids passing the pending intent around outside of the main activity.
Source: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

Android Custom Spinner - What is the useable result?

I have a custom spinner (customized for formatting). It works fine and shows the result of the selected array item. The string array with my data is called mydata[].
I want to do something with that result - I've tried hours of changes but, it seems I don't know what the container is for the selected result - it just displays automatically. The mNumber refers to a case select in a class (it's result is based on what's passed into-it).
My question(s) - refer to the * WHAT DO I USE HERE * shown in the code's last line:
1. What is the container for the result?
2. How to access and syntax it?
Here's the code:
Thank you!
**[declared in onCreate]**
Spinner spinner = (Spinner) findViewById(R.id.Spinner01);
MySpinnerAdapter adapter = new MySpinnerAdapter(this, R.layout.mspinner, R.id.text,mydata);
spinner.setAdapter(adapter);
**[declared outside of onCreate]**
#SuppressWarnings("unchecked")
private class MySpinnerAdapter extends ArrayAdapter{
public MySpinnerAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
final TextView woof =(TextView)findViewById(R.id.TextView07);
woof.setText(String.valueOf(dogday.mNumber(*** WHAT DO I USE HERE ? ***)));
}
}
EDITED - FOR MY RESPONSE TO COMMONSWARE (too many characters for a comment box).
Thanks. You know the result that gets displayed in a spinner (by default) when something's selected - that's the piece of data I need. I want to use it as an argument for a call.
The user selects something in the spinner - The selected item will be used to make some choices in the class method (dogDay), which takes a data argument for mNumber(data) and returns a result (just like a function).
I want to do some math calcs with the result. First, I want to display what's coming back (for now) so, I'm using the dogDay.mNumber(data) as an argument for woof.text.
My question is this, How to get the piece of data (the thing the user selected in the Spinner)? How did the spinner know what to display for my selection - that's what I want? I tried using something like getSelectedItem (or whatever the it was - I can't remember just now) but, it crashes.
Is their an easier way to custom format a spinner? (and get the data) I searched hi and low for info and found only one applicable to android 1.5/later (I want a spinner with all black background and red text - I can do it via the way shown in my code using a the custom layout).
Thanks - I got a bit long winded!
EDITED - with full code
Here's a full code with the custom spinner and the call you suggested. As I mentioned, I already tried it - it only shows the first item in the list - never the selected one. I use the result of getSelectedItem as an argument for the string... I tried it from both within and outside the adapter...
package com.bt.junk;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MyMaincode extends Activity {
private static String mydata[] = {"one", "two", "three"};
int poop;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// DECLARATIONS ------------------------------------------------
Spinner spinner = (Spinner) findViewById(R.id.Spinner01);
MySpinnerAdapter adapter = new MySpinnerAdapter(
this,R.layout.custom_spinner_row,R.id.text, mydata );
spinner.setAdapter( adapter );
poop = spinner.getSelectedItemPosition();
}//end onCreate ********************************************
// METHODS, CLASSES, etc ---------------------------------------
#SuppressWarnings("unchecked") //<-- I added that
private class MySpinnerAdapter extends ArrayAdapter{
public MySpinnerAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
final TextView sayWhat = (TextView) findViewById(
R.id.TextView01);
sayWhat.setText(String.valueOf(mydata[poop]));
}//end MySpinnerAdapter
}//end class MySpinnerAdapter
}//end activity
Your question makes little sense to me. I am assuming that "I want to do something with that result" means "I want to find out the selected item's position in the array". If that assumption is correct, you can get the selected position for a Spinner by calling getSelectedItemPosition(). This will be 0 when the Spinner first appears, unless you change the position yourself.
Your code is also very strange, IMHO. The constructor of an ArrayAdapter should not be attempting to manipulate a widget.

Categories

Resources