Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1273 from Arlene238/master
Browse files Browse the repository at this point in the history
#5 #613 第5次实验和第5次实验报告
  • Loading branch information
zengsn authored Dec 11, 2020
2 parents 209a7ef + 9ca088b commit 47968b5
Show file tree
Hide file tree
Showing 14 changed files with 461 additions and 35 deletions.
19 changes: 19 additions & 0 deletions students/sec1814080911238/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.hzuapps.androidlabs.sec1814080911238;

public class Fruit {
private String name;
private String imageId;

public Fruit(String name, String imageId) {
this.name = name;
this.imageId = imageId;
}

public String getName() {
return name;
}

public String getImageId() {
return imageId;
}
}
78 changes: 78 additions & 0 deletions students/sec1814080911238/FruitAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package edu.hzuapps.androidlabs.sec1814080911238;

import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;

static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
TextView fruitImage;
TextView fruitName;
Button deleteBtn;

public ViewHolder(View view) {
super(view);
fruitView = view;
fruitImage = view.findViewById(R.id.textView1);
fruitName = view.findViewById(R.id.textView2);
deleteBtn = view.findViewById(R.id.button);
}
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
final ViewHolder holder = new ViewHolder(view);

holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取SharePreferences(参数1:文件名,参数2:模式)
// SharedPreferences sp = getSharedPreferences ("localData",MODE_PRIVATE);
//获取Editor对象
// SharedPreferences.Editor editor = sp.edit ();
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);

Toast.makeText(view.getContext(), fruit.getName(), Toast.LENGTH_SHORT).show();

}
});
return holder;

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setText(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}

@Override
public int getItemCount() {
return mFruitList.size();
}

public FruitAdapter(List<Fruit> fruitList) {
mFruitList = fruitList;
}

}
42 changes: 38 additions & 4 deletions students/sec1814080911238/InputActivity.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
package edu.hzuapps.androidlabs.sec1814080911238;

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

import androidx.appcompat.app.AppCompatActivity;

public class InputActivity extends AppCompatActivity {
private EditText str01,str02;
int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input_activity);

final Activity thisActivity = this;
final InputActivity thisActivity = this;

Button btnOpen3 = (Button) findViewById(R.id.button3);
str01 = findViewById (R.id.editText1);
str02 = findViewById (R.id.editText2);
btnOpen3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(InputActivity.this, "保存成功", Toast.LENGTH_LONG).show();
//获取两个输入框的内容
String string01 = str01.getText ().toString ();
String string02 = str02.getText ().toString ();
//将信息存储到SharePreferences
if(string01.equals ("") || string02.equals ("")){ //input框验证
Toast.makeText (InputActivity.this,"单纯中英文不能为空",Toast.LENGTH_LONG).show ();
}else { //入库处理
//获取SharePreferences(参数1:文件名,参数2:模式)
SharedPreferences sharedPreferences = getSharedPreferences ("localData",MODE_PRIVATE);
//获取Editor对象
SharedPreferences.Editor editor = sharedPreferences.edit ();
//找到localData中最后一条,在最后一条数据后面进行num编号添加
boolean flag = true;
while (flag) {
if(sharedPreferences.contains("English" + num)) {
num ++;
} else {
flag =false;
}
}
//存储信息
editor.putString ("English" + num,string01);
editor.putString ("China" + num,string02);
//提交
editor.commit ();
num++;
str01.setText("");
str02.setText("");
Toast.makeText (InputActivity.this,"保存成功",Toast.LENGTH_SHORT).show ();
}
}
});
}
Expand Down
33 changes: 33 additions & 0 deletions students/sec1814080911238/RememberActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package edu.hzuapps.androidlabs.sec1814080911238;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

public class RememberActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember_activity);

initFruits();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager= new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter = new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
SharedPreferences sp = getSharedPreferences ("localData",MODE_PRIVATE);

boolean flag = true;
int num = 0;
while (flag) {
if(sp.contains("English" + num)) { //判断localData中是否存在该数据,如何如果存在则通过Fruit渲染成列表
System.out.println(sp.getString("English" + num,""));
System.out.println( sp.getString("China" + num,""));
Fruit apple = new Fruit(sp.getString("English" + num,""), sp.getString("China" + num,"")); // 创建 Fruit 对象
fruitList.add(apple); // 将 apple 传入 fruitList 渲染类中
num ++; //num++渲染下一个单词
} else {
flag =false;
}
}
}

}
7 changes: 4 additions & 3 deletions students/sec1814080911238/input_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
android:layout_height="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="单词英文"
android:text="单词"
android:textSize="18sp"/>

<EditText
android:id="@+id/editText"
android:id="@+id/editText1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
Expand All @@ -40,9 +40,10 @@
android:layout_height="20sp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="中文翻译" />
android:text="翻译" />

<EditText
android:id="@+id/editText2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
Expand Down
Binary file added students/sec1814080911238/lab5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/sec1814080911238/lab5-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/sec1814080911238/lab5-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/sec1814080911238/lab5-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/sec1814080911238/lab6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/sec1814080911238/lab6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions students/sec1814080911238/list_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="50dp" android:layout_marginBottom="10dp"
>

<LinearLayout
Expand All @@ -12,20 +12,19 @@
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_height="50dp"
android:text="卤蛋"
android:gravity="center"
android:textSize="20sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:text="Marinated Egg"
android:textSize="16sp" />
</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -38,7 +37,8 @@
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@color/btnColor"
android:text="记住了" />
android:text="查看翻译" />
</LinearLayout>


</LinearLayout>
24 changes: 4 additions & 20 deletions students/sec1814080911238/remember_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,10 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />
<include layout="@layout/list_item" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

Expand Down
Loading

0 comments on commit 47968b5

Please sign in to comment.