本文介绍了如何在多列ListView中通过HashMap从DBHelper检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过HashMap在DBHelper Class中查看多列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();
返回地图列表;
}

ListView.java

  package com.multiplecolumnlist; 

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

导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.View;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入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 {
$ b $ private ArrayList< HashMap< String,String>>列表;

@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( );
}

});




$ b ListViewAdapter.java

  public class ListViewAdapters extends BaseAdapter {
$ b $ public ArrayList< HashMap< String,String>>列表;
活动活动;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public ListViewAdapters(Activity activity,ArrayList< HashMap< String,String>> list){
super();
this.activity = activity;
this.list = list;

$ b $ @Override
public int getCount(){
// TODO自动生成的方法存根
return list.size();

$ b @Override
public Object getItem(int position){
// TODO自动生成的方法存根
return list.get(position) ;
}

@Override
public long getItemId(int position){
// TODO自动生成的方法存根
return 0;



$ b @Override
public View getView(int position,View convertView,ViewGroup parent){
// TODO Auto-生成的方法存根



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>地图= list.get(位置);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));

返回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;
}


解决方案
  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 {
//你在这里创建映射,但不把这个映射添加到列表
HashMap< String,String> map = new HashMap< String,String>();
for(int i = 0; i< c.getColumnCount(); i ++){
map.put(c.getColumnName(i),c.getString(i));
}
//所以在这里添加它
maplist.add(map);
} while(c.moveToNext());
}
db.close();
返回地图列表;
}

然后运行正常。


I want to View my record in Multicolumn ListView From DBHelper Class by HashMap. that will show my data in multicolumn and also all rows are clickable. i have some difficulty. my code is here

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";
}
解决方案

replace this code with question mark

list.clear();
DBHelper helper = new DBHelper();
list = helper.getdata();

EDIT 1:Error in getdata() method

Also check your getdata() function

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

Then it'll run fine.

这篇关于如何在多列ListView中通过HashMap从DBHelper检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:25