您好我正在尝试从API获取json的id,所以如果我点击一个用户,它将从点击用户的json获取id,我该怎么做?
我的应用看起来像这样:
JSON:
[
{
"id": 6,
"name": "Tester 1",
"online": "0",
"profileimg": "https://s-media-cache-ak0.pinimg.com/736x/af/9c/6a/af9c6a69e7556ffcae42de85735f5a97.jpg",
"distancekm": 16286,
"distancem": 10119
},
{
"id": 15,
"name": "Tester 2",
"online": "0",
"profileimg": "http://assets1.ignimgs.com/2014/10/07/ironmanjpg-affbef_1280w.jpg",
"distancekm": 6216,
"distancem": 3863
},
{
"id": 22,
"name": "Tester 3",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
},
{
"id": 23,
"name": "Tester 4",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
},
{
"id": 24,
"name": "Tester 5",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
},
{
"id": 25,
"name": "Tester 6",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
},
{
"id": 26,
"name": "Tester 7",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
},
{
"id": 27,
"name": "Tester 8",
"online": "0",
"profileimg": "",
"distancekm": 1,
"distancem": 1
}
]
NearbyUsersAdapter:
public class NearbyUsersAdapter extends RecyclerView.Adapter<NearbyUsersAdapter.NearbyUsersViewHolder> {
private List<NearbyUsers> nearbyusers;
private int rowLayout;
private Context context;
private SQLiteHandler db;
public static class NearbyUsersViewHolder extends RecyclerView.ViewHolder {
LinearLayout NearbyUserLayout;
ImageView NearbyUserImage;
TextView NearbyUserUserName;
TextView NearbyUserDistance;
ImageView NearbyUserOnline;
public NearbyUsersViewHolder(View v) {
super(v);
NearbyUserLayout = (LinearLayout) v.findViewById(R.id.nearbyusers_layout);
NearbyUserImage = (ImageView) v.findViewById(R.id.thumbnail);
NearbyUserUserName = (TextView) v.findViewById(R.id.title);
NearbyUserDistance = (TextView) v.findViewById(R.id.distance);
NearbyUserOnline = (ImageView) v.findViewById(R.id.isonline);
}
}
public NearbyUsersAdapter(List<NearbyUsers> nearbyusers, int rowLayout, Context context) {
this.nearbyusers = nearbyusers;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public NearbyUsersAdapter.NearbyUsersViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new NearbyUsersViewHolder(view);
}
@Override
public void onBindViewHolder(NearbyUsersViewHolder holder, final int position) {
db = new SQLiteHandler(context);
Integer uid = nearbyusers.get(position).getId();
String thumbimage = nearbyusers.get(position).getProfileimg();
Integer useronline = nearbyusers.get(position).getOnline();
if (thumbimage.isEmpty()) {
Picasso.with(context).load(R.drawable.ic_no_user)
.resize(80, 80)
.into(holder.NearbyUserImage);
} else {
Picasso.with(context).load(thumbimage)
.resize(80, 80)
.into(holder.NearbyUserImage);
}
holder.NearbyUserUserName.setText(nearbyusers.get(position).getName());
HashMap<String, String> user = db.getUserDetails();
String measure = user.get("unit_of_measure");
if (measure.equals("Metric")) {
holder.NearbyUserDistance.setText(String.valueOf(nearbyusers.get(position).getDistancekm()) + " Km");
}
if (measure.equals("US Standard")) {
holder.NearbyUserDistance.setText(String.valueOf(nearbyusers.get(position).getDistancekm()) + " Mi");
}
if (useronline.equals(1)) {
Picasso.with(context).load(R.drawable.onlineindicator)
.into(holder.NearbyUserOnline);
}
if (useronline.equals(0)) {
Picasso.with(context).load(R.drawable.offlineindicator)
.into(holder.NearbyUserOnline);
}
}
@Override
public int getItemCount() {
return nearbyusers.size();
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private NearbyUsersAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final NearbyUsersAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
我的NearByUsersFragment:
public class NearByUsersFragment extends Fragment {
private boolean mSearchCheck;
private static final String TEXT_FRAGMENT = "TEXT_FRAGMENT";
private SQLiteHandler db;
private ProgressDialog pDialog;
private SwipeRefreshLayout swipeContainer;
private AdView mAdView;
public static NearByUsersFragment newInstance(String text){
NearByUsersFragment mFragment = new NearByUsersFragment();
Bundle mBundle = new Bundle();
mBundle.putString(TEXT_FRAGMENT, text);
mFragment.setArguments(mBundle);
return mFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
final View rootView = inflater.inflate(R.layout.fragment_nearbyusers, container, false);
mAdView = (AdView) rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.nearbyusers_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
db = new SQLiteHandler(getContext());
// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();
final String apiKey = user.get("apiKey");
final ApiInterface apiService =
RestClient.getClient().create(ApiInterface.class);
pDialog = new ProgressDialog(getContext());
pDialog.setMessage(getString(R.string.loadingusers));
pDialog.show();
Call<List<NearbyUsers>> call = apiService.NearbyUsers(apiKey);
call.enqueue(new Callback<List<NearbyUsers>>() {
@Override
public void onResponse(Call<List<NearbyUsers>> call, Response<List<NearbyUsers>> response) {
if (response.code() == 401) {
pDialog.hide();
} else {
int statusCode = response.code();
List<NearbyUsers> nearbysers = response.body();
recyclerView.setAdapter(new NearbyUsersAdapter(nearbysers, R.layout.list_item_nearbyusers, getContext()));
}
pDialog.hide();
}
@Override
public void onFailure(Call<List<NearbyUsers>> call, Throwable t) {
Log.e("NearbyUserFragment", t.toString());
pDialog.hide();
}
});
recyclerView.addOnItemTouchListener(new NearbyUsersAdapter.RecyclerTouchListener(getContext(), recyclerView, new NearbyUsersAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putInt("position", position);
Toast.makeText(getActivity(), "ID:" + String.valueOf(position), Toast.LENGTH_LONG).show();
}
@Override
public void onLongClick(View view, int position) {
}
}));
swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.nearbyuserswipeRefreshLayout);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
pDialog = new ProgressDialog(getContext());
pDialog.setMessage(getString(R.string.loadingusers));
pDialog.show();
Call<List<NearbyUsers>> call = apiService.NearbyUsers(apiKey);
call.enqueue(new Callback<List<NearbyUsers>>() {
@Override
public void onResponse(Call<List<NearbyUsers>> call, Response<List<NearbyUsers>> response) {
if (response.code() == 401) {
pDialog.hide();
} else {
List<NearbyUsers> nearbysers = response.body();
recyclerView.setAdapter(new NearbyUsersAdapter(nearbysers, R.layout.list_item_nearbyusers, getContext()));
}
pDialog.hide();
swipeContainer.setRefreshing(false);
}
@Override
public void onFailure(Call<List<NearbyUsers>> call, Throwable t) {
Log.e("NearbyUserFragment", t.toString());
pDialog.hide();
swipeContainer.setRefreshing(false);
}
});
}
});
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
//Select search item
final MenuItem menuItem = menu.findItem(R.id.menu_search);
menuItem.setVisible(true);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint(this.getString(R.string.search));
((EditText) searchView.findViewById(R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.nliveo_white));
searchView.setOnQueryTextListener(onQuerySearchView);
mSearchCheck = false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_search:
mSearchCheck = true;
Toast.makeText(getActivity(), R.string.search, Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private SearchView.OnQueryTextListener onQuerySearchView = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (mSearchCheck){
// implement your search here
}
return false;
}
};
}
答案 0 :(得分:1)
我假设您将项目映射到名为NearbyUsers的Java对象。
将已有nearbyusers
对象的getter放入适配器中。像这样:
public List<NearbyUsers> getNearbyUsers(){
return nearbyUsers;
}
在OnClick
的{{1}}方法中,您可以使用:
OnItemTouchListener