Android 如何在listview的edittext字段中显示来自sqlite的值?
我创建了一个包含两列的表,并在其中插入了一些行。现在我必须在listview中显示这些行。一列显示在textview中,与该列相关的值显示在edittext字段中。但我得到了一个名为(空指针异常)的错误 我使用了如下所示的一些代码Android 如何在listview的edittext字段中显示来自sqlite的值?,android,listview,android-edittext,Android,Listview,Android Edittext,我创建了一个包含两列的表,并在其中插入了一些行。现在我必须在listview中显示这些行。一列显示在textview中,与该列相关的值显示在edittext字段中。但我得到了一个名为(空指针异常)的错误 我使用了如下所示的一些代码 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.category1
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category1);
lv = (ListView)findViewById(android.R.id.list);
getList();
lv.setAdapter(new EfficientAdapter(this));
}
public void getList()
{
cur=db.rawQuery("SELECT * FROM detaillist ORDER BY symbol",null);
while(cur.moveToNext())
{
sy = cur.getString(cur.getColumnIndex("symbol"));
po = cur.getString(cur.getColumnIndex("position"));
}
strArray = new String[]{sy};
strArray1 = new String[]{po};
}
}
这是我的适配器类
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return symbols.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
convertView = mInflater.inflate(R.layout.editcategorylist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.symbol);
holder.text2 = (EditText) convertView.findViewById(R.id.postion);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(strArray[position]);
Log.e("text",holder.text.toString());
holder.text2.setText(strArray1[position]);
Log.e("text2",holder.text2.toString());
return convertView;
}
static class ViewHolder {
TextView text;
EditText text2;
}
}
}
谢谢。创建一个POJO类,并为符号和位置创建构造函数、getter和setter。接下来要做的事情如下:
List<YOUR POJO CLASS> myList = new ArrayList<>(); //create a simple list before accessing data from query
myList.add(new YOUR_POJO_CLASS(symbol, position));
EDIT_TEXT.setText(((YOUR_POJO_CLASS)list.get(i)).GET_INFORMATION());
一旦您进行了查询,您应该调用cur.moveToFirst()。的副本