如何订购ListView并设置背景?

时间:2019-05-26 13:22:21

标签: java android android-listview

首先,如何将最后一个项目放在列表视图顶部的列表视图中?也许每个音符都优先。示例队列号。当我创建笔记时,我想为笔记的背景颜色选择每个笔记的颜色。每个笔记都有自己的背景色值。

主要活动

optimization: {
        runtimeChunk: 'multiple',
        splitChunks: {
            chunks: 'all',
            maxInitialRequests: Infinity,
            minSize: 0,
            cacheGroups: {
                vendor: {
                    test: /[\\/]node_modules[\\/]/,
                    name(module) {
                        // get the name. E.g. node_modules/packageName/not/this/part.js
                        // or node_modules/packageName
                        // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
                        const packageParts = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/);
                        let packageName = 'core';
                        if (packageParts){
                            packageName = packageParts[1];
                        }

                        // npm package names are URL-safe, but some servers don't like @ symbols
                        return `npm.${packageName.replace('@', '')}`;
                    },
                },
            },
        },
    },

笔记班级

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mlistViewNotes = findViewById(R.id.main_listview_notes);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_main_new_note:
            //start noteaactivity
            Intent newNoteActivity =new Intent(this,NoteActivity.class);
            startActivity(newNoteActivity);
            break;
    }
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    mlistViewNotes.setAdapter(null);
    ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
    if(notes == null || notes.size() == 0){
        Toast.makeText(this, "You have no saved notes",Toast.LENGTH_SHORT).show();
        return;
    }else{
        NoteAdapter na= new NoteAdapter(this, R.layout.item_note,notes);
        mlistViewNotes.setAdapter(na);

        mlistViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String fileName =((Note)mlistViewNotes.getItemAtPosition(position)).getmDateTime() + Utilities.FILE_EXTENSION;


                Intent viewNoteIntent= new Intent(getApplicationContext(), NoteActivity.class);
                viewNoteIntent.putExtra("NOTE_FILE",fileName);
                startActivity(viewNoteIntent);
            }
        });
    }


}

NoteActivity

public Note(long mDateTime, String mTitle, String mContent) {
    this.mDateTime = mDateTime;
    this.mTitle = mTitle;
    this.mContent = mContent;
}

public void setmDateTime(long mDateTime) {
    this.mDateTime = mDateTime;
}

public void setmTitle(String mTitle) {
    this.mTitle = mTitle;
}

public void setmContent(String mContent) {
    this.mContent = mContent;
}

public long getmDateTime() {
    return mDateTime;
}

public String getmTitle() {
    return mTitle;
}

public String getmContent() {
    return mContent;
}

public String getDateTimeFormatted(Context context){
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",context.getResources().getConfiguration().locale);
    sdf.setTimeZone(TimeZone.getDefault());
    return  sdf.format(new Date(mDateTime));
}

NoteAdapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note);
    mEtTitle = findViewById(R.id.note_et_title);
    mEtContent = findViewById(R.id.note_et_content);
    mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
    if(mNoteFileName != null && !mNoteFileName.isEmpty()){
        mLoadedNote = Utilities.getNoteByName(this,mNoteFileName);

        if(mLoadedNote != null){
            mEtTitle.setText(mLoadedNote.getmTitle());
            mEtContent.setText(mLoadedNote.getmContent());
        }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_note_new, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_note_save:
            saveNote();
        case R.id.action_note_delete:
            deleteNote();
        break;
    }

    return true;
}



private void saveNote(){
    Note note;
    if(mEtTitle.getText().toString().trim().isEmpty() || mEtContent.getText().toString().trim().isEmpty()){
        Toast.makeText(this," Please enter a title and a content",Toast.LENGTH_SHORT).show();
        return;
    }
    if(mLoadedNote == null){
         note =new Note(System.currentTimeMillis(), mEtTitle.getText().toString(),mEtContent.getText().toString());
    }else{
         note =new Note(mLoadedNote.getmDateTime(), mEtTitle.getText().toString(),mEtContent.getText().toString());
    }

    if(Utilities.saveNote(this,note)){
        Toast.makeText(this,"Your note is saved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(this,"Can not save the note.", Toast.LENGTH_SHORT).show();
    }
    finish();

}
private void deleteNote() {
    if(mLoadedNote == null){
        finish();
    }else{
        AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Delete").setMessage("You are about to delete " + mEtTitle.getText().toString() + ", are you sure?").setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Utilities.deleteNote(getApplicationContext(),mLoadedNote.getmDateTime() + Utilities.FILE_EXTENSION);
                Toast.makeText(getApplicationContext(),mEtTitle.getText().toString() + " is deleted",Toast.LENGTH_SHORT).show();
                finish();
            }
        })
                .setNegativeButton("no",null).setCancelable(false);
        dialog.show();




    }

}

实用程序

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //return super.getView(position, convertView, parent);
    if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_note,null);

    }
    Note note = getItem(position);
    if(note !=null){
        TextView title = convertView.findViewById(R.id.list_note_title);
        TextView date = convertView.findViewById(R.id.list_note_date);
        TextView content = convertView.findViewById(R.id.list_note_content);
        title.setText(note.getmTitle());
        date.setText(note.getDateTimeFormatted(getContext()));
        if(note.getmContent().length() > 50 ){
            content.setText(note.getmContent().substring(0,50));

        }else{
            content.setText(note.getmContent());
        }

    }
    return convertView;

}

0 个答案:

没有答案