1

One of my activities requires a list view with custom number pickers in each row. I created the number picker as suggested here. When I click on the buttons, however, the counter doesn't increment or decrement at all.

The following is the code found at my Custom ListView Adapter.

public class MaterialListViewAdapter extends BaseAdapter 
{
    ViewHolder holder;
    int counter = 0;

    private ArrayList<MaterialClass> data;

    public static LayoutInflater inflater = null;

    public static Dialog dialog;
    String materialName;

    public MaterialListViewAdapter(Context applicationContext,
       int materialdialogcontent, ArrayList<MaterialClass> materials) 
    {
       this.data = materials;
        inflater = (LayoutInflater)applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() 
    {
        return data.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return position;
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
   }

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

        View vi = convertView;

        //ViewHolder holder = new ViewHolder();

        if(vi == null)
        {
            holder = new ViewHolder();

            vi = inflater.inflate(R.layout.materialdialogcontent, null);

            //Initialize Buttons and TextViews.

            holder.num.setText("0");

            holder.add.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                    counter++;
                    holder.num.setText("" + counter);
                }
            });

            holder.sub.setOnClickListener(new View.OnClickListener()
            {   
                @Override
                public void onClick(View v) 
                {
                    counter--;
                    holder.sub.setText("" + counter);
                }
            });


            //holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);

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

        holder.txt.setText(data.get(position).getName());
        //holder.pk.setMaxValue(20);
        //holder.pk.setMinValue(0);

        return vi;
    }

    private class ViewHolder
    {
        TextView txt;
        Button add;
        Button sub;
        TextView num;
        //NumberPicker pk;
    }

}

When I try debugging to see what's going on, counter doesn't even initialize with '0'.

I tried initializing the counter in the getView() method, however in order to do so I need to set the counter to final, and then another error shows up saying

The final local variable counter cannot be assigned, since it is defined in an enclosing type.

Any suggestions?

Updated for additional code:

public class Material extends Activity 
    {
    ArrayList<String> materialList;
    ListView lv;
    Button btnConfirm;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_material);

        lv = (ListView)findViewById(R.id.list);
        btnConfirm = (Button)findViewById(R.id.btnConfirm);

        materialList = new ArrayList<String>();
        Intent i = getIntent();

        materialList = i.getStringArrayListExtra("materialList");

        if(materialList != null)
        {
            Toast t = Toast.makeText(getApplicationContext(), materialList.get(0).toString(), Toast.LENGTH_LONG);
            t.show();
        }
        else
        {
            //FILL HERE LATER
        }

        ArrayList<MaterialClass> materials = new ArrayList<MaterialClass>();
        for(String temp : materialList)
        {
            MaterialClass m = new MaterialClass();
            m.setName(temp);
            materials.add(m);
        }

        MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);
        lv.setAdapter(adapter);
    }

}
Community
  • 1
  • 1
ClaireG
  • 1,244
  • 2
  • 11
  • 23

2 Answers2

0

Try this :

public class MaterialListViewAdapter extends BaseAdapter 
{
    ViewHolder holder;
    int counter = 0;

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

        View vi = convertView;

        //ViewHolder holder = new ViewHolder();

        if(vi == null)
        {
            holder = new ViewHolder();

            vi = inflater.inflate(R.layout.materialdialogcontent, null);

            //Initialize Buttons and TextViews.
            //holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);

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

        holder.txt.setText(data.get(position).getName());
        //holder.pk.setMaxValue(20);
        //holder.pk.setMinValue(0);

        holder.add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                counter++;
                holder.num.setText("" + counter);
            }
        });

        holder.sub.setOnClickListener(new View.OnClickListener()
        {   
            @Override
            public void onClick(View v) 
            {
                counter--;
                holder.num.setText("" + counter);
            }
        });

        return vi;
    }

    private class ViewHolder
    {
        TextView txt;
        Button add;
        Button sub;
        TextView num;
        //NumberPicker pk;
    }

}
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0

You should implement the onItemClickListener for your listView . You are using listener in adapter class but practically those numbers are nothing but items of that listView, so that to operate on those buttons (listView's items) you've not defined onItemClickListener. So after setting adapter write

lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View item, int position, long id) {

         // here you are getting position of clicked item
        }      
}

UPDATE:

I've gone through your code again and I found these:

1.you are writing this line:

MaterialListViewAdapter adapter = new MaterialListViewAdapter(getApplicationContext(), R.layout.materialdialogcontent, materials);

but I didn't see any constructor in your Adapter.

2.As you are extending BaseAdapter, you must override getCount() , getItem(int position) and getItemId(int position) method as like getView() . There you are getting issue.

imthegiga
  • 1,126
  • 8
  • 13
  • @giga_abih I do not need to get the position of the item, nor I need to click on the list item. I just need to click on a button which is present inside the listview. Do I still need to do as you suggested? – ClaireG Aug 09 '13 at 08:51
  • that's what I'm saying, buttons inside listview are nothing but they are items of that listview. Now to click on that item you should implement it's listener. Also make sure that buttons you using are clickable. In XML inside button element put `android:clickable="true"` – imthegiga Aug 09 '13 at 08:55
  • @ClaireG are you getting any view inside your list ?? It's because you've adding data into adapter, setting that adapter for listView. Also you've implemented `onItemClickListener` for listView. But where's adapter's `notifyDatasetChanged()` method ? write `adapter.notifyDataSetChanged();` after `lv.setOnItemClickListener`. So that data will be reflected into listView. Let us know if issue persist. – imthegiga Aug 09 '13 at 10:01
  • Thank you for your suggestions @giga_abhi. No, I am not getting any views inside list. Also I tried `adapter.notifyDataSetChanged();` and still nothing happens. I know there's something wrong with the passing of ids and the Adapter class but cannot find out what it is. When I had the onClickListeners inside the adapter class, the counter was incrementing correctly, and the debugger variable was indicating that the TextView's text was set to '1' (for example) but the number wasn't actually being displayed in the textview. – ClaireG Aug 09 '13 at 10:09
  • Also, as a side note, when I breakpoint the code at the listview click listener, it doesn't stop there when i click on the listview. Does that mean that the code there is useless? – ClaireG Aug 09 '13 at 10:13
  • @ClaireG please go though updated answer and let us know. Thank you. – imthegiga Aug 09 '13 at 11:10
  • Yes I do have the constructor and the other Overriden methods. I just didn't find a reason why I should post them. Please take a look at my updated original post. I will include them now. – ClaireG Aug 09 '13 at 12:20