2015-04-23 81 views
-1

在下面的代碼中,我在另一個類的幫助下保存了三個值fun_id,fun_logo,fun_req。在代碼中,ArrayList返回,現在我想要檢索fun_id,fun_logo,fun_req.I想要添加fun_logo到imagearray.I想知道如何使用類使用 retreving數據。我正在學習如此只是一個小想法回合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; 

} 
+0

添加變量字符串IMAGEURL到類,用其 – apk

+0

你需要一個C++/JAVA本書來學習OOPS基本面! –

+0

@PareshMayani哈哈...在我的代碼中,你認爲這麼.. .. ...關於OOPS概念的模糊想法....以某種方式運行代碼和獲得輸出... –

回答

1
  1. 定義Use類getter和setter方法。
  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]); 
    } 
    } 
    
2

我想知道如何使用類使用的檢索數據

所有字段是在Userespublic是ArrayList的Use類的對象。來自每個接入值作爲對象:

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

我想補充fun_logo到imagearray

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

linklist.toArray(imageArray); 
相關問題