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

Java 错误:找不到适合ArrayAdapter的构造函数(activity,int,int,string),java,android,arrays,Java,Android,Arrays,我一直想在列表视图中显示多个数据检索。然而,当我使用阵列适配器时,我一直面临这个问题。我该如何解决它?这是我获取数据并将其存储在数组列表中的类。稍后将在另一个类中调用数组列表以将其放入数组适配器中 public class connect2 extends AsyncTask<String, Void, String>{ public static final String PRODUCT_INDEX = "PRODUCT_INDEX"; View view; Activity ac

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

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);
}
} 
公共类connect2扩展了异步任务{
公共静态最终字符串PRODUCT\u INDEX=“PRODUCT\u INDEX”;
视图;
活动;
公共静态最终字符串SEARCH=“product\u img1”;
ArrayList=新建ArrayList();
ContactObjects co=新的ContactObjects();
位图;
公共连接2(活动,视图v){
这个。活动=活动;
视图=v;
}
字符串转换器StreamToString(InputStream为){
试一试{
返回新的java.util.Scanner(is).useDelimiter(“\\A”).next();
}catch(java.util.NoSuchElementException e){
返回“”;
}
}
受保护的字符串doInBackground(字符串…arg0){
字符串ipAddress=”http://192.168.43.214/apexStore2/";
试一试{
URL=新URL(ipAddress+“image1.php”);
字符串参数=
URLEncoder.encode(“cat_id”,“UTF-8”)+“=”+
URLEncoder.encode(arg0[0],“UTF-8”)+“&”+
URLEncoder.encode(“产品img1”、“UTF-8”)+“=”+
URLEncoder.encode(“???”,“UTF-8”);
HttpURLConnection连接=(HttpURLConnection)
openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded”);
connection.setRequestProperty(“内容长度”+
toString(urlParameters.getBytes().length));
connection.setRequestProperty(“内容语言”、“en-US”);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(真);
//发送请求
DataOutputStream wr=新的DataOutputStream(
connection.getOutputStream());
writeBytes(urlParameters);
wr.flush();
wr.close();
//得到回应
InputStream is=connection.getInputStream();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
弦线;
StringBuffer响应=新的StringBuffer();
而((line=rd.readLine())!=null){
响应。追加(行);
append('\r');
}
rd.close();
//System.out.println(response.toString());
JSONObject mainObject=新的JSONObject(response.toString());
JSONArray uniObject=mainObject.getJSONArray(“结果”);
for(int i=0;i
这是我调用数据并将其放入数组适配器的类

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);
}
} 
公共类CatalogActivity扩展活动{
阵列适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.catalog);
//创建列表
ListView listViewCatalog=(ListView)
findViewById(R.id.ListViewCatalog);
setContentView(R.layout.item);
新接头2(本、,
this.findviewbyd(android.R.id.content)).execute(“3”);
Intent=getIntent();
字符串搜索=intent.getStringExtra(connect2.search);
适配器=新阵列适配器(此,R.layout.item,
R.id.ImageViewItem,搜索);
setAdapter(适配器);
}
} 

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

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


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

可能重复,但该源的解决方案没有解决我的问题。可能是您需要为此创建自己的
阵列适配器构造函数,其中包含所需的参数。是否确认导入中添加了
ArrayAdapter
?是的,我已导入“导入android.widget.ArrayAdapter;”即使我用这个“String product_title=intent.getStringExtra(connect2.SEARCH1);adapter=newArrayAdapter(CatalogActivity.this,R.layout.it