单击项目时如何获取列表视图中项目的ID?

时间:2017-03-15 06:20:57

标签: android mysql

我正在使用一个项目在列表视图中显示图像,当我们单击列表视图中的任何图像时,然后转到下一个意图。当我点击时,特定项目的ID不会传递给其他意图。我不知道该怎么做。

主要Activity.java

List<GetDataAdapter> GetDataAdapter11;

RecyclerView recyclerView;

RecyclerView.LayoutManager recyclerViewlayoutManager;

RecyclerView.Adapter recyclerViewadapter;

String GET_JSON_DATA_HTTP_URL = "http://192.1622/.22/new.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";

JsonArrayRequest jsonArrayRequest ;

RequestQueue requestQueue ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_tab_activity_1);
    GetDataAdapter11 = new ArrayList<>();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

    recyclerView.setHasFixedSize(true);

    recyclerViewlayoutManager = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(recyclerViewlayoutManager);


    JSON_DATA_WEB_CALL();


}

public void JSON_DATA_WEB_CALL(){

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);
          //  GetDataAdapter2.setImageTitleNamee(String.valueOf(json.getInt(String.valueOf(JSON_id))));

            GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));

            GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter11.add(GetDataAdapter2);
    }

    recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter11, this);

    recyclerView.setAdapter(recyclerViewadapter);


}
}

RecyclerViewAdapter.Java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Bitmap bitmap;
Context context;
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
 int id;


public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {

    super();
    this.getDataAdapter = getDataAdapter;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

    ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter);

    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

    GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

    imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

    imageLoader1.get(getDataAdapter1.getImageServerUrl(),
            ImageLoader.getImageListener(
                    Viewholder.networkImageView,//Server Image
                    R.mipmap.ic_launcher,//Before loading server image the default showing image.
                    android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
            )
    );


    Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
    Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

}

@Override
public int getItemCount() {

    return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView ImageTitleNameView;
   // public TextView id;
    public NetworkImageView networkImageView;

    List<GetDataAdapter> getDataAdapter ;//= new List<GetDataAdapter>();
    Context context;

    public ViewHolder(View itemView,Context context,List<GetDataAdapter> getDataAdapter) {

        super(itemView);
        this.getDataAdapter=getDataAdapter;
        this.context=context;
        itemView.setOnClickListener(this);

        ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
        networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
        id = Integer.parseInt(((TextView) itemView.findViewById(R.id.pic_id)).getText().toString());


    }
    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        GetDataAdapter getDataAdapters=this.getDataAdapter.get(position);
        Intent intent=new Intent(this.context,Secondpage.class);

        intent.putExtra("image_title",getDataAdapters.getImageTitleName());
        Log.e("id","id of clicked image" +ImageTitleNameView);
        intent.putExtra("image_url",getDataAdapters.getImageServerUrl());
        Log.e("id","id of clicked image" +networkImageView);
       intent.putExtra("id",getDataAdapters.getId());
         Log.e("id","id of clicked image" +id);
        this.context.startActivity(intent);

    }
}

3 个答案:

答案 0 :(得分:0)

view.setTag(id); 

在onBindViewHolder和

view.getTag(); 
onClick中的

将解决您的问题

答案 1 :(得分:0)

首先,确保您获得点击项目的位置,然后从ItemList

获取项目

尝试这种方式:

  int pos = (int) v.getTag();
  Item item =ItemList.get(pos);

然后尝试从这个项目中获取id(即item.getId)

答案 2 :(得分:0)

在onBindViewHolder中添加ClickListener并从ViewHolder中删除onClick

function startTimer() {

    document.getElementById("countdown").style.display = "inline";
    document.getElementById("btn").style.display = "none";
    userInput = 10;
    if (userInput.length == 0) {
        alert("Please enter a value");
    } else {
        var numericExpression = /^[0-9]+$/;

        function display(notifier, str) {
            document.getElementById(notifier).innerHTML = str;
        }

        function toMinuteAndSecond(x) {
            return Math.floor(x / 60) + ":" + x % 60;
        }

        function setTimer(remain, actions) {
            (function countdown() {
                display("countdown", toMinuteAndSecond(remain));
                actions[remain] && actions[remain]();
                (remain -= 1) >= 0 && setTimeout(countdown, 1000);
            })();
        }

        setTimer(userInput, {
            0: function () {
                alert("Time Is Up. Please Sumbit Vote.");
                 document.getElementById("btn").style.display = "inline";
                 document.getElementById("countdown").style.display = "none";
            }
        });
    }
}