我正在Android中創建應用程序。Android通過單擊recyclerview將jsonObject傳遞給另一個活動
我正在使用翻新功能從URL(http://kerast.com/pokemon/pokemons.json)獲取包含小寵物名稱的JSON數據。名稱和類型顯示在回收站視圖中,並且在彼此點擊後,活動打開並顯示攻擊,防禦和移動。
到目前爲止,我已經能夠打開一個新的活動,但不知道如何傳遞該對象。我想沒有必要重新加載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;
}
});
}
}
所以,我怎麼能得到的只有攻擊防禦和evolveLevel是顯示在第二個活動?
謝謝您的回答! 我不明白「對象」應該做什麼?因爲我得到一個錯誤。我用「口袋妖怪」代替它,它可以工作,但它仍然強調寵物小精靈,並說:「預計表達式」 –
正如yatin提到的那樣,使用parcelable並僅序列化您在下一個活動中需要的屬性。您可以在android studio中使用插件,該插件會詢問您的字段並將爲您打包數據。 – Ritesh
@SamCaneau你應該使用mArrayList而不是對象。所以在你的目標活動中,你可以收到整個口袋妖怪的數組列表。 'ArrayList pokemons =(ArrayList )bundle.get(「String name」);' –
Yatin