今天没有做搜索功能的内容,学习了学习fragment,想做底部导航栏。
遇到的问题:界面画好了但是不知道怎么和以前现在方法连接起来,而且我发现好像和fragment相连的layout文件中写了ListView之后,运行程序不成功。
ShowFragment.java:
import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.util.ArrayList; import java.util.List; public class ShowFragment extends Fragment { final String TAG = "tag"; /*private Context context = this;*/ private NoteDatabase dbHelper; FloatingActionButton btn_add; private ListView lv_note; private NoteAdapter adapter; private List<Note> noteList = new ArrayList<Note>(); private Toolbar myToolbar; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = View.inflate(getActivity(),R.layout.fragment_show,null); /*btn_add = view.findViewById(R.id.btn_add); btn_add.setonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,Editactivity.class); intent.putExtra("mode",4); //新建笔记的mode设为4 startActivityForResult(intent,0); } });*/ lv_note = view.findViewById(R.id.lv_note); //myToolbar = view.findViewById(R.id.Toolbar_main); /*setSupportActionBar(myToolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setdisplayHomeAsUpEnabled(true); */ //用自定义的工具栏代替本身的活动栏 //初始化adapter adapter = new NoteAdapter(getActivity().getApplicationContext(),noteList); lv_note.setAdapter(adapter); lv_note.setonItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (parent.getId()) { case R.id.lv_note: Note curNote = (Note) parent.getItemAtPosition(position); Intent intent = new Intent(getActivity(), Editactivity.class); intent.putExtra("content", curNote.getContent()); intent.putExtra("id", curNote.getId()); intent.putExtra("time", curNote.getTime()); intent.putExtra("mode", 3); // MODE of 'click to edit' 修改笔记的mode设为3 intent.putExtra("tag", curNote.getTag()); startActivityForResult(intent, 1); //collect data from edit Log.d(TAG, "onItemClick: " + position); break; } } }); refreshListView(); return view; } @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { int returnMode; //-1不改变 0创建一个新笔记 1修改当前笔记 long note_id; returnMode = data.getExtras().getInt("mode",-1); note_id = data.getExtras().getLong("id",0); //修改笔记 if(returnMode == 1){ String content = data.getStringExtra("content"); String time = data.getStringExtra("time"); int tag = data.getExtras().getInt("tag",1); Note newNote = new Note(content,time,tag); newNote.setId(note_id); dao op = new dao(getActivity().getApplicationContext()); op.open(); op.updateNote(newNote); op.close(); //refreshListView(); } //新建笔记 else if (returnMode == 0) { String content = data.getExtras().getString("content"); String time = data.getExtras().getString("time"); int tag = data.getExtras().getInt("tag", 1); Note newNote = new Note(content, time, tag); dao op = new dao(getActivity().getApplicationContext()); op.open(); op.addNote(newNote); op.close(); } //删除笔记 else if (returnMode == 2){ Note delNote = new Note(); delNote.setId(note_id); dao op = new dao(getActivity().getApplicationContext()); op.open(); op.removeNote(delNote); op.close(); } refreshListView(); super.onActivityResult(requestCode, resultCode, data); } //刷新页面,有问题 运行程序 有这段代码会闪退 /*public void refreshListView(){ dao op = new dao(getApplicationContext()); if(noteList.size() > 0){ noteList.clear(); } noteList.addAll(op.getAllNotes()); op.close(); adapter.notifyDataSetChanged(); }*/ //导航栏上的图标 /*@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu,menu); return super.onCreateOptionsMenu(menu); }*/ //刷新listview public void refreshListView() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); dao op = new dao(getActivity().getApplicationContext()); op.open(); // set adapter if (noteList.size() > 0) noteList.clear(); noteList.addAll(op.getAllNotes()); /*if (sharedPreferences.getBoolean("reverseSort", false)) sortNotes(noteList, 2); else sortNotes(noteList, 1);*/ op.close(); adapter.notifyDataSetChanged(); } }