我希望在类函数中使用for循环动态传递值来保存数据,这样我怎样才能在java中执行此操作
当我实现这个代码时,这只放了一个我希望将值传递给for循环的图像
这里我把我的代码检查了
CustomData.java
/** This is just a simple class for holding data that is used to render our custom view */
public class CustomData
{
private String mText;
public CustomData(String text) {
mText = text;
}
public String getText()
{
return mText;
}
}
Activityclass.java
public class ListMobileActivity extends Activity {
Button btnradio;
ListView lv;
private ProgressDialog mProgress;
ArrayList<HashMap<String, String>> menulist = new ArrayList<HashMap<String,String>>();
ArrayList<String> test= new ArrayList<String>();
List<String> custom;
int size=1;
public CustomData[] mCustomData= new CustomData[size];
private HorizontalListView myhorizontalview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listmobileactivity);
myhorizontalview = (HorizontalListView) findViewById(R.id.myhorizontalview);
new MenuAsynctask().execute("");
myhorizontalview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
}
});
}
public class MenuAsynctask extends AsyncTask<String, ArrayList<HashMap<String,String>>,ArrayList<HashMap<String,String>>>
{
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params)
{
try
{
JsonParser jparser = new JsonParser();
String url="http://www.weboffice.co.at/webservices/services.php?method=content";
String data=jparser.getdata(url);
//Log.e("data","--->"+data);
JSONObject jobject = new JSONObject(data);
JSONArray jcontent=jobject.getJSONArray("CONTENT");
size=jcontent.length();
for(int i=0;i<jcontent.length();i++)
{
String title=jcontent.getJSONObject(i).get("title").toString();
String come=jcontent.getJSONObject(i).get("short").toString();
String image=jcontent.getJSONObject(i).get("image").toString();
mCustomData[i] = new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return menulist;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result)
{
Log.e("data","--->"+mCustomData.toString());
CustomArrayAdapter adapter = new CustomArrayAdapter(ListMobileActivity.this, mCustomData);
myhorizontalview.setAdapter(adapter);
}
}
}
i want to create array like this how to achieve this
public CustomData[] mCustomData = new CustomData[] {
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
new CustomData("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"),
};
答案 0 :(得分:3)
使用
CustomData mCustomData= new CustomData(image);
要初始化,您应该将其存储在ArrayList
中以便稍后访问数据
<强>更新强>
CustomData[] dataList = new CustomData[SIZE];
for(int i = 0; i < SIZE; i++)
{
dataList[i] = new CustomData(image);
}
如果您没有固定尺寸,请使用
ArrayList<CustomData> dataList = new ArrayList<CustomData>();
dataList.add(new CustomData(image));
and to retrieve use
dataList.get(position);
尝试
public CustomData[] mCustomData; // Remove initialization
在MenuAsynctask
doInBackground
mCustomData = new CustomData[jcontent.length()];
它会起作用