2013-02-27 122 views
8

कोड यह रहा:एंड्रॉयड स्पिनर, onItemSelected (...) कहा जाता है नहीं किया जा रहा

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 वर्ग:

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); 
} 
} 

गतिविधि लेआउट 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> 

मैं कर रहा हूँ यह ठीक से काम करने की प्रतीक्षा कर रहा है, तो मैं डिवाइस एडाप्टर को और अनुकूलित करने जा रहा हूं।

Activity implements OnItemSelectedListener, इसलिए onItemSelected(...) का ओवरराइड, जिसे बिल्कुल भी नहीं कहा जा रहा है। LogCat में कोई त्रुटि नहीं है। Spinner गतिविधि के लेआउट xml में परिभाषित किया गया है और प्रदर्शित करता है और ठीक पॉप्युलेट करता है। इसे ठीक करने के लिए कोई सलाह बहुत अच्छी होगी।

धन्यवाद।

+1

कुछ भी नहीं चुना गया है? – njzk2

+0

'पकड़ (अपवाद ई) 'एक बहुत बुरा अभ्यास है। क्या आप इसे हटा सकते हैं और पुनः प्रयास कर सकते हैं। – m0skit0

+0

मैंने कोशिश/पकड़ लिया है, एक ही समस्या है, और कुछ भी नहीं चुना गया है या तो – user1610177

उत्तर

5

यह स्पिनर लेआउट हो सकता है, मैं देख सकता हूं कि आपने एडाप्टर के लिए ड्रॉपडाउन व्यू सेट नहीं किया है। क्या आप इस तरह अपने स्पिनर को आरंभ करने का प्रयास कर सकते हैं:

deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_item, deviceList); 
deviceSpinner.setAdapter(deviceAdapter); 
deviceSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
deviceSpinner.setOnItemSelectedListener(this); 
+0

इससे चीजों में थोड़ा सुधार हुआ है (स्पिनर के दृश्य में जब इसे चुना जाता है, हालांकि इटैम पर अभी भी – user1610177

+0

नहीं कहा जाता है क्या आपने ArrayAdapter की getView() विधि को ओवरराइड किया है? यदि हां, तो आपको केवल super.getView () आपके परीक्षण के लिए यह सुनिश्चित करने के लिए कि यह कारण नहीं है। बीटीडब्लू - क्या करता है Item चयनित जब आप डिफ़ॉल्ट ArrayAdapter का उपयोग करते हैं तो कॉल किया जाता है? – GTware

+0

मैंने getView() विधि को ओवरराइड नहीं किया है, और ItemSlected को डिफ़ॉल्ट ArrayAdapter का उपयोग करते समय नहीं कहा जाता है (जो था मुख्य कारण मैंने अपना खुद का निर्माण क्यों किया) – user1610177