无法使用持有人

时间:2016-03-19 06:04:44

标签: android listview

当我尝试使用自定义适配器将值设置到列表视图时,它没有显示哈希映射中的数据。我想在列表视图中显示的值来自数据库。我的列表视图包含文本视图和编辑文本。 我的代码是

    private void SelectItems() {
    // TODO Auto-generated method stub
    try {

           datas = new ArrayList<Map<String, String>>();
        DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();
        Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
                + " from itemmaster", null);

        Log.v("item detailss", c.toString());

            while (c.moveToNext()){

            HashMap<String, String> datanums = new HashMap<String, String>();
            String im_code = c.getString(c.getColumnIndex("im_code"));

             String desc = c.getString(c.getColumnIndex("im_desc"));

             datanums.put("codec", im_code);
             datanums.put("namec", desc);


       String price = c.getString(c.getColumnIndex("im_srp"));
            datanums.put("imsrpc", price);


            }
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
           } 

}

private void ViewItems() {
    // TODO Auto-generated method stub
    list.setAdapter(null);
     arrTemp = new String[datas.size()];
     datas.add(datanums);

     MyListAdapter myListAdapter = new MyListAdapter();
    list.setAdapter(myListAdapter);
    Log.v("list itemm",datas.toString());
    }

private class MyListAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(datas != null && datas.size() != 0){
            return datas.size();    
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //ViewHolder holder = null;
        final ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            LayoutInflater inflater = Orders.this.getLayoutInflater();
            convertView = inflater.inflate(R.layout.order_simple_row, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.name);
            holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
            holder.textview3 = (TextView) convertView.findViewById(R.id.code);
            holder.editText1 = (EditText) convertView.findViewById(R.id.cases);    
            holder.editText2 = (EditText) convertView.findViewById(R.id.pcs); 


            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.ref = position;

        holder.textView1.setText(datanums.get("namec"));
        holder.textView2.setText(datanums.get("imsrpc"));
        holder.textview3.setText(datanums.get("codec"));
        holder.editText1.setText(arrTemp[position]);
        holder.editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                arrTemp[holder.ref] = arg0.toString();
            }
        });

        return convertView;
    }

    private class ViewHolder {

        TextView textView1,textView2,textview3;
        EditText editText1,editText2;
        int ref;
    }

}

在这个viewitems()和selectitems()中,是从oncreate调用的方法,用于在列表视图中检索和显示值。我无法解决确切的问题。但编辑文本在output.please帮助中正确显示。

3 个答案:

答案 0 :(得分:1)

在getView方法的开头添加以下行

HashMap<String, String> datanums=datas.get(possition);

答案 1 :(得分:0)

 public List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {

       datas = new ArrayList<Map<String, String>>();
    DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    newDB = dbHelper.getWritableDatabase();
    Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
            + " from itemmaster", null);

    Log.v("item detailss", c.toString());

        while (c.moveToNext()){

        HashMap<String, String> datanums = new HashMap<String, String>();
        String im_code = c.getString(c.getColumnIndex("im_code"));

         String desc = c.getString(c.getColumnIndex("im_desc"));

         datanums.put("codec", im_code);
         datanums.put("namec", desc);


   String price = c.getString(c.getColumnIndex("im_srp"));
        datanums.put("imsrpc", price);


        }
    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
       } 


return datas;

}

/ *在您的Activity或适配器类中调用此方法(这将返回列表)并检查条件if(arraylist!= null&amp;&amp; arraylist.size()&gt; 0)* /

答案 2 :(得分:0)

使用此代码设置文字视图。希望这有效。

holder.textView1.setText(datas.get(position).get("namec"));
holder.textview3.setText(datas.get(position).get("codec"));
holder.textview3.setText(datas.get(position).get("srpc"));