Android, how to redraw list when the user sets checkbox?

被刻印的时光 ゝ 提交于 2019-12-23 04:34:28

问题


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.

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?

my code is:

    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);
        }
    }
}

Update: I have added

@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.


回答1:


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

UPDATE

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.

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




回答2:


You probably need to handle the click from the checkbox itself and not just the listview.



来源:https://stackoverflow.com/questions/8320663/android-how-to-redraw-list-when-the-user-sets-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!