【问题标题】:Passing TextView details from RecycleView to next activity将 RecycleView 中的 TextView 详细信息传递给下一个活动
【发布时间】:2018-03-01 07:37:12
【问题描述】:

我这里有个问题,如何将这个RecycleView 选中的项目传递给另一个Activity 并从数据库中获取数据到详细信息Activity

提前致谢!


适配器列表

 @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;
    }
  }
}

我已经制作了 DetailActivity,但我不知道如何将数据从数据库中获取到我的 Activity。

【问题讨论】:

  • Bundles 一般用于在各种 Android Activity 之间传递数据......所以你应该使用 bundle
  • 我想根据列表中每个数据的“Id Number”在新活动中详细显示列表中的数据
  • 如果您想将选定的项目数据显示到下一个屏幕,然后获取所有TextView 的所有数据并使用IntentBundle 将其传递到下一个屏幕,或者只是将 id 传递到下一个屏幕和使用该 ID 从Database 获取数据

标签: android


【解决方案1】:

您使用了点击事件接口并在回收器视图中单击任何项​​目时传递数据...使适配器进行一些更改

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

然后当您获得响应值并设置适配器时调用下面的代码..

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

我希望你在 android 清单文件中定义所有活动。

【讨论】:

  • 我在哪里获得 mDataList 和 mAdapter 在我的活动中?
  • 在适配器绑定数据中,数据使一个 pojo 类像数据和适配器在定义 arraylist 像 ListmDatalist=new ArrayList()。然后在 onBind 方法调用后,如 Data object=mDatalist.get(position)。在 Main 活动中定义您的适配器对象并制作 arraylist 以获取响应数据以添加该列表,然后传递适配器构造函数。一旦您的适配器获得的值不为空,则调用 onItemClick 方法。
猜你喜欢
  • 2016-08-08
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2015-01-26
  • 1970-01-01
  • 2013-08-09
  • 2018-10-18
相关资源
最近更新 更多