I have a multi column list View Activity populate from Android database by list adapter. but when i scrolling up and down this list, Some Row position is automatically changed.
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();
}
});
}
}