2015-09-19 90 views
0

我正在嘗試使用https://github.com/bauerca/drag-sort-listview來合併拖放操作,我試圖編寫一個應用程序。我不知道我的錯誤是如何導入DragSortListView或我的代碼。DragSortListView - Android Studio無法識別dropListener和removeListener

我最初通過使用maven central來導入庫,並且在模塊:app的build.gradle中添加了依賴編譯「asia.itivity.android:drag-sort-listview:1.0」。我遇到的問題是,在我調用listItems.setDropListener(onDrop)和listItems.setRemoveListener(onRemove)時,我將在下面列出的代碼中,Android Studio無法解析符號'.setDropListener'和'setRemovelistener,但它確實識別其他所有相關內容拖動排序列表視圖。我遵循此示例Bauerca drag-sort-listview simple example以及存儲庫本身中的示例。

我改變主要活動擴展ActionBarActivity擴展ListActivity沒有區別。我使用安裝在我的計算機上的git gui(這對我來說是全新的)來將git存儲庫https://github.com/JayH5/drag-sort-listview克隆到我的計算機上的一個文件夾中,然後使用Android Studio將/ demo和/ library作爲模塊添加到我的項目中,並添加了庫作爲我的主要模塊應用程序的依賴,但我仍然得到錯誤。我也嘗試添加演示作爲依賴項,但出現錯誤,因此我刪除了該依賴項。我非常感謝任何幫助!我最初嘗試使用ListViewAnimations庫,但無法讓它工作(stableId問題)。

非常感謝!泰倫

package dtcj.bandtasker; 
 

 
import android.app.Activity; 
 
import android.app.AlertDialog; 
 
import android.app.ListActivity; 
 
import android.content.DialogInterface; 
 
import android.support.v7.app.ActionBarActivity; 
 
import android.os.Bundle; 
 
import android.view.Menu; 
 
import android.view.MenuItem; 
 
import android.view.View; 
 
import android.widget.ArrayAdapter; 
 
import android.widget.EditText; 
 
import com.mobeta.android.dslv.DragSortListView; 
 
import org.apache.commons.io.FileUtils; 
 
import java.io.File; 
 
import java.io.IOException; 
 
import java.util.ArrayList; 
 

 

 
public class MainActivity extends ListActivity { 
 
    //create list of trigger phrases 
 
    private ArrayList<String> items; 
 
    private ArrayAdapter<String> itemsAdapter; 
 

 

 
    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() 
 
    { 
 
     @Override 
 
     public void drop(int from, int to) 
 
     { 
 
      if (from != to) 
 
      { 
 
       String item = itemsAdapter.getItem(from); 
 
       itemsAdapter.remove(item); 
 
       itemsAdapter.insert(item, to); 
 
      } 
 
     } 
 
    }; 
 

 
    private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() 
 
    { 
 
     @Override 
 
     public void remove(int which) 
 
     { 
 
      String item = itemsAdapter.getItem(which); 
 
      itemsAdapter.remove(item); 
 
     } 
 
    }; 
 

 

 
    @Override 
 
    public DragSortListView getListView() { 
 
     return (DragSortListView) super.getListView(); 
 
    } 
 
    //Methods to read and write user entered items to the data file 
 

 
    private void readItems() { 
 
     File filesDir = getFilesDir(); 
 
     File todoFile = new File(filesDir, "triggers.txt"); 
 
     try { 
 
      items = new ArrayList<String>(FileUtils.readLines(todoFile)); 
 
     } catch (IOException e) { 
 
      items = new ArrayList<String>(); 
 
     } 
 
    } 
 

 
    private void writeItems() { 
 
     File filesDir = getFilesDir(); 
 
     File todoFile = new File(filesDir, "triggers.txt"); 
 
     try { 
 
      FileUtils.writeLines(todoFile, items); 
 
     } catch (IOException e) { 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 

 
     items = new ArrayList<String>(8); 
 
     readItems(); 
 
     itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, 
 
       items); 
 
     listItems.setAdapter(itemsAdapter);} 
 
     DragSortListView listItems = (DragSortListView) getListView(); 
 
    //Problem is here 
 
     listItems.setDropListener(onDrop); 
 
     listItems.setRemoveListener(onRemove); 
 
    //TO DO 
 
    // Action Bar- "How to use", About 
 

 

 
    //Add phrases to list 
 
    public void onAddItem(View v) { 
 
     //check that there are fewer than 8 trigger phrases in the array 
 
     if (items.size() <= 7){ 
 
      EditText getNewItem = (EditText) findViewById(R.id.getNewItem); 
 
      String itemText = getNewItem.getText().toString(); 
 
      itemsAdapter.add(itemText); 
 
      getNewItem.setText(""); 
 
      writeItems();} 
 
     else{ 
 
      //Warn user they have reached maximum number of Trigger Phrases 
 
      // Creating alert Dialog with one Button 
 

 
      final AlertDialog.Builder maxAlert = new AlertDialog.Builder(this); 
 
      maxAlert.setMessage("Sorry! Eight is the maximum number of trigger phrases. Please delete a phrase before adding a new one.") 
 
        .setCancelable(false) 
 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
 
         public void onClick(DialogInterface dialog, int id) { 
 
          //do things 
 

 
         } 
 
        }); 
 

 
      // Showing Alert Message 
 
      maxAlert.show(); 
 

 

 
     } 
 
    } 
 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
 
     return true; 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 

 
     //noinspection SimplifiableIfStatement 
 
     if (id == R.id.action_settings) { 
 
      return true; 
 
     } 
 

 
     return super.onOptionsItemSelected(item); 
 
    } 
 
}

的build.gradle的模塊:應用

defaultConfig { 
 
     applicationId "dtcj.bandtasker" 
 
     minSdkVersion 17 
 
     targetSdkVersion 22 
 
     versionCode 1 
 
     versionName "1.0" 
 
    } 
 
    buildTypes { 
 
     release { 
 
      minifyEnabled false 
 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
 
     } 
 
    } 
 
} 
 
repositories { 
 
    mavenCentral() 
 
} 
 
dependencies { 
 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
 
    compile 'com.android.support:appcompat-v7:22.2.1' 
 
    compile 'org.apache.commons:commons-io:1.3.2' 
 
    compile files('libs/microsoft-band-1.3.10622.3.jar') 
 
    compile 'com.nineoldandroids:library:2.4.0' 
 
    compile project(':library') 
 
}

settings.gradle

include ':app', ':demo', ':library' 

回答

0

它看起來像是一個Android工作室錯誤的問題。我試圖使無效緩存/重新啓動,刪除settings.gradle,以及我發現堆棧溢出的各種建議。最後,我剛剛打開了一個新的工作室項目,並複製了我鏈接到上面的示例代碼,並根據需要更改了變量名稱和佈局,並使用maven central將庫添加爲依賴項。新工程中的Android Studio沒有標記這些方法。