2016-07-25 119 views
1

爲什麼OnItemSelected不起作用到array adapter發佈onItemSelected

我在list view的每一行有spinner。 這是我的代碼:

package info.androidhive.materialtabs.activity; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 

import info.androidhive.materialtabs.R; 

/** 
* Created by marco on 24/07/16. 
*/ 

    public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener { 
     Spinner spinner; 

     /*public MyListAdapter(Context context, int textViewResourceId) { 
      super(context, textViewResourceId); 
     }*/ 

     public MyListAdapter(Context context) { 
      super(context, R.layout.single_listview_item); 
     } 





      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View row = convertView; 
       System.out.println("ciao"); 

       spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner); 
       ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
         getContext(), 
         R.array.country_arrays, 
         android.R.layout.simple_spinner_item); 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       spinner.setAdapter(adapter); 
       spinner.setOnItemSelectedListener(this); 

    /* 
       spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    */ 
       /*  @Override 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
         //Spinner spinner = null; 
         // String selState = (String) ((Spinner) parent).getSelectedItem(); 

         spinner.setSelection(position); 
         String selState = (String) (parent).getSelectedItem(); 
         Toast.makeText(
           parent.getContext(), 
           "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onNothingSelected(AdapterView<?> parent) { 

        } 


       });*/ 



       return row; 
      } 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      spinner.setSelection(position); 
      String selState = (String) (parent).getSelectedItem(); 
      Toast.makeText(
        parent.getContext(), 
        "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 


     /* @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      String selState = (String) spinner.getSelectedItem(); 
      System.out.println(selState); 
      Toast.makeText(
        getContext(), 
        "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     }*/ 

     } 

片斷:

listView = (ListView) rootView.findViewById(R.id.listview); 
     ListAdapter listAdapter = new MyListAdapter(getContext()); 
     listView.setAdapter(listAdapter); 
+0

希望該列表是空的。越來越顯示? – sJy

+0

列表視圖中的每一行中的列表視圖我看到飛旋,我可以選擇,但是當我選擇了一個對象,吐司沒有出現...所以方法不起作用 –

+0

它是適配器的完整代碼嗎?它缺少一些重要的覆蓋方法,如getCount()。 – sJy

回答

0

剛剛嘗試這一點在你的OnItemSelected方法....

變化

String selState = (String) (parent).getSelectedItem();  

String selState = spinner.getSelectedItem().toString(); 
+0

不起作用.... –

+0

你能幫助我嗎? –

0

嘗試在getView方法中使用此方法,而不是onItemSelected方法。你的onItemSelected方法是用於listview的。

spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){

@Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

     String selState = spinner.getSelectedItem().toString(); 
     Toast.makeText(
       parent.getContext(), 
       "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 

    } 

}); 
+0

不起作用...... –