android - 尝试在 CWAC Touchlist 的演示中复制代码并在 Android 中出现错误

标签 android commonsware-cwac

我已经使用 Java 以外的语言进行了 10 多年的开发。我是 Android 世界的新手,但我正在尝试直接进入。我正在尝试制作一个可拖动的列表,我发现了很棒的 repo https://github.com/commonsguy/cwac-touchlist拖放列表项。但是,当我尝试 @Override DropListener 接口(interface)的 drop 方法时出现错误。它指出: “新 TouchListView.DropListener(){} 类型的方法 drop(int, int) 必须覆盖父类(super class)方法”

我试图将演示利用 TouchList 的方式实现到我的应用程序中,并且我还在另一个 Action 中完全复制了代码,但我遇到了同样的错误。

我的代码:

package bu.homework.shoppinglist;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.commonsware.cwac.tlv.TouchListView;

public class ItemListActivity extends ListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private ItemsModel mDbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_list);
        mDbHelper = new ItemsModel(this);
        mDbHelper.open();
        fillList();
        TouchListView itemListView = (TouchListView) getListView();
        itemListView.setDropListener(onDrop);
        System.out.print("On Drop");
        // itemListView.setRemoveListener(onRemove);
        // registerForContextMenu(itemListView);
    }

   private TouchListView.DropListener onDrop = new TouchListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            System.out.print(from);
            System.out.print(to);
        }
    };

    private TouchListView.RemoveListener onRemove = new TouchListView.RemoveListener() {
        public void remove(int which) {

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createItem();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case INSERT_ID:
            createItem();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                mDbHelper.deleteItem(info.id);
                fillList();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ItemEditActivity.class);
        i.putExtra(ItemsModel.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillList();
    }

    private void createItem() {
        Intent i = new Intent(this, ItemEditActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    private void fillList() {
        // Get all of the notes from the database and create the item list
        Cursor itemCursor = mDbHelper.fetchAllItems();
        startManagingCursor(itemCursor);

        // Create an array to specify the fields we want to display in the list (only NAME)
        String[] from = new String[] { ItemsModel.KEY_NAME };

        // and an array of the fields we want to bind those fields to (in this case just item_row)
        int[] to = new int[] { R.id.item_row };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.item_row, itemCursor, from, to);
        setListAdapter(notes);
    }

}

代码尚未完成,但我的目标是将记事本的 android 教程的功能与触摸列表相结合。

我的问题是,我做错了什么。还有没有更好的方法来拖放列表。

谢谢, 克里斯·斯文诺

最佳答案

您可能正在使用 Java 1.5 进行构建。在 Java 1.5 中,您不能将 @Override 与接口(interface)方法定义一起使用。他们在 Java 1.6 中采用了这一点。

However for some reason my drop listener isn't getting triggered.

除了我不是 System.out.print() 的 super 粉丝之外,我没有想到您哪里出了问题。您可以尝试使用断点或 Log.w() 或其他方式来查看问题是否只是在日志记录中。

关于android - 尝试在 CWAC Touchlist 的演示中复制代码并在 Android 中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728437/

相关文章:

android - ListView 项目排序/重新排序并保存更改?

android - 从 MergeAdapter 中删除 View

android - 检测视频文件何时真正写入?

javascript - 如何在 Android 上以 Angular 获取按键事件?

javascript - Android 中的 Uri.encode() 是否等同于 Javascript 中的 encodeURIComponent()?

android - 文件 google-services.json 丢失

android - Flutter:前台通知在 Firebase 消息传递中不起作用

android - Flutter:为什么我会收到 "Vertical viewport was given unbounded height"错误?

android - 在 android 中向 Commonsware RichEditText 添加自定义控件

android - 使用 sqlitecursorloader 从 Intentservice 插入 SQLite