我有一个显示项目列表的recyclerview
,并且我尝试使用EditText实现浏览器。
我还在学习,而且我不知道在我的代码中实现它的最佳方法,希望你能给我一些建议。
我应该实现一个在我的对象数组中找到名称的函数。每次我更改 editText 并将其添加到 Recyclerview适配器?这是更多的optime解决方案吗?
ListMonsters.java
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.security.AlgorithmParameterGenerator;
import java.util.ArrayList;
import java.util.HashMap;
public class ListMonsters extends AppCompatActivity {
RecyclerView monstersgrid;
Button small;
Button big;
TextInputEditText findmonsterfield;
private static String monsterid;
ArrayList<HashMap<String, String>> MonstersList;
ArrayList<Monster> monstersArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_monsters);
monstersgrid = (RecyclerView) findViewById(R.id.monstersgrid);
small = (Button) findViewById(R.id.small);
big = (Button) findViewById(R.id.big);
findmonsterfield = (TextInputEditText) findViewById(R.id.findmonsterfield);
Context c = getApplicationContext();
HashMap<String, String> hashMap;
MonstersList = new ArrayList<HashMap<String, String>>();
monstersArray = new ArrayList<Monster>();
try {
JSONObject reader = new JSONObject(loadJSONFromAsset());
JSONArray jr = reader.getJSONArray("monsters");
for (int i = 0; i < jr.length(); i++) {
JSONObject jsonObjectLine = jr.getJSONObject(i);
// Desem els items JSON en una variable
String id = jsonObjectLine.getString("id");
String name = jsonObjectLine.getString("name");
String type = jsonObjectLine.getString("type");
String icon = jsonObjectLine.getString("icon");
//Toast.makeText(ListMonsters.this, id, Toast.LENGTH_LONG).show();
// Afegim la clau-valor a un objecte HashMap
hashMap = new HashMap<String, String>();
hashMap.put("id", id);
hashMap.put("name", name);
hashMap.put("type", type);
hashMap.put("icon", icon);
MonstersList.add(hashMap);
Monster m = new Monster();
m.setId(MonstersList.get(i).get("id"));
m.setName(MonstersList.get(i).get("name"));
m.setIcon(MonstersList.get(i).get("icon"));
m.setContext(c);
m.setId(id);
monstersArray.add(m);
MonstersArrays.addMonster(m);
}
GridLayoutManager manager1 = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
monstersgrid.setLayoutManager(manager1);
MyRecyclerViewAdapter adapter1 = new MyRecyclerViewAdapter(this.getApplicationContext(),monstersArray);
monstersgrid.setAdapter(adapter1);
} catch (JSONException e) {
e.printStackTrace();
}
small.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
big.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
}
//Read JSON
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = ListMonsters.this.getAssets().open("bigmonstersESP.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Anonymous on 22/02/2018.
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {
private List<Monster> feedItemList;
private Context mContext;
public MyRecyclerViewAdapter(Context context, List<Monster> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listviewitemmonster, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
Monster feedItem = feedItemList.get(i);
String name = feedItem.getName();
//Render image using Picasso library
Picasso.with(mContext).load(feedItem.getIcon())
.error(R.drawable.unknown)
.placeholder(R.drawable.unknown)
.into(customViewHolder.imageView);
//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(name));
customViewHolder.name = name;
customViewHolder.icon = feedItem.getIcon();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;
protected String name;
protected int icon;
public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.imglayout);
this.textView = (TextView) view.findViewById(R.id.namelayout);
CustomViewHolder.super.itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Toast.makeText(mContext,name, Toast.LENGTH_SHORT).show();
//Toast.makeText(mContext,"hola", Toast.LENGTH_SHORT).show();
}
});
}
}
}