【问题标题】:How to use returned arraylist?How to retrieve data from arraylist?如何使用返回的arraylist?如何从arraylist中检索数据?
【发布时间】:2015-07-01 04:27:00
【问题描述】:

在下面的代码中,我在另一个类 Use 的帮助下保存了三个值 fun_id、fun_logo、fun_req。在代码中返回了一个数组列表,现在我想检索 fun_id、fun_logo、fun_req。我想将 fun_logo 添加到 imagearray。我想知道如何使用 Class Use 来检索数据。我目前正在学习所以只是关于 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;

        }


    }

使用.java

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

}

【问题讨论】:

  • 将变量 String imageUrl 添加到你的类中使用并使用它
  • 你需要一本 C++/JAVA 书籍来学习 OOPS 基础知识!
  • @PareshMayani 哈哈..看看我的代码,你是这么认为的..ya ...关于 OOPS 概念的模糊概念....以某种方式运行代码并获得输出...
  • @HariSankar 我的观察表明,因为您已经全局声明了所有变量,这不是声明和使用变量的优化方式。范围应该是有限的。
  • @PareshMayani 正在查看您在某个博客上找到的示例以下载图像并在列表视图中显示..

标签: android class arraylist android-asynctask


【解决方案1】:

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

Use 中的所有字段都是 publicresUse 类的对象的 ArrayList。访问每个对象的值:

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

【讨论】:

    【解决方案2】:
    1. 使用 getter 和 setter 方法定义 Use 类。
    2. Use 类中定义一个方法getFunLogos(),它将返回所有有趣徽标的图像数组。随时随地访问此方法。

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

    【讨论】:

      【解决方案3】:

      我想将 fun_logo 添加到 imagearray 中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多