TodoList应用程式android

时间:2018-11-05 06:59:53

标签: java android

请帮忙。我正在尝试从活动中获取日期,然后将其放入数组列表中,然后在ListView中进行打印。

问题是我应该从“添加待办事项”中获取的数据没有显示在ListView中。它确实将我带到了“列表活动”,但没有显示数据。

这就是应用程序将“添加待办事项”的方式

enter image description here

这是ListView,数据应该放在其中

enter image description here


我的代码:-

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.list_view);
    arrayList = new ArrayList<>();

    todoAdapter = new  TodoAdapter(this , arrayList);
   listView.setAdapter(todoAdapter);
}

public void onClick(View view) {
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, AddTodo.class);
    startActivityForResult(intent, Intent_Constants.INTENT_REQUEST_CODE);
}

protected void  onActivityResult(int reqCode ,int resultCode , Intent data ){
    // here Iam trying to get the data from ADDING TO DO class
    if(resultCode == Intent_Constants.INTENT_REQUEST_CODE){

        titleText = data.getStringExtra(Intent_Constants.INTENT_TITLE);
        priorityText = data.getStringExtra(Intent_Constants.INTENT_PRIORITY);
        statusText = data.getStringExtra(Intent_Constants.INTENT_STATUES);
        dateText = data.getStringExtra(Intent_Constants.INTENT_DATE);
        timeText = data.getStringExtra(Intent_Constants.INTENT_TIME);

        todoAdapter.todo.add(new Todo (titleText , statusText ,priorityText ,dateText ,timeText));


    }
}

Intent_Constants

public class Intent_Constants {
    public final static  int INTENT_REQUEST_CODE = 1;
    public final static int INTENT_RESULT_CODE = 1 ;
    public final static String INTENT_TITLE = "Title";
    public final static String INTENT_PRIORITY = "Priority";
    public final static String INTENT_TIME = "Time";
    public final static String INTENT_DATE = "Date";
    public final static String INTENT_STATUES = "Statues";
}

AddTodo类

在此类中,找到ID,然后将其转换为String

public void saveButton (View view){
    Intent intent = new Intent();
    intent.putExtra(Intent_Constants.INTENT_TITLE , title);
    intent.putExtra(Intent_Constants.INTENT_DATE , date);
    intent.putExtra(Intent_Constants.INTENT_TIME , time);
    intent.putExtra(Intent_Constants.INTENT_PRIORITY , priority);
    intent.putExtra(Intent_Constants.INTENT_STATUES , completed);

    setResult(INTENT_RESULT_CODE, intent);

    finish();
 }

TodoAdapter类

ArrayList<Todo> todo ;

public TodoAdapter(@NonNull Context context, ArrayList<Todo> todo) {
   super(context, R.layout.todo_list,todo);
    this.todo = todo;
}

public static class ViewHolder {
    TextView titleText;
    TextView priorityText;
    TextView dateText;
    TextView timeText;

    CheckBox statusBox;

    ImageButton edit_image;
    ImageButton open_image;
    ImageButton delet_image;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater inflater =  LayoutInflater.from(getContext()) ;

    View customeView =inflater.inflate(R.layout.todo_list,parent ,false);

    holder.titleText.setText(getItem(position).getTitle());
    holder.priorityText.setText(getItem(position).getPriority());
    holder.dateText.setText(getItem(position).getDate());
    holder.timeText.setText(getItem(position).getTime());

    holder.statusBox.setChecked(false);

    holder.edit_image = customeView.findViewById(R.id.edit_imageView);
    holder.open_image = customeView.findViewById(R.id.open_imageButton);
    holder.delet_image = customeView.findViewById(R.id.delete_imageView);

    holder.edit_image.setImageResource(R.drawable.ic_edit_black_24dp);
    holder.open_image.setImageResource(R.drawable.ic_refresh_black_24dp);
    holder.delet_image.setImageResource(R.drawable.ic_delete_black_24dp);

    return customeView;
}

Todo类

public class Todo {
    private String title ;
    private String status ;
    private String priority ;
    private String date ;
    private String time ;

    public Todo() {
    }

    public Todo(String title, String status, String priority, String date, String time) {
        this.title = title;
        this.status = status;
        this.priority = priority;
        this.date = date;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public String getStatus() {
        return status;
    }

    public String getPriority() {
        return priority;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以设置一个新的适配器,我知道这不是最好的方法,但是应该可以完成工作。不要创建新的,而要创建新数据并对其进行初始化。

答案 1 :(得分:0)

@Noura更改此

arrayList.add(new Todo(titleText , statusText ,priorityText ,dateText ,timeText));

todoAdapter.todo.add(new Todo(titleText , statusText ,priorityText ,dateText ,timeText));
todoAdapter.notifyDataSetChanged();

在构造函数中,您需要像下面那样更新代码

ArrayList<Todo> todo ;

public TodoAdapter(@NonNull Context context, ArrayList<Todo> todo) {
this.todo = todo
super(context, R.layout.todo_list,todo);
}