【发布时间】:2015-05-10 00:07:28
【问题描述】:
我已经用谷歌搜索了如何制作自定义数组,但我一直无法找到解释我想要实现的目标的教程。我已经尝试转换包含添加图像和文本视图的教程,但是在将“图像”转换/替换为另一个文本视图的某个地方,代码会变得很糟糕。我很难找出问题所在,因为我不知道更改某件事是否会影响自定义适配器所需的其他依赖项。
无论如何,关于我的问题:有人愿意编写一个非常、非常、非常、通用、直接且简单的自定义 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(Context,int,int,List) 不是 适用(实际参数 String[] 不能通过以下方式转换为 int 方法调用转换)构造函数 ArrayAdapter.ArrayAdapter(Context,int,List) 不适用 (实际参数列表和形式参数列表的长度不同)构造函数 ArrayAdapter.ArrayAdapter(Context,int,int,String[]) 不适用 (实参String[]不能通过方法转换为int 调用转换)构造函数 ArrayAdapter.ArrayAdapter(Context,int,String[]) 不适用 (实际参数列表和形式参数列表的长度不同)构造函数 ArrayAdapter.ArrayAdapter(Context,int,int) 不适用(实际 和形式参数列表的长度不同)构造函数 ArrayAdapter.ArrayAdapter(Context,int) 不适用(实际和 形式参数列表的长度不同)
【问题讨论】:
标签: android listview custom-adapter