Android 自定义数组适配器
Android Custom ArrayAdapter
我在谷歌上搜索了制作自定义数组的麻烦,但我一直没能找到解释我想要实现的目标的教程。我已经尝试转换包含添加图像和文本视图的教程,但是在 "image" 的 conversion/replacement 期间的某个地方,对于另一个文本视图,代码陷入困境。我很难找出问题所在,因为我不知道更改一件事是否会影响自定义适配器所需的其他依赖项。
无论如何,关于我的问题:有人愿意写一个非常非常非常通用、直接且简单的 Custom 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"));
}
}
我收到的错误消息:
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)
你得到的错误是因为你在你的构造函数上调用 super(context, R.layout.custom_listview, fieldNames, fieldValues);
而没有这样的构造函数需要两个 String[]
参数可用于 ArrayAdapter
class .
您应该查看 ArrayAdapter
here 的文档并找到合适的构造函数用于您的子 class。例如:
public CustomListViewAdapter(Context context, String[] fieldNames, String[] fieldValues) {
super(context, R.layout.custom_listview);
...
}
编辑:
Here 是关于 ListView
和 ArrayAdapters
的很好的教程。
我建议您扩展 BaseAdapter
而不是 ArrayAdapter
,这里有一篇关于编写适配器的良好实践的好文章:http://www.piwai.info/android-adapter-good-practices/
它基本上涵盖了创建适配器时需要的所有内容。
我在谷歌上搜索了制作自定义数组的麻烦,但我一直没能找到解释我想要实现的目标的教程。我已经尝试转换包含添加图像和文本视图的教程,但是在 "image" 的 conversion/replacement 期间的某个地方,对于另一个文本视图,代码陷入困境。我很难找出问题所在,因为我不知道更改一件事是否会影响自定义适配器所需的其他依赖项。
无论如何,关于我的问题:有人愿意写一个非常非常非常通用、直接且简单的 Custom 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"));
}
}
我收到的错误消息:
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)
你得到的错误是因为你在你的构造函数上调用 super(context, R.layout.custom_listview, fieldNames, fieldValues);
而没有这样的构造函数需要两个 String[]
参数可用于 ArrayAdapter
class .
您应该查看 ArrayAdapter
here 的文档并找到合适的构造函数用于您的子 class。例如:
public CustomListViewAdapter(Context context, String[] fieldNames, String[] fieldValues) {
super(context, R.layout.custom_listview);
...
}
编辑:
Here 是关于 ListView
和 ArrayAdapters
的很好的教程。
我建议您扩展 BaseAdapter
而不是 ArrayAdapter
,这里有一篇关于编写适配器的良好实践的好文章:http://www.piwai.info/android-adapter-good-practices/
它基本上涵盖了创建适配器时需要的所有内容。