单击项

时间:2017-01-18 18:37:22

标签: android android-recyclerview

我正在使用RecyclerView活动,Adaper和json。解析数据并通过json加载到RecyclerView然后将数据填充到Adapter中。它可以加载数据并在视图上显示。但我无法点击项目,似乎无法正常工作。我希望然后点击项目,它将转换到另一个活动。

我在Activity类中定义了方法:setOnClickListener(),而不是Adapter类

我的课堂活动:

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();
    }
}

}

我使用了setOnClickListener()方法,但它无效。

如何解决这个问题?非常感谢你

0 个答案:

没有答案