我的应用程序不断出现此错误:AdapterView 不支持 addView(View, LayoutParams)
然后我将适配器从简单更改为自定义,因为我搜索了解决方案。然后我做了我的自定义适配器,但我不断收到这个错误,我不知道为什么。我该如何解决这个问题?
public class Main_activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ListView mListView = (ListView)findViewById(R.id.mylist);
ArrayList<String> listIds = new ArrayList<String>();
listIds.add("txtname");
listIds.add("btnnum");
listIds.add("txtTran");
mAdapter = new CustomAdapter(this,listIds);
mListView.setAdapter(mAdapter);
populateList();
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
if(!list.isEmpty())
list.clear();
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("txtname","test1");
temp1.put("btnnum", "1");
temp1.put("txtTran", "4x");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("txtname","test2");
temp2.put("btnnum", "2");
temp2.put("txtTran", "3.5x");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("txtname","test3");
temp3.put("btnnum", "3");
temp3.put("txtTran", "3.2x");
list.add(temp3);
HashMap<String,String> temp4 = new HashMap<String,String>();
temp4.put("txtname","test4");
temp4.put("btnnum", "4");
temp4.put("txtTran", "2.4x");
list.add(temp4);
}
protected CustomAdapter mAdapter;
private class CustomAdapter extends ArrayAdapter<String> {
protected Context mContext;
protected ArrayList<String> mItems;
protected LayoutInflater mInflater;
public CustomAdapter(Context context, ArrayList<String> items) {
super(context, R.layout.custom_row_view, items); // Use a custom layout file
mContext = context;
mItems = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("enters");
if(convertView == null){
convertView = mInflater.inflate(R.layout.custom_row_view,null);
}
((TextView) convertView.findViewById(R.id.nametxt)).setText(list.get(position).get("txtname"));
((Button) convertView.findViewById(R.id.btnnum)).setText(list.get(position).get("btnnum"));
((TextView) convertView.findViewById(R.id.transtxt)).setText(list.get(position).get("txtTran"));
return convertView;
}
}
}