我有一个带自定义适配器的列表视图。我试图使用适配器将值设置为listview。但是这些值并没有在运行时设置,但是在使用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添加到按钮并打印项目,并显示项目的值,如添加的名称,ID等在HashMap中显示,所以它只在渲染中出现问题?当我对xml中的值进行硬编码时,也会显示这些值。有人可以解释可能存在的问题。
答案 0 :(得分:1)
我认为所需的更改是在getView()方法中,我在其中添加了以下代码,并且它有效。这与我在onClickListener()
中使用的代码相同TextView textView=(TextView)row.findViewById(R.id.specialization);
HashMap<String, String> item = (HashMap<String, String>) adapter.getItem(position);
textView.setText(item.get("txt"));
我会对图像等其他领域做同样的事情。如果这个想法是错误的,请投票和评论。我想我甚至可以使用视图。