如何使用返回的数组列表?如何从数组列表中检索数据?
How to use returned arraylist?How to retrieve data from arraylist?
在下面的代码中,我在另一个 class Use.In 代码的帮助下保存了三个值 fun_id、fun_logo、fun_req返回一个数组列表,现在我想检索 fun_id、fun_logo、fun_req.I 想将 fun_logo 添加到 imagearray.I 想知道如何使用Class 使用 检索 data.I 目前正在学习,所以只有一些小想法 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;
}
I would like to know how to use the Class Use for retrieving data
Use
中的所有字段都是public
,res
是Use
class对象的ArrayList。从每个对象访问值:
for(int i=0;i<res.size();i++)
Use useObj=res.get(i);
String funlogo=useObj.funlogo;
String funreq=useObj.funreq;
...
}
- 使用 getter 和 setter 方法定义
Use
class。
在 Use
class 中定义一个方法 getFunLogos()
,它将 return 所有有趣徽标的图像数组。随时随地访问此方法。
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]);
}
}
I want to add fun_logo into imagearray
//linklist : as you are adding fun_log in it
imagearray=new String[linklist.size()];
linklist.toArray(imageArray);
在下面的代码中,我在另一个 class Use.In 代码的帮助下保存了三个值 fun_id、fun_logo、fun_req返回一个数组列表,现在我想检索 fun_id、fun_logo、fun_req.I 想将 fun_logo 添加到 imagearray.I 想知道如何使用Class 使用 检索 data.I 目前正在学习,所以只有一些小想法 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;
}
I would like to know how to use the Class Use for retrieving data
Use
中的所有字段都是public
,res
是Use
class对象的ArrayList。从每个对象访问值:
for(int i=0;i<res.size();i++)
Use useObj=res.get(i);
String funlogo=useObj.funlogo;
String funreq=useObj.funreq;
...
}
- 使用 getter 和 setter 方法定义
Use
class。 在
Use
class 中定义一个方法getFunLogos()
,它将 return 所有有趣徽标的图像数组。随时随地访问此方法。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]); } }
I want to add fun_logo into imagearray
//linklist : as you are adding fun_log in it
imagearray=new String[linklist.size()];
linklist.toArray(imageArray);