Android - Spinner, onItemSelected(...) not being called - android

Here's the code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.project.locationapp.model.Device;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class SelectDevice extends Activity implements OnItemSelectedListener {
private Spinner deviceSpinner;
private List<Device> deviceList;
private ArrayAdapter<Device> deviceAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_device);
deviceList = new ArrayList<Device>();
try {
deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_dropdown_item, deviceList);
deviceSpinner.setAdapter(deviceAdapter);
deviceSpinner.setOnItemSelectedListener(this);
DataAsyncTask loadDevices = new DataAsyncTask();
loadDevices.execute(new String[] { WebServiceURL.WEB_SERVICE + WebServiceURL.DEVICES + WebServiceURL.ALL });
} catch (Exception e){
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
#Override
public void onItemSelected(AdapterView<?> a, View v, int position,
long id) {
Log.d(TAG, "called!");
Intent intent = new Intent(this, ViewTrips.class);
intent.putExtra("device_id", deviceAdapter.getItem(position).getId());
startActivity(intent);
}
}
DeviceAdapter class:
import java.util.List;
import com.project.locationapp.model.Device;
import android.content.Context;
import android.widget.ArrayAdapter;
public class DeviceAdapter extends ArrayAdapter<Device> {
public DeviceAdapter(Context context, int textViewResourceId,
List<Device> objects) {
super(context, textViewResourceId, objects);
}
}
Activity layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectDevice" >
<TextView
android:id="#+id/instructions_device_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/select_device_instructions"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp" />
<Button
android:id="#+id/start_service_button"
android:layout_below="#id/instructions_device_select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="thisDevice"
android:text="#string/this_device"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp" />
<Spinner
android:id="#+id/select_device_spinner"
android:layout_below="#id/start_service_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_device"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:drawSelectorOnTop = "true" />
</RelativeLayout>
I am waiting to get this working properly, then I am going to customise DeviceAdapter further.
The Activity implements OnItemSelectedListener, hence the override of onItemSelected(...), which isn't being called at all. No errors in LogCat. Spinner is defined in the Activity's layout xml and displays and populates fine. Any advice to fix this would be great.
Thank you.

It might be the spinner layouts, I can see you didn't set a dropdown view for the adapter.
Could you try to initialize your spinner like this:
deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_item, deviceList);
deviceSpinner.setAdapter(deviceAdapter);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceSpinner.setOnItemSelectedListener(this);

I was facing the same problem today. Then I realized I am testing on emulator. When I tested the same app it worked. I am posting this if someone is trying to get call back for spinner over emulator it did not work for me. you may also check the same. It works on real device

Related

onListItemClick not being invoked

I am following a online course and I can across this exercise that for some reason is not working. The onListItemClick() is not being invoked. I have tried troubleshooting this but I couldn't get rid of the issue
Please help me solve this issue.
My code for MainActivity.java is
package hk.ust.cse.comp107x.greetfriend;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity{
String names[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
names=getResources().getStringArray(R.array.friends);
//setListAdapter((ListAdapter)new ArrayAdapter<String>(this,R.layout.friend_item,names));
setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent in=new Intent(this,ShowMessage.class);
in.putExtra("message","Good Day "+names[(int) id]+"!!");
startActivity(in);
}
}
Code for ShowMessage.java is
package hk.ust.cse.comp107x.greetfriend;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowMessage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_message);
Intent in=getIntent();
String message=in.getStringExtra("message");
TextView textMessage=(TextView)findViewById(R.id.textMessage);
textMessage.setText(message);
}
}
My code for friend.xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:autoText="true"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textview">
</TextView>
And finally my code for strings.xml is
<resources>
<string name="app_name">GreetFriend</string>
<string-array name="friends">
<item>John</item>
<item>Paul</item>
<item>George</item>
<item>Ringo</item>
</string-array>
</resources>
Use the following code for your Friend.xml and run the code...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textviewfriend">
</TextView>
</LinearLayout>

EditText with history

I want to show the history of text introduced in a TextView. It should be something similar to the search bar on google play:
By the moment I'm using an AutoCompleteTextView with android:completionThreshold="1", but I would like to don't need to write any text before suggestions starts.
Any ideas??
Try this its working fine:
MainActivity.java
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
// In the onCreate method
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>

Android - adapter code does not compile

I have some code which I think should compile, but doesn't:
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.problemio.ViewSolutionsActivity.DownloadWebPageTask;
import com.problemio.data.Discussion;
import com.problemio.data.DiscussionMessage;
import com.problemio.data.SuggestedSolution;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TopicActivity extends Activity
{
ArrayAdapter<Discussion> adapter;
ArrayList<Discussion> discussion = new ArrayList <Discussion>( );
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discussion);
// Have to display the topic, and the existing discussion, and the form field.
Discussion d = new Discussion ();
d.setDiscussionTopicName( "Please wait while the discussion comments load" );
discussion.add(d);
adapter = new ArrayAdapter<Discussion>( this,R.layout.discussion_comments, discussion);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
There is more code below but the line with setListAdapter(adapter); gives this error:
The method setListAdapter(ArrayAdapter<Discussion>) is undefined for the type TopicActivity
Any idea why? I actually copied this code from another class and it worked well there.
Thanks!
You can only user setListAdapter() in a ListActivity
I think this should work extends your class to ListActivity.
You need to extend your activity with the ListActivity. Right now you have
public class TopicActivity extends Activity
Change it to
public class TopicActivity extends ListActivity
setListAdapter(adapter) works with
ListActivity or
if the layout has atleast one list view with id android.R.id.list

Java null pointer exception while using google maps in Android

package com.crumbin.tabs;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.client.HttpClient;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.webkit.WebView;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class ExploreActivity extends MapActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(com.app.main.R.layout.user_main_tab_explore);
MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView);
mv.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="Mykey"/>
</LinearLayout>
This code gives me a java null pointer exception. I debugged it and found out that Mapview mv is null.
Shouldn't this code just display the map and no overlay/data on it? Or am I missing something here?
This line
MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView);
needs to be
MapView mv = (MapView)findViewById(R.id.myMapView);
To explain more, you are requesting an ID of a View that is on the R class in the package com.app.main, when you need to request it from the R class in your own package, which can be omitted.

android call log

With the following codes, the call log displayed is empty.Why is that soo??
package fypj.c;
import java.security.Provider;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class c extends ListActivity {
private SimpleCursorAdapter myAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] column = new String[] {android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.DURATION, android.provider.CallLog.Calls.TYPE};
int[] names = new int[] {R.id.CLName, R.id.CLDuration, R.id.CLType};
myAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, cursor, column, names);
setListAdapter(myAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLName"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLType"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLDuration"
/>
</LinearLayout>
According to SimpleCursorAdapter,
"This constructor is deprecated.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader. "
Try an alternative to rule out any problem with your code.

Categories

Resources