如何在listview中使用userId?

时间:2016-02-01 12:10:45

标签: java android listview

我有一个与ListView相关的问题,在这个listview中每一行都有自己的auction_id,我想在点击行时使用该auction_id并在另一个Activity中使用auction_id作为JSON输入,请帮帮我

这是我的代码

private void getMyAuctionList(JSONObject response){
    //MyAuctionListBean listBean = new MyAuctionListBean();
    List<MyAuctionListBean> list = new ArrayList<MyAuctionListBean>();

    if(response != null){
        try {
            JSONArray jsonArray = response.getJSONArray("success");

            for (int i = 0 ; i < jsonArray.length(); i++){
                MyAuctionListBean listBean = new MyAuctionListBean();
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                listBean.setAuction_id(jsonObject1.getString("auction_id"));
                listBean.setEvent_name(jsonObject1.getString("event_name"));
                listBean.setContact_no(jsonObject1.getString("contact_no"));
                listBean.setAuction_date(jsonObject1.getString("auction_date"));
                listBean.setEnd_date(jsonObject1.getString("end_date"));
                listBean.setLocation(jsonObject1.getString("location"));
                listBean.setPeople_range(jsonObject1.getString("people_range"));
                listBean.setPrice_range(jsonObject1.getString("price_range"));
                listBean.setIs_active(jsonObject1.getString("is_active"));
                listBean.setTotal_bid_count(jsonObject1.getString("total_bid_count"));
                listBean.setTotal_quoted(jsonObject1.getString("total_quoted"));
                listBean.setTAproved(jsonObject1.getString("TAproved"));
                listBean.setAuction_description(jsonObject1.getString("auction_description"));
                list.add(listBean);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    hAdapter = new MyAuctionHistoryAdapter(getActivity(),list);
    hRecyclerView.setAdapter(hAdapter);
}

这是第二个活动

private void getAuctionDetail(){

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JSONObject jsonObject = null;
    LoginBean loginBean = new LoginBean();
    mSharedPreferences = getSharedPreferences("user_preference", Context.MODE_PRIVATE);
    if(mSharedPreferences==null)
        return;
    String authorizationKey = mSharedPreferences.getString("authorization_key","");
    String userId = mSharedPreferences.getString("userId", "");
    //MyAuctionListBean listBean = new MyAuctionListBean();

    try {

        jsonObject = new JSONObject();
        jsonObject.putOpt("authorization_key",authorizationKey);
        jsonObject.putOpt("in_user_id",userId);
        jsonObject.putOpt("device_token","1111");
        jsonObject.putOpt("mac_address","1111");
        jsonObject.putOpt("gps_latitude","1111");
        jsonObject.putOpt("gps_longitude","1111");
        jsonObject.putOpt("in_auction_id","");

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

    CustomJSONObjectRequest jsonObjectRequest = new CustomJSONObjectRequest(Request.Method.POST, Constants.MYAUCTION_DETAIL_URL, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            getMyAuctionDetails(response);
            Utility.showLogError(AuctionActivity.this, "GetAuctionDetailsResponse" + response.toString());
            pDialog.hide();


        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            pDialog.hide();
        }
    });
    Utility.showLogError(AuctionActivity.this, "Error in Detail"+"GetAuctionDetails URL = " + Constants.MYAUCTION_DETAIL_URL);
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

}

private void getMyAuctionDetails(JSONObject response) {
    MyAuctionDetailsBean myAuctionDetailsBean = new MyAuctionDetailsBean();
    if (response != null){
        try {
            JSONArray jsonArray = response.getJSONArray("success");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                myAuctionDetailsBean.setAuction_id(jsonObject.getString("auction_id"));
                myAuctionDetailsBean.setContact_person(jsonObject.getString("contact_person"));
                myAuctionDetailsBean.setPrimary_contact_no(jsonObject.getString("primary_contact_no"));
                myAuctionDetailsBean.setAlternate_contact_no(jsonObject.getString("alternate_contact_no"));
                myAuctionDetailsBean.setAuction_title(jsonObject.getString("auction_title"));
                myAuctionDetailsBean.setClient_email(jsonObject.getString("client_email"));

                myAuctionDetailsBean.setStart_date(jsonObject.getString("start_date"));
                myAuctionDetailsBean.setEnd_date(jsonObject.getString("end_date"));
                myAuctionDetailsBean.setLocation(jsonObject.getString("location"));

                myAuctionDetailsBean.setPeople_range(jsonObject.getString("people_range"));
                myAuctionDetailsBean.setPrice_range(jsonObject.getString("price_range"));
                myAuctionDetailsBean.setFeatures_id(jsonObject.getString("features_id"));
                myAuctionDetailsBean.setCuisine_id(jsonObject.getString("cuisine_id"));
                myAuctionDetailsBean.setIs_active(jsonObject.getString("is_active"));
                myAuctionDetailsBean.setTheme_id(jsonObject.getString("theme_id"));
                myAuctionDetailsBean.setEvent_type_id(jsonObject.getString("event_type_id"));
                myAuctionDetailsBean.setTotal_bid_count(jsonObject.getString("total_bid_count"));
                myAuctionDetailsBean.setTotal_quoted(jsonObject.getString("total_quoted"));
                myAuctionDetailsBean.setTAproved(jsonObject.getString("TAproved"));
                myAuctionDetailsBean.setAuction_description(jsonObject.getString("auction_description"));
                myAuctionDetailsBean.setEvent_name(jsonObject.getString("event_name"));
            }

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

    partyType.setText(myAuctionDetailsBean.getEvent_name());
    location.setText(myAuctionDetailsBean.getLocation());
    date.setText(myAuctionDetailsBean.getStart_date());
    time.setText(myAuctionDetailsBean.getEnd_date());
    theme.setText(myAuctionDetailsBean.getTheme_id());
    noOfPeople.setText(myAuctionDetailsBean.getPeople_range());
    mobileNo.setText(myAuctionDetailsBean.getPrimary_contact_no());
    emailId.setText(myAuctionDetailsBean.getClient_email());
    cuisine.setText(myAuctionDetailsBean.getCuisine_id());
    requirment.setText(myAuctionDetailsBean.getAuction_description());
    budget.setText(myAuctionDetailsBean.getPrice_range());
    postUser.setText(myAuctionDetailsBean.getContact_person());
}

public void onBackPressed() {
    finish();
}
public class MyAuctionHistoryAdapter extends RecyclerView.Adapter<MyAuctionHistoryAdapter.ViewHolder> {
private List<MyAuctionListBean> list;
private Context hContext;
private ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public MyAuctionHistoryAdapter(Context hContext,List<MyAuctionListBean> list){
    this.hContext = hContext;
    this.list = list;

}

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

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.auction_history_row,parent,false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.partyType.setText(list.get(position).getEvent_name());
    holder.partyLocation.setText(list.get(position).getLocation());
    holder.date.setText(list.get(position).getAuction_date());
    holder.bidCount.setText(list.get(position).getTotal_bid_count());
    holder.priceRange.setText(list.get(position).getPrice_range());
    holder.auctionDetailRowLay.setId(Integer.parseInt(list.get(position).getAuction_id()));
    holder.rowAuction_Id.setText(list.get(position).getAuction_id());


}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private TextView partyType,partyLocation,restroType,budget,requirments,date,priceRange,bidCount;
    private RelativeLayout auctionDetailRowLay;
    private ImageView restoIcon;
    TextView rowAuction_Id;

    public ViewHolder(View view) {

        super(view);
        view.setOnClickListener(this);
        partyType = (TextView)view.findViewById(R.id.H_auctionPartyType);
        partyLocation = (TextView)view.findViewById(R.id.H_auctionLocation);
        restroType = (TextView)view.findViewById(R.id.restro_Name);
        budget = (TextView)view.findViewById(R.id.H_auction_Budget);
        requirments = (TextView)view.findViewById(R.id.H_auctionRequirments);
        date = (TextView)view.findViewById(R.id.H_auctionDate);
        bidCount = (TextView)view.findViewById(R.id.H_auction_Bids);
        priceRange = (TextView)view.findViewById(R.id.H_auction_RangeList);
        auctionDetailRowLay= (RelativeLayout)view.findViewById(R.id.auctionDetailRow);
        rowAuction_Id = (TextView)view.findViewById(R.id.row_Auction_id);


    }
    @Override
    public void onClick(View v) {

        Intent intent = new Intent(hContext, AuctionActivity.class);
        hContext.startActivity(intent);

    }


}

}

2 个答案:

答案 0 :(得分:2)

recycleRview中,您可以使用getPosition获取clickedItem位置 如下所示

@Override
        public void onClick(View view) {
            Log.d(TAG, "onClick " + getAdapterPosition());
            CustomBean bean =list.get(getAdapterPosition())
             String id = bean.getAcutionId()
              // now intent it
         }

通过此职位,您将获得ArrayList

的数据

答案 1 :(得分:0)

首先,给定您声明为“hRecyclerView”的变量的名称,似乎您使用的是RecyclerView而不是Listview。我假设你使用的是RecyclerView。

在MyAuctionHistoryAdapter中,您需要覆盖方法getItemId()和onBindViewHolder()

public class MyAuctionHistoryAdapter extends RecyclerView.Adapter<GenericRecyclerAdapter.ViewHolder< MyAuctionListBean >> {

private List<MyAunctionListBean> mList = new ArrayList<Bean>();

public MyAunctionHistoryAdapter(Context context,ArrayList< MyAuctionListBean> list) {

mList = list;

//each item in the list will have the auction id as their id.
public int getItemId(position) {
  return mList.get(position).getAuncion_id();
}

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


    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View view) {
      Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
      intent.putExtra(SecondActivity.EXTRA,getItemId(position));
      startActivity(intent);
     }

     });
}

现在,每当您点击列表中的某个项目时,它就会进入另一个活动

public class SecondActivity extends Activity {
public static final String EXTRA = "EXTRA";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// here is the intent you need
int aunctionId = getIntent().getStringExtra(EXTRA);



}

}