Android Custom ArrayAdapter



我已经通过制作自定义数组来搜索了heck,但是我找不到可以解释我想要实现的教程的教程。我已经尝试转换包含添加图像和文本视图的教程,但是在"图像"转换/替换为另一个文本视图中,代码陷入了地狱。我很难试图隔离问题,因为我不知道更改一件事是否会影响自定义适配器所需的其他依赖关系。

无论如何,就我的问题而言:有人愿意写一个非常非常,非常非常,通用,直率和简单的自定义ArrayAdapter,它可以期望两个字符串值,可以与listView一起使用?...或者,尝试调试我的尝试,希望发现我的问题是什么。

任何帮助将不胜感激,谢谢。

我包括了我知道的代码,但是我一直在与其他所有内容进行罢工。

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"));
    }

}

我收到的错误消息:

错误:(20,9)错误:找不到适合的构造函数 arrayadapter(context,int,string [],string [])构造函数 arrayadapter.arrayadapter(上下文,int,int,list)不是 适用(实际参数字符串[]不能通过 方法调用转换)构造函数 arrayadapter.arrayadapter(上下文,int,list)不适用 (实际和正式的论点列表的长度不同)构造函数 arrayadapter.arrayadapter(上下文,int,int,string [])不适用 (实际参数字符串[]不能通过方法转换为int 调用转换)构造函数 arrayadapter.arrayadapter(上下文,int,string [])不适用 (实际和正式的论点列表的长度不同)构造函数 arrayadapter.ArrayAdapter(上下文,int,int)不适用(实际 正式的论点列表在长度上有所不同)构造函数 arrayadapter.ararayadapter(上下文,int)不适用(实际和 正式的论点列表的长度有所不同)

您遇到的错误是因为您在构造函数上调用 super(context, R.layout.custom_listview, fieldNames, fieldValues);,而没有这样的构造函数可用于ArrayAdapter类可用的两个String[]参数。

您应该在此处查看ArrayAdapter的文档,并找到一个合适的构造函数。例如:

public CustomListViewAdapter(Context context, String[] fieldNames, String[] fieldValues) {
        super(context, R.layout.custom_listview);
        ...
    }

编辑:这是ListViewArrayAdapters的好教程。

我建议您扩展BaseAdapter而不是ArrayAdapter,这是一篇关于编写适配器时良好实践的精彩文章:http://www.piwai.info/android-android-adroid-adroid-adroid-apapter-good-good-practices/

它基本上涵盖了创建适配器时所需的所有内容。