我尝试创建自定义数组适配器,以便为我的应用程序制作自定义ListView。 我做的适配器:
package aaeu.app.presentationlayer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import aaeu.app.R;
public class AlertOverviewAdapter extends ArrayAdapter {
private final Context context;
private final String[] values;
public AlertOverviewAdapter(Context context, String[] resource) {
super(context,R.layout.test ,resource);
this.context = context;
this.values = resource;
}
@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.test, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Do something with the icon....
String s = values[position];
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
//set Ok icon
} else {
//set Not ok icon
}
return rowView;
}
}
我尝试以这种方式在列表视图中加载适配器:
ArrayAdapter adapter = new AlertOverviewAdapter(this, values);
lv_alert_overview.setAdapter(adapter);
我的价值观:
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
当我运行应用时,只有" Android"在我的列表中显示,我无法完成所有字符串在列表视图中可见。当我完成这项工作后,我可以根据自己的需要修改布局。
我认为这个问题与int position
有关,但我不知道 - 有人可以帮我吗?