我创建了一个ListView,它显示了使用ArrayList和ArrayAdapter的待办事项。当我单击一个任务时,它正在从待办任务列表中删除,并且被添加到另一个完成待办任务列表的ArrayList中。我想做的是,将任务添加到已完成的待办事项列表中,并使用不同的文本颜色。
我为完成的任务创建了另一个XML,但是我还需要获取文本颜色值以进行下一步比较。根据颜色,稍后我将添加要执行的任务和已完成的任务以及另一个列表,它们需要以不同的颜色显示。
当我在应用程序中单击“显示已完成的任务”时,我也尝试了覆盖getView方法以访问setter和getter方法,但它给了我一个例外(在text.setTextColor(Color.RED);下面的行上)。 >
我想不通,希望有人帮我弄清楚:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mainLayout;
private AlertDialog.Builder dialogBuilder, confirmBuilder;
private AlertDialog dialog, confirmDialog;
private ArrayList<String> todoList, completedList, allList;
private ArrayAdapter<String> todoAdapter,completedAdapter, allAdapter;
private ListView listView;
private TextView txt;
private boolean isCompletedJob;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isCompletedJob=false;
listView = findViewById(R.id.listView1);
mainLayout = findViewById(R.id.drawer_layout); //FIND THE LAYOUT INCLUDING MAIN CONTENT AND NAVIGATION MENU
dialogBuilder = new AlertDialog.Builder(this); //CREATE A BUILDER TO CREATE A DIALOG
dialogBuilder.setTitle("Enter a job to do:"); //CREATE DIALOG TITLE
final EditText input = (EditText) new EditText(this); //CREATE TEXT TO ADD BUILDER'S BODY
dialogBuilder.setView(input); //ADD TEXT VIEW INTO DIALOG'S BODY
/*---DIALOG BUILDER ARACIYLA DIALOG'A OK VE CANCEL BUTONLARI EKLEME---*/
dialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//OK BUTONA BASILDIĞINDA TODO'YA EKLE
AddTask(input.getText().toString());
input.setText("");
}
});
dialogBuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
/*---DIALOG BUILDER ARACIYLA DIALOG'A OK VE CANCEL BUTONLARI EKLEME---*/
dialog = dialogBuilder.create(); //DIALOG'U YARAT
todoList = new ArrayList<>();
todoAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,todoList);
completedList = new ArrayList<>();
completedAdapter = new ArrayAdapter<String>(this,R.layout.simplerow,completedList)/*{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);
return view;
}
}*/;
NavigationView navigationView = findViewById(R.id.nav_view); //FIND NAVIGATION VIEW'S ID
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if(menuItem.getTitle().toString().startsWith("Add")){
//AddTask();
dialog.show();
}
else if (menuItem.getTitle().toString().startsWith("To")){
ShowToDoTasks();
}
else if (menuItem.getTitle().toString().startsWith("Completed")){
ShowCompletedTasks();
}
else if (menuItem.getTitle().toString().startsWith("All")){
ShowAllTasks();
}
else if (menuItem.getTitle().toString().startsWith("Close")){
CloseProgram();
}
else
return false;
return true;
}
}
);
}
public void ItemClicked(View v){
txt = (TextView) v;
if(!isCompletedJob) {
CreateConfirmDialog();
}
}
public void AddTask(String msg){
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
todoAdapter.add(msg);
listView.setAdapter(todoAdapter);
isCompletedJob=false;
}
public void ShowCompletedTasks(){
//Toast.makeText(this,"Completed pressed",Toast.LENGTH_LONG).show();
Toast.makeText(this,completedList.size()+"",Toast.LENGTH_LONG).show();
listView.setAdapter(completedAdapter);
isCompletedJob=true;
}
public void ShowAllTasks(){
Toast.makeText(this,"Show All pressed",Toast.LENGTH_LONG).show();
listView.setAdapter(allAdapter);
}
public void ShowToDoTasks(){
Toast.makeText(this,"Show ToDo pressed",Toast.LENGTH_LONG).show();
listView.setAdapter(todoAdapter);
isCompletedJob=false;
}
public void CloseProgram(){
Toast.makeText(this,"Close pressed",Toast.LENGTH_LONG).show();
}
public void UpdateLists(){
txt.setTextColor(Color.RED);
completedAdapter.add(txt.getText().toString());
todoAdapter.remove(txt.getText().toString());
Toast.makeText(MainActivity.this, txt.getText().toString() + " : Done",Toast.LENGTH_LONG).show();
}
public void CreateConfirmDialog(){
/*--- CONFIRMATION IF THE CLICKED JOB COMPLETED OR NOT ---*/
confirmBuilder = new AlertDialog.Builder(this);
confirmBuilder.setTitle("Task Completed? ");
confirmBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UpdateLists();
}
});
confirmBuilder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
confirmDialog = confirmBuilder.create();
confirmDialog.show();
}
}
例外:
E / Android运行时:致命异常:主要 流程:com.example.user.todoapp,PID:4825 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TextView.setTextColor(int)” 在com.example.user.todoapp.MainActivity $ 3.getView(MainActivity.java:83) 在android.widget.AbsListView.obtainView(AbsListView.java:2366) 在android.widget.ListView.makeAndAddView(ListView.java:2052) 在android.widget.ListView.fillDown(ListView.java:786) 在android.widget.ListView.fillFromTop(ListView.java:847) 在android.widget.ListView.layoutChildren(ListView.java:1826) 在android.widget.AbsListView.onLayout(AbsListView.java:2165) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1565) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1565) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在com.android.internal.policy.DecorView.onLayout(DecorView.java:753) 在android.view.View.layout(View.java:20672) 在android.view.ViewGroup.layout(ViewGroup.java:6194) 在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7183) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:949) 在android.view.Choreographer.doCallbacks(Choreographer.java:761) 在android.view.Choreographer.doFrame(Choreographer.java:696) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:935) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)