【问题标题】:Custom listview item in ListViewDragginganimationListViewDragginganimation 中的自定义列表视图项
【发布时间】:2015-05-08 12:18:13
【问题描述】:

所以我有一个 android 项目,我可以很好地使用排序功能。我一直在用DevBytesListViewDraggingAnimationhttps://www.youtube.com/watch?v=_BZIvjMgH-Q

我已经让它在我的应用程序和布局中工作,但仍然存在一个大问题,我无法让它与我的自定义 ListView 项目一起工作。 DevBytes 代码中的库存项目是一个普通的 TextView,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:textSize="@dimen/list_text_size"
    android:gravity="center_vertical"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:minHeight="@dimen/list_item_height"
    android:textColor="#000000"
/>

我的自定义 ListView 项目由一个 RelativeLayout 和四个 TextViews 组成。因此:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="adress"
        android:id="@+id/item_adressView"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/item_driveTypeView"
        android:textSize="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="postnr, ort"
        android:id="@+id/item_zipcodePlaceView"
        android:textSize="15dp"
        android:layout_below="@+id/item_adressView"
        android:layout_toEndOf="@+id/item_driveTypeView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="företag"
        android:id="@+id/item_companyView"
        android:textSize="15dp"
        android:layout_below="@+id/item_zipcodePlaceView"
        android:layout_toEndOf="@+id/item_driveTypeView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="P1"
        android:id="@+id/item_driveTypeView"
        android:textSize="40dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

诚然,我不太了解数据结构。我所做的每一次更改它以接受我的项目的尝试都会使程序崩溃。我担心在某些尝试中我更改的代码部分比需要的要多。有人可以帮我指出我必须更改代码的哪些部分才能让ListView 接受我的自定义项目吗?

发布所有java代码会使我的帖子超出字符限制。

代码可以在这里找到: http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip

【问题讨论】:

  • Every attempt I've made at changing it to accept my items has crashed the program - 发布您从 Logcat 获得的异常
  • 我没有保留所有的例外情况,我猜是因为我从来没有接近过我做对了的想法。在代码中引用相对布局中的文本视图而不是纯文本视图会生成以下内容(尽管我怀疑这会有很大帮助): 原因:java.lang.ClassCastException:android.widget.RelativeLayout 无法转换为 android.widget。 TextView [LegacyBackupAccountManager] 无法获取旧版传输上下文。 android.content.pm.PackageManager$NameNotFoundException:找不到应用程序包 com.google.android.backup
  • 自从我实施了这个项目,我就非常了解它。目前,您可以做的最简单的事情是在您的自定义布局中只有一个 TextView UI。原因是 StableArrayAdapter 只有一种方法只能接受 textview UI。查看代码 StableArrayAdapter(Context context, int textViewResourceId...)。这就是原因。也许稍后我会发布答案。
  • 非常感谢原始 Android 的深入回答。您的意思是它只接受列表视图上的每个项目一个字符串?例如,我希望能够拥有不同大小文本的项目。
  • 我很惊讶您收到错误“java.lang.ClassCastException”。我知道你改变了布局。您是否更改了 ListViewDraggingAnimation 应用程序中的任何 Java 代码?

标签: java android listview android-listview


【解决方案1】:

如果要将数据绑定到多个视图(除了简单的 TextView),则需要更改适配器。阅读ArrayAdapter reference 了解这些适配器的工作原理。

基本上,您的构造函数需要接受您复杂的数据结构,然后您需要重写 getView() 以将数据绑定到多个视图。使用 DevBytes 示例,尝试如下操作:

// Update the adapter to use string arrays instead of just strings
public class StableArrayAdapter extends ArrayAdapter<String[]> {

    final int INVALID_ID = -1;
    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    // Save these references to the layout and string data
    int mRowLayout;
    List<String[]> mStrings;

    // Constructor should accept string arrays
    public StableArrayAdapter(Context context, int rowLayout, List<String[]> objects) {

        super(context, rowLayout, objects);

        // Change this so your string array list can be indexed
        for (int i = 0; i < objects.size(); ++i) {

            // Make sure you are using a unique string for each list row
            // String[0] is just an example
            String[] stringArray = objects.get(i);
            String uniqueString = stringArray[0]; 

            mIdMap.put(uniqueString, i);
        }

        mRowLayout = rowLayout;
        mStrings = objects;
    }

    ...

    // Populate your layout text views with data from your string array
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(getContext()).inflate(mRowLayout, null);

        TextView tvAddress = (TextView)convertView.findViewById(R.id.item_adressView);
        TextView tvZipCode = (TextView)convertView.findViewById(R.id.item_zipcodePlaceView);
        TextView tvCompany = (TextView)convertView.findViewById(R.id.item_companyView);
        TextView tvDriveType = (TextView)convertView.findViewById(R.id.item_driveTypeView);

        tvAddress.setText(mStrings.get(position)[0]);
        tvZipCode.setText(mStrings.get(position)[1]);
        tvCompany.setText(mStrings.get(position)[2]);
        tvDriveType.setText(mStrings.get(position)[3]);

        return convertView;
    }
}

在您的活动中,只需使用您的布局 xml 文件和字符串数组加载适配器,如下所示:

StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, mStringsList);

我还没有测试过代码,但它应该可以工作。我最近用过类似的东西。此外,您可以在 Internet 上找到一些关于如何制作自定义适配器的教程。这是一个很好的检查:Using an ArrayAdapter with ListView

【讨论】:

    【解决方案2】:

    首先让我们调试当前的问题,希望能修复运行时错误“java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView”。

    使用您的设计,在布局中只制作 1 个 Textview UI,例如:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="adress"
            android:id="@+id/item_adressView"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/item_driveTypeView"
            android:textSize="15dp" />
    </RelativeLayout>
    

    祝你好运……

    EDIT1:在 logcat 消息中:

    com.example.android.listviewdragginganimation E/ArrayAdapter﹕ 您必须为 TextView 提供资源 ID

    com.example.android.listviewdragginganimation E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.example.android.listviewdragginganimation,PID:1830

    java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView 在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) ...

    注意事项

    • 最大的提示是“您必须为 TextView 提供资源 ID...”。在您的布局中,现在只需提供 1 个 TextView。我已经在上面举了一个例子。试试看会发生什么。
    • 在此修复后,您可以在布局中添加更多 TextView。但是你必须指定一个特定的 Textview 。

    【讨论】:

    • 不知道具体要发布什么,这是我在更改时收到的所有错误消息尝试您刚刚发布的内容而不更改代码:pastebin.com/D0iLuCXv 我想继续强调,尽管我什至没有知道从哪里开始,所以无论我做错了什么都不是错字或简单的错误。
    • @Flowdorio,感谢您的 logcat 帖子。我在帖子中添加了“EDIT1”,试试看。我希望这个修复会有进展。否则我不能再帮你了。如果您认为我的回答至少有帮助,您可以点击有用的图标(向上箭头);)祝您好运...
    【解决方案3】:

    有一个第三方库可以为您执行此操作,只需了解这一点并以我们想要的方式实施,只需在尝试其他任何操作之前尝试 this

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 2011-07-25
      • 2023-04-06
      相关资源
      最近更新 更多