我有自定义布局,带有复选框和2个textViews。
我正在构建另一个包含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();
}
}
答案 0 :(得分: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);
}
}
});