今天是冲刺的第八天,主要实现了用户跳转笔记详细信息界面和收藏。
遇到的困难:intent传递数据问题出错,收藏存在重复的行为。
解决思路:方法调用出错,读取整型采用读取字符串的方法进行读取出错,收藏前先对数据库进行读取,如果用户已经收藏过,提示用户信息并拒绝插入数据。
明天计划完成的任务:实现用户收藏列表界面
附上编写代码:
package com.itheima.cloudnotes.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.StrictMode; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.itheima.cloudnotes.R; import com.itheima.cloudnotes.db.DBService; import com.itheima.cloudnotes.enity.Note; public class Detail extends AppCompatActivity { private ImageView icon; private TextView writer_name; private TextView date; private ImageView collect; private TextView title; private TextView content; private Handler handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { switch(msg.what){ case 0x11: Toast.makeText(Detail.this, "收藏成功", Toast.LENGTH_SHORT).show( ); break; case 0x12: Toast.makeText(Detail.this, "收藏失败", Toast.LENGTH_SHORT).show( ); break; default: Toast.makeText(Detail.this, "收藏失败", Toast.LENGTH_SHORT).show( ); break; } } }; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); icon=findViewById(R.id.icon); writer_name=findViewById(R.id.writer_name); date=findViewById(R.id.note_date); collect=findViewById(R.id.collect); title=findViewById(R.id.title); content=findViewById(R.id.content); Intent intent=getIntent(); int note_id=intent.getIntExtra("note_id",0); DBService dbService=DBService.getDbService(); Note note=dbService.getDetailNote(note_id); icon.setImageBitmap(note.getIcon()); writer_name.setText(note.getWriter_name()); date.setText(note.getDate()); title.setText(note.getTitle()); content.setText(note.getContent()); collectNote(note_id); } public void collectNote(final int note_id) { SharedPreferences pref= getSharedPreferences("user_id", MODE_PRIVATE); final int userId = pref.getInt("userId",0); final int[] falg = {-1}; collect.setOnClickListener(new View.OnClickListener( ) { @Override public void onClick(View v) { new Thread(new Runnable( ) { public void run() { Message message=new Message(); if (falg[0] == -1) { DBService dbService = DBService.getDbService( ); int result = dbService.insertCollect(userId, note_id); if (result == 1) { falg[0] = 1; message.what=0x11; } else { message.what=0x12; } } handler.sendMessage(message); } }).start(); } }); } }