2012-04-08 4 views
-1

Я создаю собственный список, и я хочу сохранить текст некоторого TextFile, который находится в файле R.id.quantity.Сохранение текста и флажка в пользовательском списке

Я также хочу сохранить состояние флажка. Я попытался управлять проверенным состоянием флажка в списке.

Список содержит другие элементы управления представлением. Я поместил их в класс Viewholder. Я инициализирую qtyTxtV с некоторым значением начального массива, как только событие возникает в этом коде, значение массива изменяется, но содержимое списка стирается. Пожалуйста, помогите.

Теперь этот код дает мне исключения нулевого указателя:

class MyAdapter1 extends BaseAdapter { 
Context context; 
ArrayList<Integer> price = new ArrayList<Integer>(); 
ArrayList<String> names = new ArrayList<String>(); 
public static HashMap<Integer, String> myList = new HashMap<Integer, String>(); 
static class ViewHolder { 
    public TextView tvQuantityName, tvPrice, tvDishName; 
    public ImageView imv1, imv2, imv3, imv4, imv5; 
    public EditText edtQty; 
    public CheckBox chkAdd; 
} 
public MyAdapter1(VegDetail vegDetail, ArrayList<String> names2, 
     ArrayList<Integer> price2) { 
    this.context = vegDetail; 
    this.names = names2; 
    this.price = price2; 
    } 
@Override 
public int getCount() { 
    return names.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 
@Override 
public long getItemId(int position) { 
    return 0; 
} 
@SuppressWarnings("null") 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    View row = convertView; 
    ViewHolder holder = null; 

    // View row = inflater.inflate(R.layout.rowlayout, parent, false); 
    if (row == null) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
      row = inflater.inflate(R.layout.rowlayout, parent, false); 
     holder.edtQty = (EditText) row.findViewById(R.id.quantity); 
     holder.imv1 = (ImageView) row.findViewById(R.id.star1); 
     holder.imv2 = (ImageView) row.findViewById(R.id.star2); 
     holder.imv3 = (ImageView) row.findViewById(R.id.star3); 
     holder.imv4 = (ImageView) row.findViewById(R.id.star4); 
     holder.imv5 = (ImageView) row.findViewById(R.id.star5); 
     holder.tvDishName = (TextView) row.findViewById(R.id.item_name); 
     holder.tvPrice = (TextView) row.findViewById(R.id.price1); 
     holder.tvQuantityName = (TextView) row.findViewById(R.id.qtyTxtV); 
     holder.chkAdd = (CheckBox) row.findViewById(R.id.chkAdd); 
     EditText edtQty = (EditText) row.findViewById(R.id.quantity); 
     edtQty.setText(VegDetail.qtyArray[position].toString()); 
     CheckBox chkAdd = (CheckBox) row.findViewById(R.id.chkAdd); 
     chkAdd.setSelected(VegDetail.chkArray[position]);   
     holder.edtQty.addTextChangedListener(new TextWatcher() { 
      public void onTextChanged(final CharSequence s, 
        final int start, final int before, final int count) { 
      } 
      public void afterTextChanged(final Editable s) { 
       int newValue; 
       final String boxContents = s.toString(); 
       if (!boxContents.isEmpty()) { 
        try { 
         newValue = Integer.parseInt(boxContents); 
         VegDetail.qtyArray[position] = newValue; 
        } catch (final Exception exc) { 
         VegDetail.qtyArray[position] = 0; 
        } finally { 
        } 
       } else { 
        VegDetail.qtyArray[position] = 0; 
       } 
      } 
      public void beforeTextChanged(final CharSequence s, 
        final int start, final int count, final int after) { 
      } 
     }); 

     row.setTag(holder); 
    } else { 
     holder = (ViewHolder) row.getTag(); 
    } 

    CheckBox chkAdd=(CheckBox)row.findViewById(R.id.chkAdd); 
    chkAdd.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      //VegDetail is another class where I gonna use checked list's states 
      VegDetail.chkArray[position] = isChecked;    
     } 
    }); 
    final TextView tv1 = (TextView) row.findViewById(R.id.item_name); 
    TextView tv2 = (TextView) row.findViewById(R.id.price1); 
    final TextView tvQty = (TextView) row.findViewById(R.id.quantity); 
    tv1.setText(names.get(position)); 
    tv1.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, 
        "itme name is=>" + tv1.getText().toString(), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 
    tv2.setText("Rs:" + price.get(position)); 
    return row; 
} 
} 
+1

паста logcat trace ... он указывал бы исключение Nullpointer в некоторой строке no. , укажите, что строка №. в вашем коде тоже –

+0

в строке holder.imv1 = (ImageView) row.findViewById (R.id.star1); – Pratik

ответ

0

NPE должен быть строкой:

holder.edtQty = (EditText) row.findViewById(R.id.quantity); 

поскольку holder является NULL, как вы только что определили выше ... ViewHolder holder = null