问题描述
- 为什么listview绑定的数据为空
-
// 为点菜列表ListView绑定数据的方法private void setMenusAdapter(){
lv = (ListView) findViewById(R.id.orderDetailListView01);// 获得EditText,CODE
EditText code_et = (EditText)findViewById(R.id.editText1);
// 获得EditText,份数
EditText fenshu_et = (EditText)findViewById(R.id.editText2);
// CODE
String codeStr = code_et.getText().toString();
// 份数
String fenshuStr = fenshu_et.getText().toString();String selection="MenuCode="+codeStr;
final String[] projection = { "_id","MenuName", "MenuPrixPlace" };
// final String sortOrder = "_id ASC";
//cr.query(uri1, projection, selection, selectionArgs, sortOrder);
// 获得ContentResolver实例
ContentResolver cr = getContentResolver();
// 访问数据的Uri
Uri uri1 = Tables.CONTENT_URI;
// 删除本地SQLite数据库中菜谱表中的数据
// 要选择桌号表中的列
final Cursor c = cr.query(uri1, projection, selection, null, null);
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this,
R.layout.listview, c,
//new String[]{ "_id","MenuTypeID", "MenuTypeName" },
new String[]{"MenuName", "MenuPrixPlace" },
new int[] { R.id.mingzi_ListView,R.id.jiaqian_ListView});
// System.out.println(c);
// 获得LayoutInflater实例
LayoutInflater inflater = LayoutInflater.from(this);
// 实例化在弹出对话框中添加的视图
final View v = inflater.inflate(R.layout.listview, null);
// 获得TextView,菜价格
TextView name_tv = (TextView) v.findViewById(R.id.mingzi_ListView);
// 获得TextView,菜名称
TextView price_tv = (TextView) v.findViewById(R.id.jiaqian_ListView);
// 菜价格值
String priceStr = price_tv.getText().toString()+"元";
// 菜名称值
String nameStr = name_tv.getText().toString();
c.close();// 装到Map中
map = new HashMap();
map.put("code", codeStr);
map.put("fenshu", fenshuStr);
map.put("jiaqian", priceStr);
map.put("mingzi", nameStr);
//
// 添加到ListView
data.add(map);// 关联的TextView
to[0] = R.id.code_ListView;
to[1] = R.id.fenshu_ListView;
to[2] = R.id.jiaqian_ListView;
to[3] = R.id.mingzi_ListView;
// 实例化SimpleAdapter
sa = new SimpleAdapter(JiacaiActivity.this, data, R.layout.listview,
from, to);
// 为ListView绑定数据
lv.setAdapter(sa);
sa.notifyDataSetChanged();Toast.
makeText(JiacaiActivity.this, " priceStr "+priceStr+"nameStr"+nameStr, Toast.LENGTH_LONG).show();}
这里priceStr nameStr是空的 但是code份数有显示
Listview XML
<?xml version="1.0" encoding="utf-8"?>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="3.0dip"
android:background="@drawable/delete"
android:layout_centerVertical="true" android:focusable="false"
android:layout_alignParentRight="true" android:layout_marginRight="20dp"/>android:text=""
android:id="@+id/code_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:textColor="#F8EFFB"
/>android:text=""
android:id="@+id/fenshu_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:textColor="#F8EFFB"
/>android:text=""
android:id="@+id/mingzi_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:textColor="#F8EFFB"
/>
android:text=""
android:id="@+id/jiaqian_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:textColor="#F8EFFB"
/>
解决方案
priceStr是空的? String priceStr = price_tv.getText().toString()+"元";
确认下这里的priceStr 是什么,使用的时候还没有值吧