我正在尝试编写一个应用程序,初始部分允许用户输入最多8个单词或短语的列表,用于触发任务。它成功测试到目前为止,但现在我正在尝试使用库ListViewAnimations来允许用户重新排序列表并通过滑动删除项目。我已经参加了codeacademy java课程和edx介绍到android编程课程,但我主要是通过谷歌搜索和查看教程与示例。 这就是我的原始代码:
package dtcj.bandtasker;
import android.app.Activity;
import android.app.AlertDialog;
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 android.widget.ListView;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
//create list of trigger phrases
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView listItems;
//Methods to read and write user entered items to the data file
private void readItems() {
File filesDir = getFilesDir();
File triggerFile = new File(filesDir, "trigger.txt");
try {
items = new ArrayList<String>(FileUtils.readLines(triggerFile));
} catch (IOException e) {
items = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir();
File triggerFile = new File(filesDir, "trigger.txt");
try {
FileUtils.writeLines(triggerFile, items);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = (ListView) findViewById(R.id.listItems);
items = new ArrayList<String>(8);
readItems();
itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
items);
listItems.setAdapter(itemsAdapter);}
//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();
}
}
我不需要图书馆提供的外观动画,但当我看到一个无法获得拖放工作的人的帖子说他们需要首先需要包含外观动画的代码是这个
MyAdapter myAdapter = new MyAdapter();
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
animationAdapter.setAbsListView(mListView);
mListView.setAdapter(animationAdapter);
我试图这样做,但如果我使用
import com.nhaarman.listviewanimations.ArrayAdapter;
代替import android.widget.Array Adapter
并尝试将代码更改为此
itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
listItems.setAdapter(itemsAdapter);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(this.itemsAdapter);
animationAdapter.setAbsListView(listItems);
listItems.setAdapter(animationAdapter);
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.nhaarman.listviewanimations.ArrayAdapter;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter;
import android.widget.Adapter;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import android.widget.ListAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.DynamicListView;
非常感谢有关如何纠正此错误的任何建议。我确信它源于我对java / android的理解中的一些漏洞。
谢谢! 泰伦
答案 0 :(得分:0)
Java中有许多类型的类。根据错误,似乎ArrayAdapter是一个抽象类,并且抽象类不能直接实例化。您需要使用扩展ArrayAdapter的类初始化您的itemsAdapter。
您可以做的一件事是创建自己的ItemsAdapter类,该类扩展ArrayAdapter,实现所需的方法,然后使用您创建的类初始化itemsAdapter。