So I have an android project where I have good use for a sorting functionality. I've been using DevBytes ListViewDraggingAnimation https://www.youtube.com/watch?v=_BZIvjMgH-Q
I have gotten it to work in my app and layout, but one big issue still remains, I cannot get it to work with my custom ListView items. The stock item in DevBytes code is a plain TextView as such:
<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"
/>
My custom ListView items consist of a RelativeLayout with four TextViews inside. As such:
<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>
Admittedly, I don't understand the data structure well. Every attempt I've made at changing it to accept my items has crashed the program. I fear I have changed more parts of the code than needed in some attempts. Could someone help me point out what parts of the code I have to change in order to get the ListView to accept my custom items?
Posting all java code will make my post exceed the character limit.
The code can be found here: http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip
You need to change the adapter if you want to bind data to multiple views (besides just a simple TextView). Read the ArrayAdapter reference to understand how these adapter works.
Basically, your constructor needs to take in your complex data structure, then you need to override getView() to bind data to multiple views. Using the DevBytes example, try something like this:
// 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;
}
}
In your activity, just load the adapter with your layout xml file and string arrays, like this:
StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.list_row, mStringsList);
I haven't tested the code, but it should work. I used something similar recently. Also, you can find several tutorials on the internet for how to make custom adapters. Here's a good one to check out: Using an ArrayAdapter with ListView
First let's debug the current issue and hope to fix the runtime error "java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView".
Using your design, make only 1 Textview UI in the layout like:
<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>
Good luck...
EDIT1: In the logcat messages:
com.example.android.listviewdragginganimation E/ArrayAdapter﹕ You must supply a resource ID for a TextView
com.example.android.listviewdragginganimation E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.android.listviewdragginganimation, PID: 1830
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) ...
Notes:
- The biggest hint is "You must supply a resource ID for a TextView...". In your layout, simply supply only 1 TextView for now. I already gave one example above. Try it out and see what happens.
- After this fix, you can add more TextView in the layout. But you must specify a specific Textview .
There is a third party library which does this for you, just need to undersatnd that and implement in a way we want, just try this before trying anything else.
来源:https://stackoverflow.com/questions/30123686/custom-listview-item-in-listviewdragginganimation