【发布时间】:2015-12-16 21:50:54
【问题描述】:
我正在尝试使用 https://github.com/bauerca/drag-sort-listview 为我正在尝试编写的应用程序合并拖放功能。我不确定我的错误是在我如何导入 DragSortListView 还是在我的代码中。
我最初只使用 maven Central 导入库,并将依赖项 compile 'asia.itivity.android:drag-sort-listview:1.0' 添加到我的 build.gradle for module:app。我遇到的问题是,在我的代码中,当我调用 listItems.setDropListener(onDrop) 和 listItems.setRemoveListener(onRemove) Android Studio 无法解析符号“.setDropListener”和“setRemovelistener”时,我将在下面列出,但它确实可以识别所有其他相关内容到拖动排序列表视图。我遵循了这个示例Bauerca drag-sort-listview simple example 以及存储库本身中的示例。
我将 Main Activity extends ActionBarActivity 更改为 extends ListActivity ,这没有任何区别。我使用安装在计算机上的 git gui 将 git 存储库 https://github.com/JayH5/drag-sort-listview 克隆到计算机上的文件夹(这对我来说都是全新的),然后使用 Android Studio 我将 /demo 和 /library 作为模块添加到我的项目中,然后将库添加为我的主模块应用程序的依赖项,但我仍然收到错误消息。我还尝试将 demo 添加为依赖项,但出现错误,因此我删除了该依赖项。我真的很感激任何帮助!我最初尝试使用 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 for module:app
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'
【问题讨论】:
标签: android listview android-listview