其他分享
首页 > 其他分享> > android – 单击按钮在列表项的textview中设置文本

android – 单击按钮在列表项的textview中设置文本

作者:互联网

我创建了一个列表视图,如下图所示.链接:

Image

问题:我希望如果用户点击按钮或 – 当前显示0的文本视图相应地递增或递减.但是,如果我单击第一个项目的按钮,则会在其他行中更新textview.我不知道如何实现这一点.

这是代码:

public class HomePage extends Fragment {

    String[] listitems;
    int[] images = { R.drawable.cadburysilk, R.drawable.cadburys_dairymilk,
            R.drawable.perk, R.drawable.kitkat,
            R.drawable.nestlemunchchocolate, R.drawable.cadbury_bournville_bar,
            R.drawable.snickers };
    ListView list;
    DBAdapter dbadapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View rootview = inflater.inflate(R.layout.activity_home_page,
                container, false);
        View view = View.inflate(getActivity(), R.layout.headerview, null);

        Resources res = getResources();
        listitems = res.getStringArray(R.array.items);
        list = (ListView) rootview.findViewById(R.id.itemslist);
        list.addHeaderView(view);
        AdapterClass adapterClass = new AdapterClass(this.getActivity(),
                listitems, images);
        list.setAdapter(adapterClass);
        return rootview;
    }

}

class AdapterClass extends ArrayAdapter<String> {
    Context context;
    int[] images;
    String[] names;
    Button add, sub;
    TextView number;
    Integer count=0;

    static class ViewHolder {
        public TextView textView;
        public ImageView imageView;

    }

    public AdapterClass(Context c, String[] items, int imgs[]) {
        // TODO Auto-generated constructor stub
        super(c, R.layout.rowlayout, R.id.quantity, items);
        this.context = c;
        this.images = imgs;
        this.names = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        // reuse views

        if (row == null) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.rowlayout, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) row.findViewById(R.id.img);
            viewHolder.textView = (TextView) row.findViewById(R.id.textView1);
            add = (Button) row.findViewById(R.id.button1);
            add.setTag(position);
            sub = (Button) row.findViewById(R.id.button2);
            sub.setTag(position);
            number = (TextView)row.findViewById(R.id.quantity);

            row.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) row.getTag();
        holder.imageView.setImageResource(images[position]);
        holder.textView.setText(names[position]);
        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                count=count+1;
                number.setText(count.toString());
            }
        });

        sub.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                count=count-1;
                number.setText(count.toString());
            }
        });
        return row;
    }



}

请帮忙.此外,我需要帮助如何显示textview包含下一个活动或片段中的正数的项目.

解决方法:

按照用户370305的回答进行初始查询.

Please help. Also i need help in how to show the items whose textview contains positive number in next activity or fragment.

创建一个数据结构,我正在使用HashMap,其中key是String.valueOf(position),Value是count int.

你知道密钥,因为它将从位置“0”到listitem.length.使用密钥,您可以获取计数值并检查正数.

为什么选择Hashmap:因为每次更改计数时,新值都将被覆盖. (在hashmap中只维护一个唯一键).

(position = 0,count = 0) – > – 点击第0行 – > (位置0,计数= -1)将在key = 0单元格上重写.

完整代码(具有数字增量修复并在DataStructure中具有一组正值):

public class HomePage extends Fragment { 

    String[] listitems;
    int[] images = { R.drawable.cadburysilk, R.drawable.cadburys_dairymilk,
            R.drawable.perk, R.drawable.kitkat,
            R.drawable.nestlemunchchocolate, R.drawable.cadbury_bournville_bar,
            R.drawable.snickers };
    ListView list;
    DBAdapter dbadapter;

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub 

        View rootview = inflater.inflate(R.layout.activity_home_page,
                container, false);
        View view = View.inflate(getActivity(), R.layout.headerview, null);

        Resources res = getResources();
        listitems = res.getStringArray(R.array.items);
        list = (ListView) rootview.findViewById(R.id.itemslist);
        list.addHeaderView(view);
        AdapterClass adapterClass = new AdapterClass(this.getActivity(),
                listitems, images);
        list.setAdapter(adapterClass);
        return rootview;

        //You can use adapterClass.getPositiveNumber(); to retrieve all positive rows and the values!
        //Unique key will be String : 1,2,3....listitems.length. 

    } 

} 

class AdapterClass extends ArrayAdapter<String> {
    Context context;
    int[] images;
    String[] names;
    Button add, sub;
    ViewHolder holder; // Since will be accessed inside onclick

    HashMap<String,Integers> positiveNumbers = new HashMap<String,Integers>;
    Integer count=0;

    static class ViewHolder { 
        public TextView textView;
        public ImageView imageView;
        public TextView number;
        public String uniqueKey;

    } 

    public AdapterClass(Context c, String[] items, int imgs[]) {
        // TODO Auto-generated constructor stub 
        super(c, R.layout.rowlayout, R.id.quantity, items);
        this.context = c;
        this.images = imgs;
        this.names = items;
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub 
        View row = convertView;
        // reuse views 

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.rowlayout, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) row.findViewById(R.id.img);
            viewHolder.textView = (TextView) row.findViewById(R.id.textView1);
            viewHolder.number = (TextView)row.findViewById(R.id.quantity);
            viewHolder.uniqueKey = String.valueOf(position);
            add = (Button) row.findViewById(R.id.button1);
            add.setTag(position);
            sub = (Button) row.findViewById(R.id.button2);
            sub.setTag(position);
            row.setTag(viewHolder);

        } 
        holder = (ViewHolder) row.getTag();  //keeping one global memory
        holder.imageView.setImageResource(images[position]);
        holder.textView.setText(names[position]);
        add.setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v) {
                // TODO Auto-generated method stub  
                count=count+1;
                holder.number.setText(count.toString());
                positiveNumbers.put(holder.uniqueKey,count); //Key -> String.valueOf(position) and Value -> int count
            } 
        }); 

        sub.setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v) {
                // TODO Auto-generated method stub 
                count=count-1;
                holder.number.setText(count.toString());
                positiveNumbers.put(holder.uniqueKey,count);   //Key -> String.valueOf(position) and Value -> int count
            } 
        }); 

        return row;
    } 


     public Map<String,Integer> getPositiveNumbers()
     {
       return positiveNumbers;
     }


}

附:我输入了编辑器,可能会有一些编译时错误.此外,您可以将每行的对象存储在键上,这样当您获得正数时,您可以获得任何其他参考,例如图像,我猜你需要稍后显示!

标签:listviewitem,android,listview,android-listview
来源: https://codeday.me/bug/20191003/1846540.html