【发布时间】:2015-03-01 20:33:56
【问题描述】:
我有一个带有自定义适配器的列表视图。我正在尝试使用适配器为列表视图设置值。但是这些值不是在运行时设置的,而是在使用 simpleadapter 实现时工作。我的自定义适配器代码如下。
public class dataListAdapter extends SimpleAdapter {
// HashMap<String, String> map = new HashMap<String, String>();
List<HashMap<String, String>> data = new ArrayList();
public dataListAdapter(Context context,
List<HashMap<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.data = data;
}
@Override
public Object getItem(int arg0) {
if (null != data) {
try {
return data.get(arg0);
} catch (IndexOutOfBoundsException e) {
return null;
}
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater
.inflate(R.layout.listview_layout, parent, false);
}
Button vd = (Button) row.findViewById(R.id.viewDetails);
// Button gd = (Button) row.findViewById(R.id.getDirections);
Button ra = (Button) row.findViewById(R.id.reqAppointment);
final OnClickListener vdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
ListView listView = (ListView) parent;
dataListAdapter adapter = (dataListAdapter) listView
.getAdapter();
HashMap<String, String> item = (HashMap<String, String>) adapter
.getItem(position);
// listView.getAdapter().get
Toast.makeText(getApplicationContext(),
"vd clicked Id " + item,
Toast.LENGTH_LONG).show();
}
};
final OnClickListener gdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"gdclicked Id ", Toast.LENGTH_LONG)
.show();
}
};
final OnClickListener ravClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"req appclicked Id ", Toast.LENGTH_LONG)
.show();
}
};
vd.setOnClickListener(vdClickListener);
// gd.setOnClickListener(gdClickListener);
ra.setOnClickListener(ravClickListener);
return row;
}
}
这就是我为自定义适配器设置值的方式。但这些值未在列表视图中设置。
String[] from = { "flag", "txt", "cur", "vd", "gd",
"ra" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.name, R.id.spec,
R.id.vd, R.id.gd, R.id.ra};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
adapter = new dataListAdapter(getBaseContext(), aList,
R.layout.listview_layout, from, to);
// new CallToServer(adapter, aList).execute("");
// Getting a reference to listview of main.xml layout file
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", "Name : " + "name");
hm.put("cur", "spec: " + "spec");
hm.put("flag", Integer.toString(0x7f020001));
hm.put("but1", "");
hm.put("but2", "");
hm.put("but3", "");
hm.put("ID", "100");
aList.add(hm);
System.out.println("Adapter object " + adapter);
adapter.notifyDataSetChanged();
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
// setContentView(listView);
谁能指导我哪里出错了,因为设置到自定义适配器的值没有显示在列表视图中。
我已尝试将 onClickListener 添加到按钮并打印项目,并显示项目的值,例如显示在 HashMap 中添加的名称、Id 等,因此仅在渲染中存在问题?当我对 xml 中的值进行硬编码时,也会显示这些值。有人可以解释一下可能是什么问题。
【问题讨论】:
-
发布您的 getCount() 代码。
-
@jvrodrigues 我没有重写那个方法,我只有 3 个被重写的方法,它们是 getView、getItem(int) 和 getItemId(int)
标签: android listview custom-adapter