从列表视图单击位置获取数据

时间:2018-07-21 18:07:02

标签: android

我正在使用以下方法从远程JSON文件填充列表视图:

private class GetHttpResponseReuniones extends AsyncTask<Void, Void, Void>
{
    public Context context;




    String ResultHolder;

    List<SubastasHome> subastasHomeList;

    public GetHttpResponseReuniones(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {




        String URLFinal = ServerURLReuniones;



        HttpServicesClass httpServiceObject = new HttpServicesClass(URLFinal);
        try
        {
            httpServiceObject.ExecutePostRequest();

            if(httpServiceObject.getResponseCode() == 200)
            {
                ResultHolder = httpServiceObject.getResponse();

                if(ResultHolder != null)
                {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(ResultHolder);

                        JSONObject jsonObject;

                        SubastasHome SubastasHome;

                        subastasHomeList = new ArrayList<SubastasHome>();

                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            SubastasHome = new SubastasHome();

                            jsonObject = jsonArray.getJSONObject(i);

                            Log.d("SUBASTAS ","SUBASTAS:"+jsonObject);

                            SubastasHome.id_subasta = jsonObject.getString("id_subasta");
                            SubastasHome.marca = jsonObject.getString("marca");
                            SubastasHome.modelo = jsonObject.getString("modelo");
                            SubastasHome.ano = jsonObject.getString("ano");
                            SubastasHome.foto = jsonObject.getString("foto");
                            SubastasHome.referencia = jsonObject.getString("referencia");
                            SubastasHome.titulo = jsonObject.getString("titulo");
                            SubastasHome.descripcion = jsonObject.getString("descripcion");
                            SubastasHome.empieza = jsonObject.getString("empieza");
                            SubastasHome.termina = jsonObject.getString("termina");
                            SubastasHome.comprador = jsonObject.getString("comprador");
                            SubastasHome.vendedor = jsonObject.getString("vendedor");
                            SubastasHome.puja_ganadora = jsonObject.getString("puja_ganadora");
                            SubastasHome.generacion = jsonObject.getString("generacion");
                            SubastasHome.serie = jsonObject.getString("serie");
                            SubastasHome.motor = jsonObject.getString("motor");
                            SubastasHome.total_pujas = jsonObject.getString("total_pujas");
                            SubastasHome.mejor_precio = jsonObject.getString("mejor_precio");
                            // cumpleanos.SubjectName2 = jsonObject.getString("subjects2");
                            String termina = jsonObject.getString("termina");



                                    //COUNTDOWN here


                                    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String datetermina = termina;
                            try {
                                date1 = (Date) formatter.parse(datetermina);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }

                            date2 = Calendar.getInstance().getTime();

                            diff = date1.getTime() - date2.getTime();
                            Log.d("TERMINA","termina:date diff "+diff);


                                    long dias = TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toHours(diff));

                                    long horas = (TimeUnit.MILLISECONDS.toHours(diff) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(diff)));

                                    long mins =(TimeUnit.MILLISECONDS.toMinutes(diff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diff)));

                                    long seconds = ((TimeUnit.MILLISECONDS.toSeconds(diff) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(diff))));

                                    SubastasHome.termina = dias+"d "+horas+"h "+mins+"m ";

                                    if (dias < 0 && horas <0 && mins < 0){
                                        SubastasHome.termina = "Finalizada";
                                    }




                            subastasHomeList.add(SubastasHome);
                        }
                    }
                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                Toast.makeText(context, httpServiceObject.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {


        progressBarSubject.setVisibility(View.GONE);

        //CumplesListView.setVisibility(View.VISIBLE);

        if(subastasHomeList != null)
        {
            ListAdapterClassSubastasHome adapter = new ListAdapterClassSubastasHome(subastasHomeList, context);

            SubastasListView.setAdapter(adapter);




        }
    }
}

它工作正常,但是现在我需要从单击的行中获取数据。 我在片段中的oncreate方法中包含了此代码:

 SubastasListView.setClickable(true);
        SubastasListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {




            }
        });

我正在从单击的行中获取项目位置,但是如何从该项目位置获取所有数据以进一步使用它们呢?

例如,我需要数据id_subasta作为单击位置。

2 个答案:

答案 0 :(得分:1)

String id ;
SubastasListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        id  = subastasHomeList.get(position).id_subasta  ;
        // Similarly Rest of fields can be retrieved on item click

     }
});

答案 1 :(得分:1)

您可以通过getter和setter实现它,如下所示。然后执行asyncTask时,您将需要扩展异步任务的类的对象,然后通过其对象获取getList方法。

List<SubastasHome> tempList = new ArrayList<>(); // Create tempList at top of the class..    


@Override
protected void onPostExecute(Void result)

{
    progressBarSubject.setVisibility(View.GONE);
    //CumplesListView.setVisibility(View.VISIBLE);
    if(subastasHomeList != null)
    {
        setList(subastasHomeList); // there set the List..
        ListAdapterClassSubastasHome adapter = new ListAdapterClassSubastasHome(subastasHomeList, context);
        SubastasListView.setAdapter(adapter);
    }
}


public void setList(List<SubastasHome> mSubastasHomeList){
    tempList = mSubastasHomeList;
}

public List<SubastasHome> getList(){
    return tempList;
}

该类的make对象具有AsyncTask并执行线程。

GetHttpResponseReuniones getHttpResp = new GetHttpResponseReuniones();
getHttpResp.execute();


 SubastasListView.setClickable(true);
    SubastasListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick (AdapterView < ? > parent, View view,int position, long id){
        SubastasHome subastasHome = getHttpResp.getList().get(position); // there get object..
        subastasHome.get....() /// get your data from this object.....
    }
}