滚动时自动更改列表的行位置

时间:2016-06-16 12:15:28

标签: android sqlite listview adapter

我有一个多列列表View Activity通过列表适配器从Android数据库填充。但是当我在此列表中上下滚动时,Some Row位置会自动更改。

DBHelper.java

public ArrayList<HashMap<String, String>> getAlldata(){
        ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String,String>>();
        SQLiteDatabase db = getReadableDatabase();
        String query = "select *  from tbentry order  by column_id DESC";
        Cursor c = db.rawQuery(query, null);
        if(c.moveToFirst()) {
            do {
                // you are creating map here but not adding this map to list
                HashMap<String, String> map = new HashMap<String, String>();

                map.put(FIRST_COLUMN, c.getString(4));
                map.put(SECOND_COLUMN, c.getString(2));
                map.put(THIRD_COLUMN, String.valueOf(String.format("%.2f", c.getDouble(5))));
                map.put(FOURTH_COLUMN, String.valueOf(String.format("%.2f", c.getDouble(6))));
                map.put(FIFTH_COLUMN, String.valueOf(String.format("%.2f", c.getDouble(7))));
                map.put(SIXTH_COLUMN, c.getString(3));

                // so do add it here
                maplist.add(map);
            } while (c.moveToNext());
        }
        db.close();
        return maplist;
    }

Constant.java

public class Constant {

    public static final String FIRST_COLUMN="First";
    public static final String SECOND_COLUMN="Second";
    public static final String THIRD_COLUMN="Third";
    public static final String FOURTH_COLUMN="Fourth";
    public static final String FIFTH_COLUMN ="Fifth";
    public static final String SIXTH_COLUMN ="Sixth";
}

ListAdapterView.java

public class ListViewAdapter extends BaseAdapter {

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    TextView txtFifth;
    public ListViewAdapter(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



        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null){

            convertView=inflater.inflate(R.layout.grid_item, null);


            txtFirst=(TextView) convertView.findViewById(R.id.item1);
            txtSecond=(TextView) convertView.findViewById(R.id.item2);
            txtThird=(TextView) convertView.findViewById(R.id.item3);
            txtFourth=(TextView) convertView.findViewById(R.id.item4);
            txtFifth = (TextView) convertView.findViewById(R.id.item5);

        }

        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));
        txtFifth.setText(map.get(FIFTH_COLUMN));

        return convertView;
    }
}

ViewReport.java

public class ViewReport extends Activity{

    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report_view);
        Intent intent = getIntent();
        String typemonth = intent.getStringExtra("byMonth");
        DBHelper db = new DBHelper(this);
        final ListView lv= (ListView)findViewById(R.id.list_view);

        list=new ArrayList<HashMap<String,String>>();

        if("all".equals(typemonth)){
            list = db.getAlldata();
        }else if("currentmonth".equals(typemonth)){
            list = db.getReportCurrentMonth();
        }else if("lastmonth".equals(typemonth)){
            list = db.getReportLastMonth();
        }else if("last3month".equals(typemonth)){
            list = db.getReportThreeMonth();
        }

        final ListViewAdapter adapter=new ListViewAdapter(this, list);
        lv.setAdapter(adapter);
        LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.header_list, lv, false);
        lv.addHeaderView(header, null, false);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long idInDB) {
                // TODO Auto-generated method stub
                HashMap<String, String> rowData = (HashMap<String, String>) adapter.getItem(position);
                final Dialog dialog = new Dialog(ViewReport.this);
                dialog.setTitle(rowData.get(SECOND_COLUMN));
                dialog.setContentView(R.layout.custom_dialog);
                TextView txt1 = (TextView) dialog.findViewById(R.id.tv_dialog_date);
                TextView txt2 = (TextView) dialog.findViewById(R.id.tv_dialog_ledger);
                TextView txt3 = (TextView) dialog.findViewById(R.id.tv_dialog_Particuller);
                TextView txt4 = (TextView) dialog.findViewById(R.id.tv_dialog_income);
                TextView txt5 = (TextView) dialog.findViewById(R.id.tv_dialog_expenses);

                txt1.setText(rowData.get(FIRST_COLUMN));
                txt2.setText(rowData.get(SECOND_COLUMN));
                txt3.setText(rowData.get(SIXTH_COLUMN));
                txt4.setText(rowData.get(THIRD_COLUMN));
                txt5.setText(rowData.get(FOURTH_COLUMN));

               dialog.show();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

“自动更改位置”是android重用视图。我认为问题出在你的适配器上。即使您检查视图是否为null,仍然需要设置TextView ID。

  if(convertView == null){
        convertView=inflater.inflate(R.layout.grid_item, null);
  }

   txtFirst=(TextView) convertView.findViewById(R.id.item1);
   txtSecond=(TextView) convertView.findViewById(R.id.item2);
   txtThird=(TextView) convertView.findViewById(R.id.item3);
   txtFourth=(TextView) convertView.findViewById(R.id.item4);
   txtFifth = (TextView) convertView.findViewById(R.id.item5);

答案 1 :(得分:0)

像这样放置你的getView()代码

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        v=convertView;
        if(convertView==null)
        {
            //inflate the view for each row of listview
            v=inflator.inflate(R.layout.myadapter,null);
            //ViewHolder object to contain myadapter.xml elements
            vholder=new ViewHolder();             
vholder.txtFirst=(TextView) v.findViewById(R.id.item1);
 vholder.txtSecond=(TextView) v.findViewById(R.id.item2);
  vholder.txtThird=(TextView) v.findViewById(R.id.item3);
   vholder.txtFourth=(TextView) v.findViewById(R.id.item4);
   vholder.txtFifth = (TextView) v.findViewById(R.id.item5);
            //set holder to the view
            v.setTag(vholder);
        }
        else
            vholder=(ViewHolder)v.getTag();
        //getting MyItem Object for each position
      HashMap<String, String> map=list.get(position);
    vholder.txtFirst.setText(map.get(FIRST_COLUMN));
    vholder.txtSecond.setText(map.get(SECOND_COLUMN));
    vholder.txtThird.setText(map.get(THIRD_COLUMN));
    vholder.txtFourth.setText(map.get(FOURTH_COLUMN));
    vholder.txtFifth.setText(map.get(FIFTH_COLUMN));

return v;
 }

并在Viewholder类中定义您的视图,如下所示

//Viewholder class to contain inflated xml views
    private  class ViewHolder
    {
        TextView txtFirst, txtSecond, txtThird, txtFourth, txtFifth;
    }

请参阅此处了解更多信息:http://coderzpassion.com/android-custom-listview-with-edittext/