Search code examples
androidandroid-arrayadapter

Android simple ArrayAdapter from String[]


i can write simple array adapter from class structure, but now i want to write simple array adapter from String, in my application i have simple array as :

private String[]  panelNumbers;
panelNumbers = G.getActiveUserInfo().phoneNumbers.split(",");

for show this array into Listview, array items merged with RadioButton, my xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="@android:drawable/btn_radio"
        android:gravity="right|center_vertical"
        android:text="xcgdf"
        android:textColor="@color/black_text" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@drawable/gradient_divider"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

now i want to fill this array items by custom array adapter my ArrayAdapter dont correct

My ArrayAdapter

public class AdapterSmsPanelNumbers extends ArrayAdapter<String[]> {

    public AdapterSlideMenuSMS(String[] array) {
        super(G.context, R.layout.call_list, array);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.single_choice, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(array[position]);
        return convertView;
    }

    private static class ViewHolder {
        private final TextView    text;
        private final RadioButton radio;
        public ViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.text1);
            radio = (RadioButton) view.findViewById(R.id.radio1);
        }
    }
}

PROBLEM i can not fill listview items by single String[], i dont want to create class from panelNumbers,please help me to resolve this class problems to have simple ArrayAdapter showing simple Array


Solution

  • The Adapter concept has to be understood first. It needs an array (or list) of objects so for each item it will find inside it, it will generate a View (when needed).

    Here, if you want to play with an array of String, you must tell to the ArrayAdapter that it will handle Strings, with :

    public class AdapterSmsPanelNumbers extends ArrayAdapter<String>
    

    The ArrayAdapter already has a mechanic to handle these Strings you will give him. A method make it easy to get an item in the provided array :

    String stringAtPosition = getItem(position);
    

    So, as @SorryForMyEnglish suggested:

    public class AdapterSmsPanelNumbers extends ArrayAdapter<String> {
    
        public AdapterSlideMenuSMS(Context context, String[] array) {
            super(context, 0, array);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_choice, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            holder.text.setText(getItem(position));
            return convertView;
        }
    
        private static class ViewHolder {
            private final TextView    text;
            private final RadioButton radio;
            public ViewHolder(View view) {
                text = (TextView) view.findViewById(R.id.text1);
                radio = (RadioButton) view.findViewById(R.id.radio1);
            }
        }
    }
    

    Then in your Activity, you can use this custom ArrayAdapter :

    private String[]  panelNumbers;
    panelNumbers = G.getActiveUserInfo().phoneNumbers.split(",");
    
    ArrayAdapter adapter = new AdapterSmsPanelNumbers(this, panelNumbers);
    listView.setAdapter(adapter);