问题描述
- android listview适配器的错误
-
具体报错见下面。高手求解决·····
先贴代码,下面是PointMgrActivity.javapackage com.app.bluetoothpro;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.app.DB.DBManager;
import com.app.DB.PointClass;import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class PointMgrActivity extends Activity {
protected static final String ACTIVITY_TAG = "LogDemo"; private DBManager dbManager; public SQLiteDatabase db; String gID; String pStatu; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.point_mgr); dbManager = new DBManager(this); /* * Points person = new Points(); person.pid = "id1"; person.pstatu = * "OFF"; db.execSQL("INSERT INTO points VALUES (?, ?)", new * Object[]{person.pid, person.pstatu}); */ addbtn(); } /********************************* 添加add方法 ****************************************/ public void add() { ArrayList<PointClass> persons = new ArrayList<PointClass>(); PointClass person = new PointClass(gID, pStatu); persons.add(person); dbManager.add(persons); } * */***************************** 添加query()查询方法 ******************************/ public void query() { List<PointClass> persons = dbManager.query(); ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); for (PointClass person : persons) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("pid", person.pid); map.put("pstatu", getstatu(pStatu)); listItem.add(map); Log.d(ACTIVITY_TAG, person.pid + " +++++++++++++++++++++++++++ " + getstatu(pStatu)); } ListView list = (ListView) findViewById(R.id.pointListView); SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem, R.layout.point_mgr_list_item, new String[] { "pointsID", "pointsStatu" }, new int[] { R.id.pointsID, R.id.pointsStatu }); list.setAdapter(listItemAdapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } }); } private Object getstatu(String str) { // TODO Auto-generated method stub if (str.equals("ON")) { return R.drawable.led_green; } else { return R.drawable.led_red; } }* * /************************* 为添加按钮添加事件 **************************/ void addbtn() { ImageButton ib_add = (ImageButton) findViewById(R.id.addpoint); ib_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LayoutInflater inflater = getLayoutInflater(); final View layout = inflater.inflate(R.layout.add_dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(PointMgrActivity.this) .setTitle("请添加节点") .setView(layout) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText et = (EditText) layout .findViewById(R.id.addID); gID = et.getText().toString(); pStatu = "OFF"; /***************************************** 将输入的id,状态写入map中 **************/ Log.i(PointMgrActivity.ACTIVITY_TAG, gID + " " + pStatu); // insert(db,gID,pStatu); // close(db); add(); query(); /******************** 调试代码 ********************************************/ Log.i(PointMgrActivity.ACTIVITY_TAG, gID); /***************************************************************************************/ } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } }); }
}
layout.xml布局文件中有listview,有 两列textview imageview ,
上面query()方法是将从sqlite查询的数据在listview显示,下面是适配器代码:
ListView list = (ListView) findViewById(R.id.pointListView);
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,
R.layout.point_mgr_list_item,
new String[] { "pointsID", "pointsStatu" },
new int[] { R.id.pointsID, R.id.pointsStatu });
list.setAdapter(listItemAdapter);
log调试得到正确从数据库查询数据,就是不listview显示数据.
运行报错:
10-23 10:56:36.670: E/BitmapFactory(12701): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
解决方案
适配器参数写错了
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem, R.layout.point_mgr_list_item, new String[] { "pid", "pstatu" }, new int[] { R.id.pointsID, R.id.pointsStatu }); list.setAdapter(listItemAdapter);