如何在Multicolumn ListView中通过HashMap从DBHelper中检索数据

时间:2016-02-09 10:39:27

标签: android hashmap

我想通过HashMap在DBHelper类的Multicolumn ListView中查看我的记录。这将以多列显示我的数据,并且所有行都是可点击的。我有一些困难。我的代码在这里

DBHelper.java

 public ArrayList<HashMap<String, String>> getdata(){
        ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String,String>>();
        SQLiteDatabase db = getReadableDatabase();
        String query = "select * from tbexpenses";
        Cursor c = db.rawQuery(query, null);
        if(c.moveToFirst()){
            do{
                HashMap<String, String> map = new HashMap<String, String>();
                for(int i=0; i<c.getColumnCount();i++){
                    map.put(c.getColumnName(i), c.getString(i));
                }
                }while (c.moveToNext());
            }db.close();
            return maplist;
    }  

ListView.java

 package com.multiplecolumnlist;

 import java.util.ArrayList;
 import java.util.HashMap;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.Toast;
 import static com.multiplecolumnlist.Constants.FIRST_COLUMN;
 import static com.multiplecolumnlist.Constants.FOURTH_COLUMN;
 import static com.multiplecolumnlist.Constants.SECOND_COLUMN;
 import static com.multiplecolumnlist.Constants.THIRD_COLUMN; 

 public class MainActivity extends Activity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView=(ListView)findViewById(R.id.listView1);

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

    ?????????????????????????????????????????????

    ListViewAdapters adapter=new ListViewAdapters(this, list);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
          @Override
          public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
          {
            int pos=position+1;
            Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();  
          }

    });

}

ListViewAdapter.java

public class ListViewAdapters extends BaseAdapter{

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



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

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

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        txtFourth=(TextView) convertView.findViewById(R.id.status);

    }

    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));

    return convertView;
}

}

Constant.java

public class Constants {

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";
}

1 个答案:

答案 0 :(得分:1)

用问号

替换此代码
list.clear();
DBHelper helper = new DBHelper();
list = helper.getdata();

编辑1:getdata()方法错误

同时检查您的getdata()功能

public ArrayList<HashMap<String, String>> getdata(){
    ArrayList<HashMap<String, String>> maplist = new ArrayList<HashMap<String,String>>();
    SQLiteDatabase db = getReadableDatabase();
    String query = "select * from tbexpenses";
    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>();
            for(int i=0; i<c.getColumnCount();i++) {
                map.put(c.getColumnName(i), c.getString(i));
            }
            // so do add it here
            maplist.add(map);
        } while (c.moveToNext());
    }
    db.close();
    return maplist;
}

然后它会运行良好。