我一直希望在列表视图中显示多个数据检索。但是,当我使用阵列适配器时,我一直面临着这个问题。我该怎么解决?这是我获取数据并将其存储在数组列表中的类。稍后将在另一个类中调用数组列表以将其放入数组适配器中。
public class connect2 extends AsyncTask<String, Void, String>{
public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
View view;
Activity activity;
public static final String SEARCH = "product_img1";
ArrayList<String> list = new ArrayList<String>();
ContactObjects co = new ContactObjects();
Bitmap bitmap;
public connect2(Activity activity, View v) {
this.activity = activity;
view = v;
}
String convertStreamToString(InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}
protected String doInBackground(String... arg0) {
String ipAddress = "http://192.168.43.214/apexStore2/";
try {
URL url = new URL(ipAddress +"image1.php");
String urlParameters =
URLEncoder.encode("cat_id", "UTF-8") + "=" +
URLEncoder.encode(arg0[0], "UTF-8") + "&" +
URLEncoder.encode("product_img1", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
//System.out.println(response.toString());
JSONObject mainObject = new JSONObject(response.toString());
JSONArray uniObject = mainObject.getJSONArray("result");
for(int i = 0; i < uniObject.length(); i++) {
JSONObject rowObject = uniObject.getJSONObject(i);
//EventObject co = new EventObject();
co.img1 = ipAddress +"img/products/" +
rowObject.getString("product_img1");
//mContentItems.add(co);
System.out.println("hi" +co.title);
list.add(co.img1);
}
//To further break down JSON
//JSONObject oneObject = mainObject.getJSONObject("1");
//String id = oneObject.getJSONObject("id");
try{
}
finally{
connection.disconnect();
}
} catch (Exception e){
System.out.println(e.toString());
}
return "";
}
protected void onPreExecute(){
}
@Override
protected void onPostExecute(String result){
Intent intent = new Intent(view.getContext(), CatalogActivity.class);
intent.putExtra(SEARCH, list);
activity.startActivity(intent);
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
ImageView img;
public LoadImage(ImageView img){
this.img = img;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new
URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
}else{
}
}
}
}
这是我调用数据并将其放入数组适配器的类。
public class CatalogActivity extends Activity {
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catalog);
// Create the list
ListView listViewCatalog = (ListView)
findViewById(R.id.ListViewCatalog);
setContentView(R.layout.item);
new connect2 (this,
this.findViewById(android.R.id.content)).execute("3");
Intent intent = getIntent();
String search = intent.getStringExtra(connect2.SEARCH);
adapter = new ArrayAdapter<String>(this, R.layout.item,
R.id.ImageViewItem, search);
listViewCatalog.setAdapter(adapter);
}
}
答案 0 :(得分:0)
问题似乎是您正在使用ImageView
的资源ID,而ArrayAdapter
仅使用TextView
作为资源ID。
编辑 - 由于您的字符串只是文字,因此容器不能是ImageView
。
另一个编辑 - 您需要传递List
个字符串作为参数作为第4个参数。将其添加为第4个参数: - new String [] {search}