如何使列表标题只在列表菜单中可见,而不是列表项的整个内容在listview android studio?



我正在尝试制作一个简单的笔记应用程序。我试图在列表视图中显示笔记。但是当我向注释中添加更多内容时,它会在列表视图的主菜单中显示整个内容。如何使只有列表的标题可见在列表视图和内容可见只有当我们打开列表项?

MainActitvity.java

package com.example.notes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static   ArrayList<String> notes=new ArrayList<>();
static ArrayAdapter arrayAdapter;
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.add_note_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId()==R.id.add_note){
Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
startActivity(intent);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview=(ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=(HashSet<String>)sharedPreferences.getStringSet("notes",null);
if(set==null) {
notes.add("Example note");
}
else{
notes=new ArrayList(set);
}
notes.add("Note 1");
arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,notes) ;
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
Intent intent=new Intent(getApplicationContext(),NoteEditorActivity.class);
intent.putExtra("noteId",i);
startActivity(intent);
}
});
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
final int itemToDelete=i;
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("are you sure?")
.setMessage("do you want to delete?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
notes.remove(itemToDelete);
arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=new HashSet<>(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes",set).apply();
}
})
.setNegativeButton("no",null)
.show();
return true;
}
});
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/note1"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

note_editor_activity.java

package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.HashSet;
public class NoteEditorActivity extends AppCompatActivity {
int noteId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
EditText editText=(EditText) findViewById(R.id.editText);
Intent intent=getIntent();
noteId=intent.getIntExtra("noteId",-1);
if(noteId!=-1){
editText.setText(MainActivity.notes.get(noteId));
}
else{
MainActivity.notes.add("");
noteId=MainActivity.notes.size()-1;
MainActivity.arrayAdapter.notifyDataSetChanged();
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
MainActivity.notes.set(noteId,String.valueOf(charSequence));
MainActivity.arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences("com.robpercival.notes", Context.MODE_PRIVATE);
HashSet<String> set=new HashSet<>(MainActivity.notes);
sharedPreferences.edit().putStringSet("notes",set).apply();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}

acitivity_node-editor.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/olo"
tools:context=".NoteEditorActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:padding="0dp"
tools:layout_editor_absoluteX="118dp"
tools:layout_editor_absoluteY="91dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

add_note_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add_note" android:title="Add note"></item>
</menu>

对不起,前一个,我没有解释正确使用另一种格式使用两个edittext一个标题和一个身体。将它们保存在不同的首选项中,使用body键作为标题将标题首选项传递给listview。

相关内容