2014-05-07 62 views
0

我是新的android(直到現在3周),我有一個問題(當然)。 我搜查了很多,發現有關這個問題的幾個主題,但沒有得到正確的答案。Android拖放,API <11

我不能在api中使用clipdata < 11所以我完全從我的代碼中刪除了該部分。 並將view.startDrag中的clipdata替換爲null。而且對於API> = 11,一切正常。 但是對於低於11的API級別,我收到錯誤消息。

我不知道爲什麼(posible dragEvent ???),因爲我沒有收到任何消息,說明我的代碼中的任何方法與API級別2.2不兼容。我的代碼是:

public class GlavnaAktivnost extends Activity { 
String linije[] = { "141 - SAMOBOR - Rakov Potok - JAGNJIĆ DOL", 
     "142 - SAMOBOR - Klake - TERIHAJI", 
     "143 - SAMOBOR - Rude - BRASLOVJE", 
     "144 - SAMOBOR - Smerovišće - LIPOVEC", 
     "145 - SAMOBOR - SLANI DOL", 
     "146 - SAMOBOR - Otruševac - VRHOVČAK", "147 - SAMOBOR - BREGANA", 
     "148 - SAMOBOR - OTOK", "149 - SAMOBOR - MEDSAVE", 
     "150 - SAMOBOR - Grdanjci - Kršlin - GORNJA VAS", 
     "150A - SAMOBOR - Grdanjci - NORŠIĆ SELO", 
     "151 - SAMOBOR - Sv. Nedelja - ZAGREB (ČRNOMEREC)", 
     "153 - SAMOBOR - Sv. Nedelja - ZAGREB (LJUBLJANICA)", 
     "155 - SAMOBOR - Sv. Nedelja - ZAGREB (AUTOBUSNI KOLODVOR)", 
     "157 - SAMOBOR - Sv. Martin - GALGOVO", 
     "158 - SAMOBOR - Cerje - BUKOVJE", 
     "159 - ZAGREB (SAVSKI MOST) - Galgovo - DREŽNIK/SV. MARTIN", 
     "160 - MALA GORICA - Kerestinec - ZAGREB (A. K.)", 
     "162 - SAMOBOR - Sv. Nedjelja - Novaki - Rakitje - ČRNOMEREC", 
     "260 - LULIĆ - Jastrebarsko - ZAGREB (SAVSKI MOST)", 
     "260A - JASTREBARSKO - Krašić - PRIBIĆ", 
     "261 - KLINČA SELA - Repišće - NOVO SELO", 
     "262 - PISAROVINA - KLINČA SELO", 
     "263 - ZAGREB (SAVSKI MOST) - Kupinečki Kraljevac - DONJA KUPČINA", 
     "264 - JASTREBARSKO - Klinča Selo - ZAGREB (A. K.)", 
     "265 - PISAROVINA - LIJEVO SREDIČKO", 
     "266 - PISAROVINA - Lučelnica - Vukomerić - DUBRANEC", 
     "267 - KLINČA SELA - BETER", "KALJE - KOSTANJEVAC - KALJE", 
     "SOŠICE - JASTREBARSKO - SOŠICE", 
     "SVETA JANJA - JASTREBARSKO - SLAVETIĆ - SVETA JANA" }; 
String favoriti[] = { "Dodaj favorit +", "", "" }; 
MyDragEventListener myDragEventListener = new MyDragEventListener(); 
ListView list1, list2; 
int i = 0; 
int pozicija; 
ArrayAdapter<String> adapter1, adapter2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.pozadina); 
    inicijalizacijaIinterface(); 

    list2.setOnItemLongClickListener(new OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 
      MyDragShadowBuilder myShadow = new MyDragShadowBuilder(view); 
      try{view.startDrag(null, // ClipData 
        myShadow, // View.DragShadowBuilder 
        linije[position], 0);// Object myLocalState 
      } 
      catch(Exception e){e.printStackTrace();} 


      pozicija = adapter2.getPosition(linije[position].toString()); 

      return true; 
     } 
    }); 
    list1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 
      if (position == 0) { 
       Intent intent = new Intent(
         "samobor.samoborcek.RasporedVoznje"); 
       intent.putExtra("LIN", 143); 
       startActivity(intent); 
      } 
     } 
    }); 
} 

private void inicijalizacijaIinterface() { 
    // TODO Auto-generated method stub 
    list1 = (ListView) findViewById(R.id.list1); 
    list2 = (ListView) findViewById(R.id.list2); 
    adapter1 = new ArrayAdapter<String>(GlavnaAktivnost.this, 
      R.layout.listview, favoriti); 

    adapter2 = new ArrayAdapter<String>(GlavnaAktivnost.this, 
      R.layout.listview, linije); 
    list2.setAdapter(adapter2); 

    list1.setAdapter(adapter1); 
    list1.setOnDragListener(myDragEventListener); 
    list2.setOnDragListener(myDragEventListener); 
    list1.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      if (MotionEvent.ACTION_MOVE == event.getAction()) 
       return true; 
      return false; 
     } 
    }); 

} 

public void paraf() { 
    // TODO Auto-generated method stub 

    if (i == 1) { 
     LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 82); 
     params3.bottomMargin = 20;// Jedna linija 
     list1.setLayoutParams(params3); 
     LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 18); 
     list2.setLayoutParams(params4); 
    } 
    if (i == 2) { 
     LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 66); 
     params3.bottomMargin = 20;// Dvije linije 
     list1.setLayoutParams(params3); 
     LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 34); 
     list2.setLayoutParams(params4); 
    } 
    if (i == 3) { 
     LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 49); 
     params5.bottomMargin = 20;// 3 linije 
     list1.setLayoutParams(params5); 
     LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
       android.view.ViewGroup.LayoutParams.MATCH_PARENT, 51); 
     list2.setLayoutParams(params6); 
    } 

} 

private static class MyDragShadowBuilder extends View.DragShadowBuilder { 
    private static Drawable shadow; 

    public MyDragShadowBuilder(View v) { 
     super(v); 
     shadow = new ColorDrawable(Color.argb(140, 102, 255, 255)); 
    } 

    @Override 
    public void onProvideShadowMetrics(Point size, Point touch) { 
     int width = getView().getWidth(); 
     int height = getView().getHeight(); 

     shadow.setBounds(0, 0, width, height); 
     size.set(width, height); 
     touch.set(width/2, height/2); 
    } 

    @Override 
    public void onDrawShadow(Canvas canvas) { 
     shadow.draw(canvas); 
    } 

} 

private class MyDragEventListener implements View.OnDragListener { 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     final int action = event.getAction(); 

     switch (action) { 
     case DragEvent.ACTION_DRAG_ENTERED: 
      if (v == list1) { 
       i++; 
       paraf(); 
      } 
      break; 
     case DragEvent.ACTION_DRAG_EXITED: 
      if (v == list1) { 
       i--; 
       paraf(); 
      } 
      break; 
     case DragEvent.ACTION_DROP: 
      // Gets the item containing the dragged data 

      // If apply only if drop on buttonTarget 
      if (v == list1 && i <= 3) { 

       favoriti[i - 1] = linije[pozicija].toString(); 

       adapter1.notifyDataSetChanged(); 

       while (pozicija + 1 < linije.length) { 
        linije[pozicija] = linije[pozicija + 1].toString(); 
        if (pozicija + 1 == linije.length) { 
         linije[pozicija] = " "; 
        } 
        pozicija++; 

       } 

       adapter2.notifyDataSetChanged(); 
      } 
      if (v != list1 || i > 3) 
       Toast.makeText(getApplicationContext(), "Nedozvoljeno!", 
         Toast.LENGTH_SHORT).show(); 

      break; 
     } 
     return true; 
    } 
} 

問題:什麼是錯的?或者你可以告訴我,如果API水平高於11做些什麼,如果不做另一件事,該怎麼做? 因爲MyDragEventListener是在任何方法之外構造的,並且如果我在例子onCreat中構造它,它不會是全局的,所以它不會在任何其他方法中可見。

我現在就解決它,我創建了2個其他活動,第一個活動是主屏幕...如果api級別是例子2.2啓動此活動(在此活動中,如果list2上的項目是長按發送數據到list1(NO DRAG AND DROP)),如果它是4.3開始其他活動(DRAG AND DROP)。

P.S.我知道,這個代碼是個瘸子,我不知道爲什麼我就是不看是石蠟()方法,我會在稍後解決它:) 謝謝 Matija

+0

請將您的代碼去掉只顯示相關部分。 – nhaarman

+0

你有什麼錯誤?請發佈logcat輸出。 – matiash

回答

0

View.OnDragListener was introduced with API level 11。嘗試創建MyDragEventListener類(源自它)的實例將在預蜂窩設備上崩潰。

您可以檢查API級別,並相應地使用android.os.Build.VERSION.SDK_INT字段執行操作。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) 
{ 
    MyDragEventListener myDragEventListener = new MyDragEventListener(); 
    ... 
} 

與View.DragShadowBuilder相同,& c。