我正在安装Android应用。
我正在使用改造从URL(http://kerast.com/pokemon/pokemons.json)获取包含小宠物名称的JSON数据。名称和类型显示在recyclerView中,并且在单击彼此之后,Activity将打开并显示攻击,防御和移动。
到目前为止,我已经能够打开一个新活动,但不知道如何传递该对象。我想没有必要通过为每个活动重复写两次相同的代码来重新加载json?
这是我的班级:
public class Pokemon {
private String name;
private String type;
private Integer attack;
private Integer defense;
private Integer evolveLevel;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public Integer getattack() { return attack; }
public Integer getdefense() { return defense; }
public Integer getEvolveLevel() { return evolveLevel; }
}
我的DataAdpater:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable {
private ArrayList<Pokemon> mArrayList;
private ArrayList<Pokemon> mFilteredList;
public DataAdapter(ArrayList<Pokemon> arrayList) {
mArrayList = arrayList;
mFilteredList = arrayList;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(mFilteredList.get(i).getName());
viewHolder.tv_version.setText(mFilteredList.get(i).getName());
viewHolder.tv_api_level.setText(mFilteredList.get(i).getType());
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = mArrayList;
} else {
ArrayList<Pokemon> filteredList = new ArrayList<>();
for (Pokemon marraylist : mArrayList) {
if (marraylist.getName().toLowerCase().contains(charString) || marraylist.getType().toLowerCase().contains(charString)) {
filteredList.add(marraylist);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<Pokemon>) filterResults.values;
notifyDataSetChanged();
}
};
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Main2Activity.class);
intent.putExtra("attack", Pokemon);
view.getContext().startActivity(intent);
}
});
}
}
}
最后我的MainActivity:
public class MainActivity extends AppCompatActivity {
public static final String BASE_URL = "http://kerast.com";
private RecyclerView mRecyclerView;
private ArrayList<Pokemon> mArrayList;
private DataAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initViews();
loadJSON();
}
private void initViews(){
mRecyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
mArrayList = new ArrayList<>(Arrays.asList(jsonResponse.getPokemons()));
mAdapter = new DataAdapter(mArrayList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search);
search(searchView);
return true;
}
private void search(SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return true;
}
});
}
}
那么如何才能在第二个Activity中显示Attack Defense和evolveLevel?
答案 0 :(得分:0)
实现Serializable接口Pokemon实现Serializable 和 然后你可以使用putExtra()
在intent extra中传递对象实例
//send :
intent.putExtra("Pokemon", obj);
//retrieve :
getIntent().getSerializableExtra("MyClass");
答案 1 :(得分:0)
akshay的答案是正确的,但我认为这将是更好的方法。我们可以使用 Parcelable 类代替可序列化。
请参阅difference b/w parcelable and serializale
的以下链接并创建一个可转换的POJO类我使用online creator。
休息是一样的
//Sending data
intent.putExtra("String name", object);
//Receving end
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Pokemon pokemon = (Pokemon) bundle.getParcelable("String name")
}