Custom listview item in ListViewDragginganimation

穿精又带淫゛_ 提交于 2019-12-04 16:48:18

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!