This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1273 from Arlene238/master
- Loading branch information
Showing
14 changed files
with
461 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.