有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误:找不到适合ArrayAdapter的构造函数(activity、int、int、string)

我一直想在列表视图中显示多个数据检索。然而,当我使用阵列适配器时,我一直面临这个问题。我该如何解决它?这是我获取数据并将其存储在数组列表中的类。稍后将在另一个类中调用数组列表以将其放入数组适配器中

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(安卓.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);
}
} 

共 (1) 个答案

  1. # 1 楼答案

    问题似乎是您正在使用ImageView的资源ID,而ArrayAdapter只将TextView作为资源ID

    编辑-由于字符串只是文本,因此容器不能是ImageView

    另一次编辑-需要将List个字符串作为参数传递给第四个参数。将其添加为第四个参数:-新字符串[]{search}