Android安卓开发大作业源码(动态留言页增删改查)

Android安卓开发大作业源码(动态留言页增删改查)_第1张图片

MySQLites.java(创建数据库数据表及方法)

package com.example.qq;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;

class MySQLites extends SQLiteOpenHelper {
    private static final String DB_NAME="stays.db";
    private static final String TABLE_NAME="stay";
    private static final String CREATE_TABLE_SQL_USER="create table stay(id integer primary key autoincrement" +
            ",names text,contents text,del text)";
    public MySQLites(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MySQLites(@Nullable Context context) {
        super(context, DB_NAME, null,1);
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public MySQLites(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, name, version, openParams);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_SQL_USER);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    }


    //查询全部留言
    public List<Userstays> selectallStay(String name){
        SQLiteDatabase db=getReadableDatabase();
        List<Userstays> userstaysList=new ArrayList<>();
        Cursor c=db.query("stay",null,null,null,null,null,null,null);
        if(c!=null){
            while(c.moveToNext()){
                @SuppressLint("Range") String stayName=c.getString(c.getColumnIndex("names"));
                @SuppressLint("Range") String stayContent=c.getString(c.getColumnIndex("contents"));
                Userstays userstays=new Userstays();
                userstays.setNames(stayName);
                userstays.setContents(stayContent);
                userstaysList.add(userstays);
            }
            c.close();
        }
        return userstaysList;
    }
    //添加留言
    public long insertStay(Userstays userstays) {
        SQLiteDatabase db=getReadableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("names",userstays.getNames());
        contentValues.put("contents",userstays.getContents());
        return db.insert(TABLE_NAME,null,contentValues);
    }

    //留言删除
    public int deleteDataByName(String name){
        SQLiteDatabase db=getWritableDatabase();
        return db.delete(TABLE_NAME,"names like ?",new String[]{name});
    }


    //留言修改
    public long updateStay(Userstays userstays){
        SQLiteDatabase db=getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("names",userstays.getNames());
        contentValues.put("contents",userstays.getContents());
        return db.update(TABLE_NAME,contentValues,"names like ?",new String[]{userstays.getEditn()});
    }
}

DynamicsActivity.java

package com.example.qq;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class DynamicsActivity extends AppCompatActivity {
    private BottomNavigationView navigationView;
    private ImageView dyimg_zan,dyimg_zan2;
    private boolean display;
    private LinearLayout stayL,dyL;
    private Button inBtn,adminBtn,editBtn;
    private ImageButton dyEdit;
    private MySQLites mySQLites;
    private TextView stayTv,tvv;
    private ListView dyLv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamics);


        mySQLites=new MySQLites(this);
        DyFragment dyFragment=new DyFragment();
        FragmentManager supportFragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.dy_fl,dyFragment);
        fragmentTransaction.commit();
        display=true;
        initView();


        navigationView.setSelectedItemId(R.id.dynamics);
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.message:
                        startActivity(new Intent(getApplicationContext(),MessageActivity.class));
                        overridePendingTransition(0,0);
                        return true;
                    case R.id.contact:
                        startActivity(new Intent(getApplicationContext(),ContactActivity.class));
                        overridePendingTransition(0,0);
                        return true;
                    case R.id.dynamics:

                        return true;
                }
                return false;
            }
        });

        editBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),EditstayActivity.class));
                overridePendingTransition(0,0);
            }
        });


        inBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),StayActivity.class));
                overridePendingTransition(0,0);

            }
        });


    }


    private void initView() {
        navigationView=findViewById(R.id.navigation_bottom);
        dyimg_zan=findViewById(R.id.img_zan);
        dyimg_zan2=findViewById(R.id.img_zan2);
        stayL=findViewById(R.id.stay_ll);
        dyL=findViewById(R.id.dy_ll);
        inBtn=findViewById(R.id.in_btn);
        dyLv=findViewById(R.id.dy_lv);
//        stayTv=findViewById(R.id.stay_tv_name);
//        dyDelete=findViewById(R.id.dels);
//        dyEdit=findViewById(R.id.edits);
        editBtn=findViewById(R.id.edit_btn);
    }


    //留言
    String name="1";
    public void stayOnClick(View view) {
        if(display==true){
            dyL.setVisibility(View.GONE);
            stayL.setVisibility(View.VISIBLE);
            display=false;
        }else{
            dyL.setVisibility(View.VISIBLE);
            stayL.setVisibility(View.GONE);
            display=true;
        }

        //listview输出数据库内容
            List<Userstays> userstays=mySQLites.selectallStay(name);
            if(userstays!=null){
                List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); //获取键值对
                for(Userstays userspace1:userstays){
                    HashMap<String,Object> item=new HashMap<String,Object>();
                    item.put("names",userspace1.getNames());
                    item.put("contents",userspace1.getContents());
                    data.add(item);
                }
                SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.item_layout_dy,new String[]{"names","contents"},
                        new int[]{R.id.item_names,R.id.item_contents}); //将获取到的数据库内容进行显示
                dyLv.setAdapter(adapter);
                //listview 通过item实现长按删除
                dyLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //提示对话框
                        AlertDialog.Builder builder=new AlertDialog.Builder(DynamicsActivity.this);
                        builder.setIcon(R.drawable.delete);
                        builder.setTitle("警告:");
                        builder.setMessage("确定删除这条留言吗?");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                TextView c = (TextView) view.findViewById(R.id.item_names); //获取所被点击行的名字
                                String name= c.getText().toString(); //对所获取到的名字进行转换
                                int ad= mySQLites.deleteDataByName(name); //调用删除
                                if(ad>0){
                                    ToastUtill.toastshort(DynamicsActivity.this,"删除成功!");
                                    startActivity(new Intent(getApplicationContext(),DynamicsActivity.class));
                                    overridePendingTransition(0,0);
                                }else{
                                    ToastUtill.toastshort(DynamicsActivity.this,"删除失败!");
                                }
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                ToastUtill.toastshort(DynamicsActivity.this,"已取消");
                            }
                        });
                        builder.show();
//                    Toast.makeText(StayadminActivity.this, playerChanged+"", Toast.LENGTH_LONG).show();
                        return false;
                    }
                });

            }


    }


}

StayActivity.java(添加留言)

package com.example.qq;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class StayActivity extends AppCompatActivity {
    private MySQLites mySQLites;
    private Button publishBtn;
    private EditText stayEtName,stayEtContent;
    private LinearLayout stayL,dyL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stay);
        initView();
        mySQLites=new MySQLites(this);
        publishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=stayEtName.getText().toString().trim();
                String content=stayEtContent.getText().toString().trim();
                Userstays userstays=new Userstays();
                userstays.setNames(name);
                userstays.setContents(content);

                long rowId=mySQLites.insertStay(userstays);
                if(rowId!=-1){
                    ToastUtill.toastshort(StayActivity.this,"发表成功");
                    startActivity(new Intent(getApplicationContext(),DynamicsActivity.class));
                }else{
                    ToastUtill.toastshort(StayActivity.this,"哦豁,发表失败了!");

                }
            }
        });
    }

    private void initView() {
        publishBtn=findViewById(R.id.publish_btn);
        stayEtName=findViewById(R.id.stay_ed_name);
        stayEtContent=findViewById(R.id.stay_ed_content);
        stayL=findViewById(R.id.stay_ll);
        dyL=findViewById(R.id.dy_ll);
    }

}

EditstayActivity.java(点击想要修改的留言)

package com.example.qq;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EditstayActivity extends AppCompatActivity {
    private MySQLites mySQLites;

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

        mySQLites=new MySQLites(this);
        ListView stayEdit=findViewById(R.id.stay_edit_lv);


        String name="1";
        List<Userstays> userstays=mySQLites.selectallStay(name);
        if(userstays!=null) {
            List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
            for (Userstays userspace1 : userstays) {
                HashMap<String, Object> item = new HashMap<String, Object>();
                item.put("names", userspace1.getNames());
                item.put("contents", userspace1.getContents());
                item.put("del", userspace1.getEditn());
                data.add(item);
            }
            SimpleAdapter adapter = new SimpleAdapter(this, data,
                    R.layout.item_layout_dyadmine, new String[]{"names", "contents"},
                    new int[]{R.id.item_editnames, R.id.item_editcontents});
            stayEdit.setAdapter(adapter);
            stayEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    TextView tv=(TextView) view.findViewById(R.id.item_editnames);
                    TextView tvs=(TextView) view.findViewById(R.id.item_editcontents);

                    String name=tv.getText().toString();
                    String content=tvs.getText().toString();
                    Intent intents = new Intent(EditstayActivity.this,EditstaysActivity.class);
                    intents.putExtra("name",name.trim() );  //传值
                    intents.putExtra("content",content.trim() );
                    startActivity(intents);


                }
            });
        }
    }


}

EditstaysActivity.java(修改留言页)

package com.example.qq;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditstaysActivity extends AppCompatActivity {
    private EditText editName,editContent;
    private Button Btn;
    private MySQLites mySQLites;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editstays);
        mySQLites=new MySQLites(this);

        initView();
        Intent intent=getIntent();
        editName.setText(intent.getStringExtra("name"));
        editContent.setText(intent.getStringExtra("content"));

        Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ename=editName.getText().toString();
                String econtent=editContent.getText().toString();
                Userstays userstays=new Userstays();
                userstays.setEditn(intent.getStringExtra("name")); //传递最初没有修改的姓名
                userstays.setNames(ename);
                userstays.setContents(econtent);
                long ad=mySQLites.updateStay(userstays);
                if(ad>0){
                    ToastUtill.toastshort(EditstaysActivity.this,"修改成功");
                    startActivity(new Intent(getApplicationContext(),DynamicsActivity.class));
                }else{
                    ToastUtill.toastshort(EditstaysActivity.this,"修改失败啦");
                }
            }
        });
    }

    private void initView() {
        editName=findViewById(R.id.edit_name);
        editContent=findViewById(R.id.edit_content);
        Btn=findViewById(R.id.edit_btns);
    }
}

你可能感兴趣的:(Android,android,android,studio,java)