0

I'm using RecyclerView activity, Adaper and json. when the data is parsed and load into RecyclerView by json and then fill data into Adapter. it can load data and display on view. But I can not clicked on item and it seem is not working. I wish then click item, it will transition to another activity.

I have defined method: setOnClickListener() in Activity class, not Adapter class

My class activity:

public class CategoryCarActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener {

RecyclerViewCategoryAdapter recyclerViewAdapter;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewLayoutManager;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> carsList;
static final String URL_CATEGORY = "myURL";
// ALL JSON node names
static String TAG_NAME = "name";
static final String TAG_MANUFACTURER = "name";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler_category_adapter);      // activity_sgv

    // Hashmap for ListView
    carsList = new ArrayList<HashMap<String, String>>();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerViewLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(recyclerViewLayoutManager);

    new LoadCategoryCars().execute();

    recyclerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);
            // both car id and price is needed
            String car_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
            String price = ((TextView) view.findViewById(R.id.price)).getText().toString();
            Toast.makeText(getApplicationContext(), "Car Id: " + car_id  + ", Price: " + price, Toast.LENGTH_SHORT).show();
            i.putExtra("car_id", car_id);
            i.putExtra("price", price);
            startActivity(i);
        }
    });
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
/**
 * Background Async Task to Load all tracks under one album
 * */
class LoadCategoryCars extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CategoryCarActivity.this);
        pDialog.setMessage("Loading cars ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    /**
     * getting tracks json and parsing
     * */
    protected String doInBackground(String... args) {
        try {
            JSONObject jObj = new JSONObject(json);
            if (jObj != null) {
                String car_id = jObj.getString(TAG_ID);
                manufacturer_name = jObj.getString(TAG_MANUFACTURER);
                manufacturers = jObj.getJSONArray(TAG_CARS);
                if (manufacturers != null) {
                    // looping through All cars
                    for (int i = 0; i < manufacturers.length(); i++) {
                        JSONObject c = manufacturers.getJSONObject(i);
                        String name = new String(c.getString(TAG_NAME).getBytes("ISO-8859-1"), "UTF-8");
                        String price = c.getString(TAG_PRICE);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_NAME, name);
                        map.put(TAG_PRICE, price);
                        // adding HashList to ArrayList
                        carsList.add(map);
                    }
                } else {
                    Log.d("Manufacturers: ", "null");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        recyclerViewAdapter = new RecyclerViewCategoryAdapter(carsList, CategoryCarActivity.this);
        recyclerViewAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(recyclerViewAdapter);
        // dismiss the dialog after getting all manufacturers
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

}

I used setOnClickListener() method but it's not working.

How to fix this the problem ? thank you so much

Khanh Luong Van
  • 476
  • 3
  • 14
  • 31
  • I know my question same with a lot of another question, but in my case, I define method: setOnClickListener() in activity class, not Adapter class – Khanh Luong Van Jan 18 '17 at 18:45
  • you are setting onClickListener on recyclerView not on its items. maybe this [link](http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif?rq=1) will help you – Omer Jan 18 '17 at 20:02
  • Thank you @Omer: your link is useful for me, maybe I will try again – Khanh Luong Van Jan 18 '17 at 20:08

0 Answers0