如何使用返回的arraylist?如何从arraylist中检索数据?

时间:2015-04-23 06:49:57

标签: android class arraylist android-asynctask

在下面的代码中,我在另一个类Use的帮助下保存了三个值fun_id,fun_logo,fun_req。在代码中返回了一个arraylist,现在我想要检索fun_id,fun_logo,fun_req.I想要将fun_logo添加到imagearray中。我想知道如何使用类使用来恢复数据。我正在学习当前所以只有小想法回合android。

 ArrayList<Use> stringArrayList = null;
    ArrayList<Use> res = null;
    ArrayList<String> linklist = null;
    String getdetailsurl;
    String  link [];
    String[] ar;
    Use[] getalldet;
   public String imagearray[]; 

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         new Getdetailsfromweb().execute();

        list=(ListView)findViewById(R.id.listView1);
      // adapter=new myadpter(this, imagearray);
        list.setAdapter(adapter);


    }
public class Getdetailsfromweb extends AsyncTask<String,Void,String>
    {

        String result = "";

        @Override
        protected String doInBackground(String... params) {

            getdetailsurl=Webcall.getdet();

            if(getdetailsurl!=null)
            {

                result = "Success";
            }
            else
            {
                result = "Failure";

            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            res = new ArrayList<Use>();

            try {
                if (result.contentEquals("Success")) 
                {

                    res=passdetails();
                    System.out.println("lsize is " + res.size());
                    for(int i=0;i<res.size();i++)
                    {
                        // To retreive value what should i do here



                    imagearray[i]=obj.funlogo;
                     System.out.println("logo is " + imagearray[i]);

                    }


                } 
                else 
                {

                }
            } catch (Exception e1) {

                e1.printStackTrace();
            }


        }



public   ArrayList<Use> passdetails()
        {
            JSONArray array = null;
            stringArrayList = new ArrayList<Use>();
            linklist= new ArrayList<String>();
            Use usee;
            try {
                array = new JSONArray(getdetailsurl);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             for(int i = 0;i <array.length() ;i++ ) 
             {

                 String fun_id= null;
                 String fun_logo= null;
                 String fun_req = null;
                try {
                    fun_id = array.getJSONObject(i).getString("up_id");

                    System.out.println("up_id is " + fun_id);

                      fun_logo=array.getJSONObject(i).getString("logo");
                     System.out.println("logo is " + fun_logo);

                      fun_req=array.getJSONObject(i).getString("requirements");
                     System.out.println("req is " + fun_req);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                usee=new Use();

                usee.funid=fun_id;
                 System.out.println("fun is " + usee.funid);
                usee.funlogo=fun_logo;
                 System.out.println("fun is " + usee.funlogo);
                usee.funreq=fun_req;
                 System.out.println("fun is " + usee.funreq);

                 linklist.add(fun_logo);
                 stringArrayList.add(usee);

             }

            return stringArrayList;

        }


    }

Use.java

public class Use {
    public static String funid;
    public String funlogo;
    public  String funreq;

}

3 个答案:

答案 0 :(得分:2)

  

我想知道如何使用Class Use来检索数据

public中的所有字段均为UseresUse类对象的ArrayList。从每个对象访问值:

for(int i=0;i<res.size();i++)
 Use useObj=res.get(i);
 String funlogo=useObj.funlogo;
 String funreq=useObj.funreq;
 ...
}

答案 1 :(得分:1)

  1. 使用getter和setter方法定义Use类。
  2. getFunLogos()类中定义一个方法Use,它将返回所有有趣徽标的图像数组。从任何地方访问此方法。

    protected String[] getFunLogos(ArrayList<Use> listFunLogos) {
    for(int i=0;i<res.size();i++)
    {
       // To retreive value what should i do here
       imagearray[i]=obj.funlogo;
       System.out.println("logo is " + imagearray[i]);
    }
    }
    

答案 2 :(得分:0)

  

我想将fun_logo添加到imagearray

//linklist : as you are adding fun_log in it
imagearray=new String[linklist.size()];

linklist.toArray(imageArray);