2015-10-10 15 views
5

Sto cercando di creare un raccoglitore di ruote come this Ho provato a scaricare questo progetto ma gli unici file inclusi nel download .zip sono wheel-demo.apk e notes.txt. Notes.txt non ha istruzioni su come utilizzare questo file con Android Studio. Ho trovato un post che suggeriva di usare ListViews per creare lo stesso effetto. Invece di fare il mio ho passato un altro giorno a cercare su Internet e ho trovato il codice sorgente here ma quando ho importato i file nel mio progetto l'IDE ha mostrato dozzine di errori. Attraverso tentativi ed errori sono riuscito a correggere tutti tranne 3 errori. Abbastanza sicuro ho messo il codice in questione sotto
MainActivity.java:Come fare un raccoglitore di ruote

OnWheelScrollListener scrolledListener = new OnWheelScrollListener() 
{ 
    public void onScrollingStarted(WheelView wheel) 
    { 
     wheelScrolled = true;// "Cannot resolve symbol wheelScrolled 
    } 

    public void onScrollingFinished(WheelView wheel) 
    { 
     wheelScrolled = false;// "Cannot resolve symbol wheelScrolled 
     updateStatus(); 
    } 
}; 

// Wheel changed listener 
private final OnWheelChangedListener changedListener = new OnWheelChangedListener() 
{ 
    public void onChanged(WheelView wheel, int oldValue, int newValue) 
    { 
     if (!wheelScrolled)// "Cannot resolve symbol wheelScrolled 
     { 
      updateStatus(); 
     } 
    } 
}; 
private void initWheel1(int id) 
{ 
    WheelView wheel = (WheelView) findViewById(id); 
    wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); //cannot resolve method 'setAdapter(com.projectname.ArrayWheelAdapter<Java.lang.String>) 
    wheel.setVisibleItems(2); 
    wheel.setCurrentItem(0); 
    wheel.addChangingListener(changedListener); 
    wheel.addScrollingListener(scrolledListener); 
} 

OnWheelScrollListener.java:

public interface OnWheelScrollListener { 
/** 
* Callback method to be invoked when scrolling started. 
* @param wheel the wheel view whose state has changed. 
*/ 
void onScrollingStarted(WheelView wheel); 

/** 
* Callback method to be invoked when scrolling ended. 
* @param wheel the wheel view whose state has changed. 
*/ 
void onScrollingFinished(WheelView wheel);} 

OnWheelChangedListener.java:

public interface OnWheelChangedListener { 
    /** 
    * Callback method to be invoked when current item changed 
    * @param wheel the wheel view whose state has changed 
    * @param oldValue the old value of current item 
    * @param newValue the new value of current item 
    */ 
    void onChanged(WheelView wheel, int oldValue, int newValue); 
} 

ArrayWheelAdapter.java

public class ArrayWheelAdapter<T> extends AbstractWheelTextAdapter { 

    // items 
    private T items[]; 

    /** 
    * Constructor 
    * @param context the current context 
    * @param items the items 
    */ 
    public ArrayWheelAdapter(Context context, T items[]) { 
     super(context); 

     //setEmptyItemResource(TEXT_VIEW_ITEM_RESOURCE); 
     this.items = items; 
    } 

    @Override 
    public CharSequence getItemText(int index) { 
     if (index >= 0 && index < items.length) { 
      T item = items[index]; 
      if (item instanceof CharSequence) { 
       return (CharSequence) item; 
      } 
      return item.toString(); 
     } 
     return null; 
    } 

    @Override 
    public int getItemsCount() { 
     return items.length; 
    } 
} 

Tutti i 3 file .java sono stati aggiunti all'elenco di importazione in MainActivity pensando che potrebbe risolvere il problema, ma non è stato così. Grazie per il consiglio finora.

+0

Hai provato in quel modo? –

+0

Cosa intendi in questo modo? il codice nel post è per quanto ho ottenuto prima di imbattersi nel problema di scorrimento che ho menzionato. Android Studio non ha potuto importare nulla dai file che ho estratto da .zip che ho scaricato. –

+0

Puoi esportare l'intero progetto su https://code.google.com/p/android-wheel/ su GitHub usando il pulsante "Esporta su GitHub". Ciò semplifica l'importazione in Android Studio. – Kuffs

risposta

7

Prova questo

public class MainActivity extends Activity 
    { 
     // TODO: Externalize string-array 
     String wheelMenu1[] = new String[]{"name 1", "name 2", "name 3", "name 4", "name 5", "name 6","name 7","name 8","name 9"}; 
     String wheelMenu2[] = new String[]{"age 1", "age 2", "age 3"}; 
     String wheelMenu3[] = new String[]{"10", "20","30","40","50","60"}; 

     // Wheel scrolled flag 
     private boolean wheelScrolled = false; 

     private TextView text; 
     private EditText text1; 
     private EditText text2; 
     private EditText text3; 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 

       initWheel1(R.id.p1); 
       initWheel2(R.id.p2); 
       initWheel3(R.id.p3); 

       text1 = (EditText) this.findViewById(R.id.r1); 
       text2 = (EditText) this.findViewById(R.id.r2); 
       text3 = (EditText) this.findViewById(R.id.r3); 
       text = (TextView) this.findViewById(R.id.result); 
      } 

     // Wheel scrolled listener 
     OnWheelScrollListener scrolledListener = new OnWheelScrollListener() 
      { 
       public void onScrollStarts(WheelView wheel) 
        { 
         wheelScrolled = true; 
        } 

       public void onScrollEnds(WheelView wheel) 
        { 
         wheelScrolled = false; 
         updateStatus(); 
        } 
      }; 

     // Wheel changed listener 
     private final OnWheelChangedListener changedListener = new OnWheelChangedListener() 
      { 
       public void onChanged(WheelView wheel, int oldValue, int newValue) 
        { 
         if (!wheelScrolled) 
          { 
           updateStatus(); 
          } 
        } 
      }; 

     /** 
     * Updates entered PIN status 
     */ 
     private void updateStatus() 
      { 
       text1.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()]); 
       text2.setText(wheelMenu2[getWheel(R.id.p2).getCurrentItem()]); 
       text3.setText(wheelMenu3[getWheel(R.id.p3).getCurrentItem()]); 

       text.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()] + " - " + wheelMenu2[getWheel(R.id.p2).getCurrentItem()] + " - " + wheelMenu3[getWheel(R.id.p3).getCurrentItem()]); 
      } 

     /** 
     * Initializes wheel 
     * 
     * @param id 
     *   the wheel widget Id 
     */ 

     private void initWheel1(int id) 
      { 
       WheelView wheel = (WheelView) findViewById(id); 
       wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); 
       wheel.setVisibleItems(2); 
       wheel.setCurrentItem(0); 
       wheel.addChangingListener(changedListener); 
       wheel.addScrollingListener(scrolledListener); 
      } 

     private void initWheel2(int id) 
      { 
       WheelView wheel = (WheelView) findViewById(id); 
       wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu2)); 
       wheel.setVisibleItems(2); 
       wheel.setCurrentItem(0); 
       wheel.addChangingListener(changedListener); 
       wheel.addScrollingListener(scrolledListener); 
      } 

     private void initWheel3(int id) 
      { 
       WheelView wheel = (WheelView) findViewById(id); 

       wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu3)); 
       wheel.setVisibleItems(2); 
       wheel.setCurrentItem(0); 
       wheel.addChangingListener(changedListener); 
       wheel.addScrollingListener(scrolledListener); 
      } 

     /** 
     * Returns wheel by Id 
     * 
     * @param id 
     *   the wheel Id 
     * @return the wheel with passed Id 
     */ 
     private WheelView getWheel(int id) 
      { 
       return (WheelView) findViewById(id); 
      } 

     /** 
     * Tests wheel value 
     * 
     * @param id 
     *   the wheel Id 
     * @param value 
     *   the value to test 
     * @return true if wheel value is equal to passed value 
     */ 
     private int getWheelValue(int id) 
      { 
       return getWheel(id).getCurrentItem(); 
      } 
    } 

main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@drawable/layout_bg"> 


    <LinearLayout 
     android:layout_marginTop="24dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <com.example.wheel.WheelView 
      android:id="@+id/p1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" /> 
     <com.example.wheel.WheelView 
      android:id="@+id/p2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" /> 
     <com.example.wheel.WheelView 
      android:id="@+id/p3" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp"> 
     <EditText 
      android:id="@+id/r1" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      android:layout_gravity="center_horizontal" 
      android:textSize="18sp" 
      android:textColor="#000"> 
     </EditText> 
     <EditText 
      android:id="@+id/r2" 
      android:layout_width="80dip" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      android:layout_gravity="center_horizontal" 
      android:textSize="18sp" 
      android:textColor="#000"> 
     </EditText> 
     <EditText 
      android:id="@+id/r3" 
      android:layout_width="80dip" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      android:layout_gravity="center_horizontal" 
      android:textSize="18sp" 
      android:textColor="#000"> 
     </EditText> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/result" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:layout_gravity="center_horizontal" 
     android:textSize="18sp" 
     android:textColor="#FFF" 
     android:text="Your choice"> 
    </TextView> 
</LinearLayout> 

anche provare questo demo

enter image description here

+0

Grazie per aver fornito del codice, cercherò di farlo funzionare nel progetto. Una cosa però, sembra che dipenda dal codice importato. Non sono riuscito a ottenere il progetto demo della ruota che ho scaricato per funzionare, speriamo che questo codice non dipenda da quel progetto. –

+0

C'era molto "impossibile risolvere il simbolo" simbolo casuale "" Supponevo che fosse dovuto al fatto che il raccoglitore di ruote non era stato importato correttamente nel progetto. trovato questo tutorial http://tolkianaa.blogspot.mx/2012/03/do-not-try-to-reinvent-wheel.html. Non è stato utile per ottenere i file giusti importati nel mio progetto Android Studio. –

+0

ok, finalmente ho preso la ruota di GitHub che ha funzionato nel mio progetto e tutti tranne una riga nella casella di codice superiore funziona. la linea: wheel.setAdapter (nuovo ArrayWheelAdapter (wheelMenu1)); ho esaminato il mio progetto per ore e ora non riesco a capire perché questa riga di codice mostra errori IDE –

23

Si può semplicemente utilizzare il default NumberPicker incluso in Android dal API 11. È può visualizzare qualsiasi Strin gs che vuoi usando NumberPicker # setDisplayedValues.

XML:

<NumberPicker 
     android:id="@+id/number_picker" 
     android:layout_width="match_parent" 
     android:layout_height="180dp" /> 

Java:

String[] data = new String[]{"Berlin", "Moscow", "Tokyo", "Paris"}; 
    picker.setMinValue(0); 
    picker.setMaxValue(data.length-1); 
    picker.setDisplayedValues(data); 

Screenshot:

NumberPicker displaying Cities

+0

Grazie per il suggerimento. Proverò in questo modo la prossima volta. –

+2

[Aggiunto in livello API 11] (https://developer.android.com/reference/android/widget/NumberPicker.html). –

+0

@ Mir-Ismaili Grazie. Aggiustato. – plexus

Problemi correlati