在 listView 中带有复选框的自定义布局:当我删除一行时,如果它被选中(checkBox = true)下一行得到检查 [英] Custom layout with checkbox in listView: When I delete a line if it was checked(checkBox = true) the next line gets the check

查看:22
本文介绍了在 listView 中带有复选框的自定义布局:当我删除一行时,如果它被选中(checkBox = true)下一行得到检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我删除一行时,如果它被checked(checkBox = true) 下一行得到检查.

When I delete a line if it was checked(checkBox = true) the next line gets the check.

我有几行,第二行检查第三行没有检查.我删除了第二行,然后第三行(现在是第二行)被选中......我不想在删除后检查它.

I have few lines, second line checked third NOT checked. I delete second line, and then the third(which is now the second) becomes checked... I dont want it to be checked after delete.

我在 onContextItemSelected() 中的活动代码中删除.

I delete in Activity code in onContextItemSelected().

适配器代码:

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 tempholder = new todoHolder();

        if(localView == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            localView = inflater.inflate(layoutResourceId, parent, false);
            tempholder.task = (TextView) localView.findViewById(R.id.txtTodoTitle);
            tempholder.date = (TextView) localView.findViewById(R.id.txtTodoDueDate);
            tempholder.status = (CheckBox) localView.findViewById(R.id.toDoChecked);
            localView.setTag(tempholder);
        }
        else
        {
            tempholder = (todoHolder) localView.getTag();
        }
        final todoHolder holder = tempholder;

        final TodoTask currentTask = data.get(position);
        final CheckBox status = (CheckBox) holder.status;
        status.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            changeColorOnCheckBox(holder, status, currentTask);
          }
        });
        if(!holder.status.isChecked()){
          if(isOverdue(currentTask)){
            holder.task.setTextColor(Color.RED);
            holder.date.setTextColor(Color.RED);
        }
        else{
          holder.task.setTextColor(Color.BLACK);
          holder.date.setTextColor(Color.BLACK);
        }
      }

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

    private static boolean isOverdue(TodoTask task){
      int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
      int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
      int currentYear = Calendar.getInstance().get(Calendar.YEAR);
      if(task.getYear() < currentYear || task.getMonth() < currentMonth || task.getDay() < currentDay)
        return true;
      return false;
    }

    private static void changeColorOnCheckBox(todoHolder holder, CheckBox cb, TodoTask newTask){
      if (cb.isChecked()) {
        holder.task.setTextColor(Color.GREEN);
        holder.date.setTextColor(Color.GREEN);
      }
      else{
        if(isOverdue(newTask)){
          holder.task.setTextColor(Color.RED);
          holder.date.setTextColor(Color.RED);
        }
        else{
          holder.task.setTextColor(Color.BLACK);
          holder.date.setTextColor(Color.BLACK);
        }
      }
    }

    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);
        if(item.getItemId() == R.id.menuItemAdd){
            Intent intent = new Intent(this, AddNewTodoItemActivity.class); 
            startActivityForResult(intent, BROWSER_ACTIVATION_REQUEST);
        }
        if(item.getItemId() == R.id.menuAbout){
            Toast.makeText(getApplicationContext(), "Made by Andrey Dobrikov", Toast.LENGTH_SHORT).show();
        }
        if(item.getItemId() == R.id.exitMenu){
            finish();
        }
        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 curTask = tasks.get(pos);
        String title = curTask.getTask();
        menu.setHeaderTitle(title); //set title for delete menu.

        MenuInflater inflater = getMenuInflater();
        if(firstWordCall(title) != -1){
            inflater.inflate(R.menu.context_delete_call, menu);
            MenuItem mi =(MenuItem) menu.findItem(R.id.menuItemCall);
            mi.setTitle(title);
        }
        else{
            inflater.inflate(R.menu.context_delete, menu);
        }
    }

    private int firstWordCall(String title) {
        String[] split = title.split(" ",2);
        String firstWord = split[0].toLowerCase();
        if(firstWord.equals("call")){
            String rest = split[1];
            rest = rest.replaceAll("\\s","");
            if(rest.startsWith("-")) return -1;
            rest = rest.replaceAll("-", "");
            if(rest.matches("^[0-9]+")){
                return Integer.parseInt(rest);
            }
        }
        return -1;
    }

    @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();
        }
        if(item.getItemId() == R.id.menuItemCall){
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + tasks.get(id).getNumber()));
            startActivity(callIntent);
        }
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent input) {
        // if the results is coming from BROWSER_ACTIVATION_REQUEST 
        String newTask;
        int number = -1;

        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");
                    if(newTask.toLowerCase().contains("call")){
                        number = firstWordCall(newTask);
                    }
                    TodoTask todoLine = new TodoTask(day, month, year, newTask, number);
                    newTask += day;
                    newTask += month;
                    newTask += year;
                    tasks.add(todoLine);
                    ad.notifyDataSetChanged();
                }
            }
        }
    }

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

推荐答案

经过更多调查我发现了问题:

After some more investigation i found the problem:

在我的适配器代码中:此代码需要更改:

In my adapter code: This code need to be changed:

final TodoTask currentTask = data.get(position);
final CheckBox status = (CheckBox) holder.status;
status.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    changeColorOnCheckBox(holder, status, currentTask);
  }
});

此代码:

final TodoTask currentTask = data.get(position);
final CheckBox status = (CheckBox) holder.status;

status.setChecked(currentTask.isChecked());
status.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        changeColorOnCheckBox(holder, status, currentTask);
    if(status.isChecked()){
        Log.w("----------", "CheckboxT");
    currentTask.setChecked(true);
    }
    else{
        Log.w("----------", "CheckboxF");
    currentTask.setChecked(false);
    }
    }
});

这将更新被按下的实际行的复选框.

This will update the actual line's check box that is being pressed.

非常感谢所有试图提供帮助的人!非常感谢!

Big thanks for all who was trying to help! Very appreciated!

这篇关于在 listView 中带有复选框的自定义布局:当我删除一行时,如果它被选中(checkBox = true)下一行得到检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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