Reputation: 851
I've Googled the heck out of making custom arrays, but I have not been able to find a tutorial that explains what I'm looking to achieve. I've tried converting the tutorials that comprise of adding a image and a textview, but somewhere during the conversion/replacement of the "image" for another textview the code goes to hell. I'm having a hard time trying to isolate the problem, since I do not know if changing one thing will affect other dependencies that are needed for a custom adapter.
Anyhow, on to my question: Would someone be willing to write a very, very, very, generic, straight-forward, and simple Custom ArrayAdapter that expects two string values, that can be used with a ListView?...or, attempt to debug my attempt and hopefully spot what my problem is.
Any help is appreciated, thank you.
I've included the code that I know is required, but I've been striking out with everything else.
custom_listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Please wait.."
android:layout_weight="1"
android:id="@+id/txtFieldName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Please wait.."
android:id="@+id/txtFieldValue" />
</LinearLayout>
CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] fieldNames;
private final String[] fieldValues;
public CustomListViewAdapter(Context context, String[] fieldNames, String[] fieldValues) {
super(context, R.layout.custom_listview, fieldNames, fieldValues);
this.context = context;
this.fieldNames = fieldNames;
this.fieldValues= fieldValues;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.custom_listview, parent, false);
TextView txtField= (TextView) rowView.findViewById(R.id.txtFieldName);
TextView txtValue= (TextView) rowView.findViewById(R.id.txtFieldValue);
txtField.setText(fieldNames[position]);
txtValue.setText(fieldValues[position]);
return rowView;
}
}
MainActivity.java
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new CustomListViewAdapter(this, "myTestField", "myTestValue"));
}
}
Error message I'm receiving:
Error:(20, 9) error: no suitable constructor found for ArrayAdapter(Context,int,String[],String[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable (actual argument String[] cannot be converted to int by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable (actual argument String[] cannot be converted to int by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable (actual and formal argument lists differ in length)
Upvotes: 0
Views: 694
Reputation: 1210
I would suggest you to extend BaseAdapter
instead of ArrayAdapter
, here's a great article on good practices when writing adapters: http://www.piwai.info/android-adapter-good-practices/
It covers basically all you need when creating an adapter.
Upvotes: 0
Reputation: 3785
The error you get is because you are calling super(context, R.layout.custom_listview, fieldNames, fieldValues);
on your constructor while there is no such constructor that takes two String[]
parameters available for the ArrayAdapter
class.
You should look at the documentation for ArrayAdapter
here and find a suitable constructor to be used in your subclass. For example:
public CustomListViewAdapter(Context context, String[] fieldNames, String[] fieldValues) {
super(context, R.layout.custom_listview);
...
}
EDIT:
Here is a good tutorial on ListView
and ArrayAdapters
.
Upvotes: 0