【问题标题】:Android: Custom layout with checkbox in list view instant change text colorAndroid:列表视图中带有复选框的自定义布局即时更改文本颜色
【发布时间】:2014-03-17 10:28:05
【问题描述】:

我有一个带有复选框和 2 个文本视图的自定义布局。

我正在构建另一个包含 listView 的布局。此 listView 行应使用自定义布局。

问题是。我想根据复选框状态立即更改 textViews 颜色:当我选中/取消选中颜色更改时。 但是只有当我添加另一个项目时颜色才会改变......

我的适配器代码:

public class TodoAdapter2 extends ArrayAdapter<TodoTask>{

    Context context; 
    int layoutResourceId;    
    ArrayList<TodoTask> data = null;

    public TodoAdapter2(Context context, int layoutResourceId, ArrayList<TodoTask> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View localView = convertView;
        todoHolder holder = null;
        if(localView == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            localView = inflater.inflate(layoutResourceId, parent, false);
            holder = new todoHolder();
            holder.task = (TextView) localView.findViewById(R.id.taskName);
            holder.date = (TextView) localView.findViewById(R.id.taskDate);
            holder.status = (CheckBox) localView.findViewById(R.id.toDoChecked);
            localView.setTag(holder);
        }
        else
        {
            holder = (todoHolder) localView.getTag();
        }

        TodoTask newTask = data.get(position);
        if(holder.status.isChecked()){
            holder.task.setTextColor(Color.GREEN);
            holder.date.setTextColor(Color.GREEN);
        }
        else{
            holder.task.setTextColor(Color.BLACK);
            holder.date.setTextColor(Color.BLACK);
        }



        holder.task.setText(newTask.getTask());
        holder.date.setText(newTask.toStringDate());
        return localView;
    }

    class todoHolder{
        CheckBox status;
        TextView task;
        TextView date;
    }
}

我的活动代码:

public class TodoListManagerActivity extends Activity {
    private ArrayList<TodoTask> tasks;
    private ListView list;
    TodoAdapter2 ad;
    final int BROWSER_ACTIVATION_REQUEST = 2; // request code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tasks = new ArrayList<TodoTask>();
        setContentView(R.layout.activity_todo_list_manager);
        list = (ListView) findViewById(R.id.lstTodoItems);

        ad = new TodoAdapter2(this, R.layout.todoline_layout, tasks);


        list.setAdapter(ad);

        registerForContextMenu(list);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate the menu. this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.todo_list_manager_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //What happens when we click the menu.
        super.onOptionsItemSelected(item);
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //To hide keyboard after addition.
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //To hide keyboard.
        if(item.getItemId() == R.id.menuItemAdd){
            Intent intent = new Intent(this, AddNewTodoItemActivity.class); 
            Log.w("Proc", "Before start");
            startActivityForResult(intent, BROWSER_ACTIVATION_REQUEST);

        }
        if(item.getItemId() == R.id.menuAbout){
            Toast.makeText(getApplicationContext(), "ABOUT ME", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        int pos = info.position;
        TodoTask title = tasks.get(pos);
        menu.setHeaderTitle(title.getTask()); //set title for delete menu.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context, menu);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent input) {
        Log.w("PROC", "REturn");
        // if the results is coming from BROWSER_ACTIVATION_REQUEST 
        String newTask;
        if (requestCode == BROWSER_ACTIVATION_REQUEST) {

            // check the result code set by the activity
            if (resultCode == RESULT_OK) {
                if(input.hasExtra("Task") && input.getExtras().getString("Task") != null){
                    newTask = input.getExtras().getString("Task");
                    int day = input.getExtras().getInt("Day");
                    int month = input.getExtras().getInt("Month");
                    int year = input.getExtras().getInt("Year"); 
                    TodoTask todoLine = new TodoTask(day, month, year, newTask);
                    newTask += day;
                    newTask += month;
                    newTask += year;
                    tasks.add(todoLine);
                    ad.notifyDataSetChanged();
                    Log.w("--------------", "UPDATED"); 
                }
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int id = info.position;
        if(item.getItemId() == R.id.menuItemDelete){
            String itemToDelete = tasks.get(id).getTask();
            tasks.remove(id);
            ad.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Item: [" + itemToDelete + "] was successfully deleted!", Toast.LENGTH_SHORT).show();
        }
        return super.onContextItemSelected(item);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }
}

【问题讨论】:

    标签: android android-layout listview checkbox


    【解决方案1】:

    一个简单的解决方案是在适配器中放置一个 onClickListener

    final CheckBox cb = (CheckBox)    holder.status;
    cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cb.isChecked()) {
                        holder.task.setTextColor(Color.GREEN);
                        holder.date.setTextColor(Color.GREEN);
                    } else {
                        holder.task.setTextColor(Color.BLACK);
                        holder.date.setTextColor(Color.BLACK);
                    }
                }
            });
    

    【讨论】:

    • 您好,感谢您的回答,但我对此回答有疑问,它希望我将持有人设为最终版本,然后我无法拆分适配器内的流量。 (如果(localView == null),否则)。您对如何解决这个问题有什么建议吗?
    • 还有一个问题:现在,当我删除一行时,复选框“跳”到下一行...我找不到解决方法...请帮忙!
    • 你能提出一个新问题并提供代码吗?我不知道你的意思是什么,在注释中写代码不容易阅读!
    • 您好,感谢您的回复,我创建了新问题:stackoverflow.com/questions/22472913/…
    猜你喜欢
    • 2018-11-24
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多