如何在listview中的edittext字段中显示值来自sqlite的值?

时间:2010-12-24 06:03:16

标签: android listview android-edittext

我创建了一个包含两列的表,并在其中插入一些行。现在我必须在listview中显示这些行。在textview中显示一列,并在edittext字段中显示该列相关值。但是我得到了一个名为(NullPointer Exception)的错误

我使用了下面显示的一些代码

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;

           }
           }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

创建一个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));

然后在你的适配器类的getView方法中做这样的事情:

EDIT_TEXT.setText(((YOUR_POJO_CLASS)list.get(i)).GET_INFORMATION());