如何解析JSON并设置为控件。 ListView或RecyclerView并单击该项可以启动android中的另一个活动?

时间:2017-02-27 07:18:58

标签: android json listview

如何解析JSON并设置为控件。 ListView或RecyclerView并单击该项可以在android中启动另一个活动吗?

http://beta.json-generator.com/api/json/get/VJ9snXcFG

我们使用json(beta.json-generator.com/api/json/get/VJ9snXcFG)在列表视图中打印所有标题。接下来的第一个标题是点击json中的第一个url(beta.json-generator.com / api / json / get / VJ9snXcFG),第二个标题将在json上单击打开第二个url(beta.json-generator.com/api/json/get/VJ9snXcFG)等等

1 个答案:

答案 0 :(得分:0)

解析JSON使用改造。这是一个很好的例子:https://square.github.io/retrofit/ 在RecyclerView中,在onBindViewHolder函数中添加一个onClicklistener,然后在那里开始你的活动。

示例:

Json有这样的数据:

[{"id":302,"latitude":-23.725,"longitude":34.2773,"place":"Panda Mozambik null"},{"id":312,"latitude":46.7681,"longitude":20.8186,"place":"Kondoros Magyarország null"},{"id":322,"latitude":8,"longitude":3,"place":"rr"}]

数据类:

public class Data {
    public int id;
    public String place;
    public float  longitude;
    public float  latitude;

    public Data(){}
}

的NetAPI

public interface NetApi {
   @GET("/")
   Call<List<Data>> getData();
}

在活动中:

private Retrofit retrofit;
private NetApi netApi;
private RecyclerView recyclerView;
ItemAdapter adapter;
public List<Data> items = new ArrayList<Data>();
private static final String ENDPOINT_ADDRESS = "https://endpoint.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_ADDRESS).client(new OkHttpClient.Builder().build()).addConverterFactory(GsonConverterFactory.create()).build();
    netApi = retrofit.create(NetApi.class);
    initRecycleView()
    getData();
}
public void getData() {
    netApi.getData().enqueue(new Callback<List<Data>>() {
        @Override
        public void onResponse(Call<List<Data>> call, Response<List<Data>> response) {
            if (response.isSuccessful()) {
                Log.d(TAG, response.body().toString());
                items = new ArrayList<Data>((response.body()));
                adapter.update(items);
            } else {
                 Toast.makeText(MainActivity.this, "Error: " + response.message(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<List<Data>> call, Throwable t) {

        }
    });
}
private void initRecycleView() {
    recyclerView = (RecyclerView) view.findViewById(R.id.MainRecyclerView);
    adapter = new ItemAdapter();
    recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
    recyclerView.setAdapter(adapter);

}

ItemAdapter是这样的:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

public final List<Data> items;

public ItemAdapter() {
    items = new ArrayList<>();
}


@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView =
            LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_recyclerview, parent, false);
    ItemViewHolder viewHolder = new ItemViewHolder(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    Data item = items.get(position);
    holder.longitude.setText(Float.toString(item.longitude));
    holder.latitude.setText(Float.toString(item.latitude));
    holder.place.setText(item.place);
    holder.place.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_LONG).show();
        }
    });

}

public void addItem(Data item) {
    items.add(item);
    notifyItemInserted(items.size() - 1);

}



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

public void update(List<Data> itemsrec) {
    items.clear();
    items.addAll(itemsrec);
    notifyDataSetChanged();

}

public class ItemViewHolder extends RecyclerView.ViewHolder {

    TextView place;
    TextView longitude;
    TextView latitude;

    public ItemViewHolder(View itemView) {
        super(itemView);
        place = (TextView) itemView.findViewById(R.id.place);
        longitude = (TextView) itemView.findViewById(R.id.longitude);
        latitude = (TextView) itemView.findViewById(R.id.latitude);

    }


}

}