开发者

Change number in listview with button(s) on same row

I have created a c开发者_开发技巧ustom ListActivity that shows a list of skills and buttons next to them as well as a number. The list looks something like this:

Change number in listview with button(s) on same row

The skill names are drawn from a database as are the points (PTS). I need help figuring out how to set the buttons on the side so they are clickable and they increase or decrease their related points column number.

I'll try for the double bonus and ask if anyone knows how I can then retrieve all these values and update the database they are drawn from, possibly on a "Finalize" button at the bottom of the screen.


After inflate your XML view use findViewById to find your buttons and then add an onClickListener()

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

...
final Skill s = (Skill) getItem(position);

if (convertView == null) {
    LayoutInflater li = (LayoutInflater) this.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = li.inflate(R.layout.yourlayout, null);
} else {
  // use cached view
}

Button buttonInc = (Button) convertView.findViewById(R.id.buttonInc);
buttonInc.setOnClickListener(new View.OnClickListener() {
         public boolean onClick(View v) {
                // call to your core class to increase your data (skill object),    
                // preferable using AsyncTask.
         });


Button buttonDec = (Button) convertView.findViewById(R.id.buttonDec);
buttonDec.setOnClickListener(new View.OnClickListener() {
         public boolean onClick(View v) {
                // call to your core class to decrease your data (skill object),
                // preferable using AsyncTask.
         });

....
return convertView;
}

Note: remember to cache your view.

Hope this help


Some assumtions:

  • You have a view layout named skillLayout
  • You have into the the view two buttons named skillUp and skillDown
  • Skill class is like below

    private class Skill {
    
        private int skill = 0;
    
        public void skillUp() {
            skill++;
        }
    
        public void skillDown() {
            skill--;
        }
    }
    

Before to c&p code below you must create a ids.xml file int res/values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="skillId"/>
</resources>

Then use this code as your skill adapter

class SkillAdapter extends ArrayAdapter<Skill> {

    public SkillAdapter(Context context, int textViewResourceId,
            List<Skill> skills) {
        super(context, textViewResourceId, skills);
    }

    private final View.OnClickListener skillUpListener = new View.OnClickListener() {
        public void onClick(View v) {
            // Note: a better approach may be call to a method 
            // into your application base class.
            Skill s = (Skill) v.getTag(R.id.skillId);
            s.skillUp();
        }
    };

    private final View.OnClickListener skillDownListener = new View.OnClickListener() {
        public void onClick(View v) {
            // Note: a better approach may be call to a method 
            // into your application base class.
            Skill s = (Skill) v.getTag(R.id.skillId);
            s.skillDown();
        }
    };

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

        final Skill s = getItem(position);

        RecycledView recycled = null;

        if (convertView == null) {

            LayoutInflater li = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = li.inflate(R.layout.skillLayout, null);

            recycled.buttonUp = (Button) convertView
                    .findViewById(R.id.skillUp);
            recycled.buttonDown = (Button) convertView
                    .findViewById(R.id.skillDown);

            recycled.buttonUp.setOnClickListener(skillUpListener);
            recycled.buttonDown.setOnClickListener(skillDownListener);

        } else {
            recycled = (RecycledView) convertView.getTag();
        }

        recycled.buttonUp.setTag(R.id.skillId, s);
        recycled.buttonDown.setTag(R.id.skillId, s);

                    // Fill the row list with your data

        return convertView;
    }
}

    // For recycling stuff
static class RecycledView {
    Button buttonUp;
    Button buttonDown;
}

I hope this clarifies

Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜