我在这里遇到问题,如何将此RecycleView
个选定项目传递给另一个Activity
并从数据库获取数据到详细信息Activity
?
提前致谢!
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;
}
}
}
我已经制作了DetailActivity,但我不知道如何将数据从数据库转移到我的活动中。
答案 0 :(得分:1)
您使用了click事件的接口,并在回收者查看时传递数据,点击任何项目,例如... make adapter less 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);
}
});
然后当你得到响应值并设置适配器时,调用下面的代码..
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清单文件中定义所有活动。