0

I have a problem here, how can I pass this RecycleView selected item to another Activity and get data from database to Detail Activity?

Thanks in advance!

My Android Image


AdapterList

 @Override
        public void onBindViewHolder(final ViewHolder holder, final int position){
            Glide.with(context)
                    .load("http://192.168.56.1/signalss/image/" + list_data.get(position).get("Icon"))
                    .crossFade()
                    .placeholder(R.mipmap.ic_launcher)
                    .into(holder.imgsignals);
            holder.txtsignals1.setText(list_data.get(position).get("SignalsId"));
            holder.txtsignals2.setText(list_data.get(position).get("PairName"));
            holder.txtsignals3.setText(list_data.get(position).get("SignalsPosition").equals("B") ? "BUY" : "SELL");
            holder.txtsignals4.setText(list_data.get(position).get("AreaOpenPrice1"));
            holder.txtsignals5.setText(list_data.get(position).get("AreaOpenPrice2"));
            holder.txtsignals6.setText(list_data.get(position).get("StopLoss"));
            holder.txtsignals7.setText(list_data.get(position).get("TargetProfit1"));
            holder.txtsignals8.setText(list_data.get(position).get("TargetProfit3"));
            holder.txtsignals9.setText(list_data.get(position).get("AddDate"));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), DetailSignals.class);
                    i.putExtra("SignalsId", list_data.get(position));
                    v.getContext().startActivity(i);
                }
            });



            if (list_data.get(position).get("SignalsPosition").equals("B")){
                ((LinearLayout)holder.linearLayout).setBackgroundColor(Color.parseColor("#FF00F449"));
                ((LinearLayout)holder.linearLayout1).setBackgroundColor(Color.parseColor("#FF00F449"));
            }else{
                ((LinearLayout)holder.linearLayout).setBackgroundColor(Color.parseColor("#f40000"));
                ((LinearLayout)holder.linearLayout1).setBackgroundColor(Color.parseColor("#f40000"));
            }
        }

tab1

@Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        String url = "http://192.168.56.1/signalss/getActive.php";

        View v =inflater.inflate(R.layout.tabmenu,container,false);
        recyclerView = (RecyclerView) v.findViewById(R.id.rvtab1);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(llm);

        requestQueue = Volley.newRequestQueue(getActivity());

        list_data = new ArrayList<HashMap<String, String>>();
        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.d("response", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("listactive");
                    for (int a = 0; a < jsonArray.length(); a++) {
                        JSONObject json = jsonArray.getJSONObject(a);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("SignalsId", json.getString("SignalsId"));
                        map.put("Icon", json.getString("Icon"));
                        map.put("PairName", json.getString("PairName"));
                        map.put("SignalsPosition", json.getString("SignalsPosition"));
                        map.put("AreaOpenPrice1", json.getString("AreaOpenPrice1"));
                        map.put("AreaOpenPrice2", json.getString("AreaOpenPrice2"));
                        map.put("StopLoss", json.getString("StopLoss"));
                        map.put("TargetProfit1", json.getString("TargetProfit1"));
                        map.put("TargetProfit3", json.getString("TargetProfit3"));
                        map.put("AddDate", json.getString("AddDate"));
                        list_data.add(map);
                        AdapterList adapter = new AdapterList(tab1.this, list_data);
                        recyclerView.setAdapter(adapter);


                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        requestQueue.add(stringRequest);

        //Calling asyn task to get json
        new tab1.GetContacts().execute();
        return v;

        }

DetailActivity

public class DetailSignals extends AppCompatActivity {

TextView posisi, status, entry, profit, stop, stat, result;

private static String url = "http://192.168.56.1/signalss/getId.php";

// JSON Node Names
private static final String TAG_CONTACTS = "listactive";

private static final String TAG_POSISI = "SignalsPosition";
private static final String TAG_STATUS = "PairName";
private static final String TAG_ENTRY = "AreaOpenPrice1";
private static final String TAG_PROFIT = "TargetProfit1";
private static final String TAG_STOP = "StopLoss";
private static final String TAG_STAT = "Status";
private static final String TAG_RESULT = "Result";

// contacts JSONArray
JSONArray contacts = null;

// Progress Dialog
private ProgressDialog pDialog;



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

posisi = (TextView)findViewById(R.id.posisi);
status = (TextView)findViewById(R.id.status);
entry = (TextView)findViewById(R.id.entry);
profit = (TextView)findViewById(R.id.target);
stop = (TextView)findViewById(R.id.stop);
stat = (TextView)findViewById(R.id.stat);
result = (TextView)findViewById(R.id.result);

//Loading in Backgtound Thread
new GetContext().execute();
}

class GetContext extends AsyncTask<String, String, String>{

@Override
protected void onPreExecute(){
    super.onPreExecute();
    pDialog = new ProgressDialog(DetailSignals.this);
    pDialog.setMessage("Please Wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
}

@Override
protected String doInBackground(String... args) {
    ServiceHandler sh = new ServiceHandler();

    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

    Log.d("Response : ", "> " + jsonStr);

    if (jsonStr !=null){
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            contacts = jsonObj.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);

                String posisi = c.getString(TAG_POSISI);
                String status = c.getString(TAG_STATUS);
                String entry = c.getString(TAG_ENTRY);
                String target = c.getString(TAG_PROFIT);
                String stop = c.getString(TAG_STOP);
                String stat = c.getString(TAG_STAT);
                String result = c.getString(TAG_RESULT);

                HashMap<String, String> contact = new HashMap<String, String>();

                contact.put(TAG_POSISI, posisi);
                contact.put(TAG_STATUS, status);
                contact.put(TAG_ENTRY, entry);
                contact.put(TAG_PROFIT, target);
                contact.put(TAG_STOP, stop);
                contact.put(TAG_STAT, stat);
                contact.put(TAG_RESULT, result);

            }
        } catch (JSONException e){
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }
    return null;
    }
  }
}

I already make the DetailActivity, but I don't know how to intent and get data from database to my activity.

R G
  • 461
  • 3
  • 15
Eiiryu
  • 11
  • 5
  • 1
    Possible duplicate of https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Santanu Sur Mar 01 '18 at 07:39
  • Bundles are generally used for passing data between various Android activities....So you should use bundle – Vikas singh Mar 01 '18 at 07:40
  • I want to display data from the list in details in the new activity based on the "Id Number" of each data in the list – Eiiryu Mar 01 '18 at 07:50
  • If you want to display selected item data to next screen then take all data of all `TextView` and pass it to next screen using `Intent` or`Bundle` or just pass id to next screen and get data from `Database` using that id – R G Mar 01 '18 at 08:20

1 Answers1

1

you used interface for click event and pass the data when recycler view in click any item like ... make adapter few changes

OnRecyclerItemClickListener onRecyclerItemClickListener;

public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener) {
    this.onRecyclerItemClickListener = onRecyclerItemClickListener;
}

public interface OnRecyclerItemClickListener {
    void onViewItemClicked(MyData myData);// your pojo class you bind in recyclerview.
}
 @Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    final MyData data=mDataList.get(position);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onRecyclerItemClickListener.onViewItemClicked(data);
        }
    });

then after when you get response value and set adapter that time call below code..

 mAdapter.setOnRecyclerItemListener(new HomeAdapter.OnRecyclerItemClickListener() {
        @Override
        public void onViewItemClicked(MyData myData) {
            Intent i = new Intent(v.getContext(), DetailSignals.class);
            i.putExtra("SignalsId", myData.getSifnalsId());//set your value to pass.
            startActivity(i);
        }
    });

and i hope you define all the activity in android manifest file.

  • where i get mDataList and mAdapter in my activity? – Eiiryu Mar 01 '18 at 10:41
  • in adapter bind data that data make one pojo class like data and adapter in define arraylist like ListmDatalist=new ArrayList(). and then after onBind Method call like Data object=mDatalist.get(position). In Main activity define your adapter object and make arraylist for getting response data to add that list and then pass adapter constructor. once your adapter get value not null that time call onItemClick method. –  Mar 01 '18 at 13:59