Android的,如何重绘列表,当用户设置复选框? [英] Android, how to redraw list when the user sets checkbox?

查看:188
本文介绍了Android的,如何重绘列表,当用户设置复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图每行包含两个textviews和一个复选框。 我需要的用户组复选框只有一个,如果用户设置其他复选框,previous复选框应该被清零,第二个复选框可以设置。

I have a list view and each row contains two textviews and a checkbox. I need user sets just one of checkboxes and if user sets other checkbox, previous checkbox should be cleared and second checkbox can be set.

我可以生成列表,但我不知道为什么 onListItemClick()方法不起作用?因此,我不知道是哪个复选框被设置。

I can generate the list but i don't know why onListItemClick() method does not work? therefore, I don't know which checkbox is set.

和我的下一个问题是,点击复选框用户后,我怎么能重绘列表?

and my next question is, how can i redraw list after clicking checkbox by user?

我的code是:

    package com.Infindo.DRM;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class Infindo_PackageActivity extends ListActivity {

    private final String[] titels = {"Try before buy", "Pay per play", "Pay per day", "Pay per week", "Pay per month", "Daily subscription", "Weekly subscription", "Monthly subscription"};
    private final String[] descriptions = {"Free of charge", "Price: $1.00", "Price: $5.00", "Price: $10.00", "Price: $30.00", "Price: $5.00", "Price: $10.00", "Price: $30.00"};
    private String[] flag = {"false", "false", "false", "false", "false", "false", "false", "false"};
    private ListAdapter listAdapter;

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

        listAdapter = new ListAdapter(this);
        setListAdapter(listAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.i("List Clicked:", "******");
    }

    public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
    }


    //*********************
    //  RowModel Class
    //*********************
    private class RowModel{
        TextView title;
        TextView description;
        CheckBox checkbox;
    }

    //*********************
    //  ListAdapter Class
    //*********************
    private class ListAdapter extends ArrayAdapter<String>{

        public ListAdapter(Context c) {
            super(c, R.layout.infindo_listformat, titels);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            RowModel holder;
            View row = convertView;

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.infindo_listformat, parent, false);

                holder = new RowModel();
                holder.title = (TextView) row.findViewById(R.id.title);
                holder.description = (TextView) row.findViewById(R.id.description); 
                holder.checkbox = (CheckBox) row.findViewById(R.id.checkbox);

                row.setTag(holder);

            } else {
                holder = (RowModel) row.getTag(); 
            }

            holder.title.setText(titels[position]);
            holder.description.setText(descriptions[position]);
            String s = flag[position];
            if(s.equalsIgnoreCase("false")) 
                holder.checkbox.setChecked(false);
            else
                holder.checkbox.setChecked(true);

            return(row);
        }
    }
}

更新: 我已经加入

@Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            Log.i("List Redrawed:", "**notifyDataSetChanged()**");
        }

进入我的列表适配器类,并在把它称为

into my List Adapter class and called it in

public void onCheckboxClicked(View v){
        int i = getSelectedItemPosition();
        Log.i("item pos:", String.valueOf(i)); // every time the result is -1???
        listAdapter.notifyDataSetChanged();
    }

我觉得重绘的问题就解决了​​。但我仍然不知道是由用户点击,以改变旗阵什么复选框。

I think the problem of redraw is solved. but I still I don't know what checkbox is clicked by the user in order to change flag array.

推荐答案

您可以使用 setOnCheckedChangeListener 来知道哪些复选框被选中。你可以看一下ListView控件使用复选框这里

You can use setOnCheckedChangeListener to know which checkbox was selected. You can look at complete example of ListView with CheckBox here.

更新

为使您的 onListItemClick()工作,你需要写机器人:可调焦=假其他项目ListView控件,其因其他意见的重点ListView的 onListItemClick()未peforming理所应当执行。

To make your onListItemClick() work you need to write android:focusable="false" for other items of the ListView, its because of the focus of other views ListView's onListItemClick() is not peforming as it should be performing.

结帐这个答案,为什么<一href="http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items">Android自定义的ListView无法单击项目

Checkout this answer, why Android custom ListView unable to click on items

这篇关于Android的,如何重绘列表,当用户设置复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆