当记录量太大以至于整个视图超出了手机的屏幕时,就会发生问题.现在,当我滚动时,其中一行中的所有记录都在更改(它们的值已更改).其他所有行都很好.可能是什么问题? public class ListViewAdapters extends BaseAdapter{ public ArrayList<HashMap<String, String>> list; Activity activity; TextView txtFirst; TextView txtSecond; TextView txtFourth; public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){ super(); this.activity=activity; this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub HashMap<String, String> map=list.get(position); LayoutInflater inflater=activity.getLayoutInflater(); if(convertView == null) { if(map.get(MARKED)=="no") { convertView=inflater.inflate(R.layout.colmn_row, null); } else if (map.get(MARKED)=="yes") { convertView=inflater.inflate(R.layout.colmn_row_clicked, null); } txtFirst=(TextView) convertView.findViewById(R.id.name); txtSecond=(TextView) convertView.findViewById(R.id.gender); txtFourth=(TextView) convertView.findViewById(R.id.status); } txtFirst.setText(map.get(FIRST_COLUMN)); txtSecond.setText(map.get(SECOND_COLUMN)); txtFourth.setText(map.get(FOURTH_COLUMN)); return convertView; }}解决方案请尝试以下代码:public class ListViewAdapters extends BaseAdapter {public ArrayList<HashMap<String, String>> list;Activity activity;TextView txtFirst;TextView txtSecond;TextView txtFourth;public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){ super(); this.activity=activity; this.list=list;}@Overridepublic int getCount() { // TODO Auto-generated method stub return list.size();}@Overridepublic Object getItem(int position) { // TODO Auto-generated method stub return list.get(position);}@Overridepublic long getItemId(int position) { // TODO Auto-generated method stub return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; HashMap<String, String> map=list.get(position); LayoutInflater inflater=activity.getLayoutInflater(); if(convertView == null) { holder = new ViewHolder(); if (map.get(MARKED) == "no") { convertView = inflater.inflate(R.layout.colmn_row, null); } else if (map.get(MARKED) == "yes") { convertView = inflater.inflate(R.layout.colmn_row_clicked, null); } holder.txtFirst = (TextView) convertView.findViewById(R.id.name); holder.txtSecond = (TextView) convertView.findViewById(R.id.gender); holder.txtFourth = (TextView) convertView.findViewById(R.id.status); holder.txtFirst.setText(map.get(FIRST_COLUMN)); holder.txtSecond.setText(map.get(SECOND_COLUMN)); holder.txtFourth.setText(map.get(FOURTH_COLUMN)); }else { holder = (ViewHolder) convertView.getTag(); } return convertView;}@Overridepublic int getItemViewType(int position) { return position;}@Overridepublic int getViewTypeCount() { return getCount();}public class ViewHolder { TextView txtFirst, txtSecond,txtFourth;}} I tried to create a multicolumn listview by following this tutorial:http://techlovejump.com/android-multicolumn-listview/ .The problem occured, when the amount of records was so big that the whole view exceeded the screen of a phone. Now when I'm scrolling, all of the records in one of the rows are changing (their values are changed). All the other rows are perfectly fine. What could be the problem? public class ListViewAdapters extends BaseAdapter{ public ArrayList<HashMap<String, String>> list; Activity activity; TextView txtFirst; TextView txtSecond; TextView txtFourth; public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){ super(); this.activity=activity; this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub HashMap<String, String> map=list.get(position); LayoutInflater inflater=activity.getLayoutInflater(); if(convertView == null) { if(map.get(MARKED)=="no") { convertView=inflater.inflate(R.layout.colmn_row, null); } else if (map.get(MARKED)=="yes") { convertView=inflater.inflate(R.layout.colmn_row_clicked, null); } txtFirst=(TextView) convertView.findViewById(R.id.name); txtSecond=(TextView) convertView.findViewById(R.id.gender); txtFourth=(TextView) convertView.findViewById(R.id.status); } txtFirst.setText(map.get(FIRST_COLUMN)); txtSecond.setText(map.get(SECOND_COLUMN)); txtFourth.setText(map.get(FOURTH_COLUMN)); return convertView; }} 解决方案 Please try this code: public class ListViewAdapters extends BaseAdapter {public ArrayList<HashMap<String, String>> list;Activity activity;TextView txtFirst;TextView txtSecond;TextView txtFourth;public ListViewAdapters(Activity activity, ArrayList<HashMap<String, String>> list){ super(); this.activity=activity; this.list=list;}@Overridepublic int getCount() { // TODO Auto-generated method stub return list.size();}@Overridepublic Object getItem(int position) { // TODO Auto-generated method stub return list.get(position);}@Overridepublic long getItemId(int position) { // TODO Auto-generated method stub return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; HashMap<String, String> map=list.get(position); LayoutInflater inflater=activity.getLayoutInflater(); if(convertView == null) { holder = new ViewHolder(); if (map.get(MARKED) == "no") { convertView = inflater.inflate(R.layout.colmn_row, null); } else if (map.get(MARKED) == "yes") { convertView = inflater.inflate(R.layout.colmn_row_clicked, null); } holder.txtFirst = (TextView) convertView.findViewById(R.id.name); holder.txtSecond = (TextView) convertView.findViewById(R.id.gender); holder.txtFourth = (TextView) convertView.findViewById(R.id.status); holder.txtFirst.setText(map.get(FIRST_COLUMN)); holder.txtSecond.setText(map.get(SECOND_COLUMN)); holder.txtFourth.setText(map.get(FOURTH_COLUMN)); }else { holder = (ViewHolder) convertView.getTag(); } return convertView;}@Overridepublic int getItemViewType(int position) { return position;}@Overridepublic int getViewTypeCount() { return getCount();}public class ViewHolder { TextView txtFirst, txtSecond,txtFourth;}} 这篇关于滚动时列表视图中的行会更改(Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 06:29