ListViewDragginganimation中的自定义列表视图项

所以我有一个android项目,我很好地使用了排序function。 我一直在使用DevBytes ListViewDraggingAnimation https://www.youtube.com/watch?v=_BZIvjMgH-Q

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

  

我的自定义ListView项目包含一个RelativeLayout ,里面有四个TextView 。 因此:

       

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

发布所有java代码将使我的post超出字符限制。

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

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

基本上,构造函数需要接受复杂的数据结构,然后需要覆盖getView()以将数据绑定到多个视图。 使用DevBytes示例,尝试这样的事情:

 // Update the adapter to use string arrays instead of just strings public class StableArrayAdapter extends ArrayAdapter { final int INVALID_ID = -1; HashMap mIdMap = new HashMap(); // Save these references to the layout and string data int mRowLayout; List mStrings; // Constructor should accept string arrays public StableArrayAdapter(Context context, int rowLayout, List 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); 

我没有测试过代码,但它应该可以运行。 我最近用过类似的东西。 此外,您可以在互联网上找到有关如何制作自定义适配器的几个教程。 这是一个很好的检查: 使用ArrayAdapter和ListView

首先让我们调试当前的问题并希望修复运行时错误“ java.lang.ClassCastException :android.widget.RelativeLayout不能转换为android.widget.TextView”。

使用您的设计,在布局中只创建一个Textview UI,如:

    

祝好运…

EDIT1 :在logcat消息中:

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

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

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

备注

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

有一个第三方库可以为你做这件事,只需要低估并以我们想要的方式实现,只需在尝试其他任何事情之前尝试这个 。