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

发布于 2024-12-09 06:34:46 字数 5070 浏览 0 评论 0 原文

我使用 Java 以外的语言进行开发已有 10 多年了。我是 Android 世界的新手,但我正在尝试直接投入。我正在尝试制作一个可拖动列表,我发现了精彩的存储库 https://github.com/commonsguy/cwac-touchlist 来拖放列表项。但是,当我尝试 @Override DropListener 接口的 drop 方法时,出现错误。它指出: “new TouchListView.DropListener(){} 类型的方法 drop(int, int) 必须重写超类方法”

我尝试将演示使用 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 教程的功能与触摸列表结合起来。

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

谢谢, 克里斯·斯韦纳

I have been developing for over 10 years in languages other than Java. I am new to the Android world, but I'm trying to dive right in. I am trying to make a draggable list and I found the wonderful repos https://github.com/commonsguy/cwac-touchlist to do the dragging and dropping of the list items. However, I am getting an error when I am trying to @Override the drop method for the DropListener interface. It states that:
"The method drop(int, int) of type new TouchListView.DropListener(){} must override a superclass method"

I tried to implement the way the demo utilizes the TouchList into my application, and I also copied the code exactly in another Action, and I'm getting the same error.

My code:

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);
    }

}

The code isn't near complete, but my goal is to combine the functionality of the android tutorial for the notepad with the touchlist.

My question is, what am I doing wrong. Also is there a better way to do drag and drop lists.

Thanks,
Chris Swenor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-12-16 06:34:46

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

但是由于某种原因我的放置侦听器没有被触发。

除了我不是 System.out.print() 的忠实粉丝之外,我并没有意识到你哪里出了问题。您可以尝试使用断点或 Log.w() 或其他方法来查看问题是否仅存在于日志记录中。

You may be building using Java 1.5. In Java 1.5, you cannot use @Override with interface method definitions. They adopted that with Java 1.6.

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

Nothing leaps out at me as to where you are going wrong, other than I'm not a huge fan of System.out.print(). You might try using breakpoints or Log.w() or something instead to see if the issue is simply in the logging.

能否归途做我良人 2024-12-16 06:34:46

它抱怨是因为您试图覆盖不存在的方法。我认为该方法可能称为 onDrop,而不是 drop - 更改它并查看它是否可以编译。另外,如果您使用的是 eclipse,请在 drop 侦听器定义中单击并键入 control+space,然后应该会出现要重写的方法列表,我猜您会看到 onDrop,而不是 drop。

It's complaining because you're trying to override a method that doesn't exist. I think the method may be called onDrop, not drop - change it and see if it compiles. Also, if you're using eclipse, click within your drop listener definition and type control+space, and a list of methods to override should come up, I'm guessing you'll see onDrop, not drop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文