字符串数组进入android中的列表视图时出错

时间:2015-04-14 08:10:36

标签: java android arrays android-listview android-arrayadapter

我正在将字符串数组放入列表视图。当我这样做时,我收到了错误

Error:(127, 47) error: no suitable constructor found for ArrayAdapter(PrimaryActivity.FetchWebsiteData,int,Void)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; PrimaryActivity.FetchWebsiteData cannot be converted to
Context)constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not   applicable(argument mismatch; PrimaryActivity.FetchWebsiteData cannot be   converted to Context)constructor    ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; PrimaryActivity.FetchWebsiteData cannot be converted to Context)

我的java代码是

protected Void doInBackground(Void... params) {

    ArrayList<String> hrefs=new ArrayList<String>();
    try {

        // Connect to website                
    } catch (IOException e) {

        e.printStackTrace();
    }

    //parsing first URL
    try {

        //listValue=colValue.text();
        String subString=colValue.text();
        String[] result=subString.split("(?<=[a-z])\\.(?=\\s*[A-Z])|[,:]");
        //expose all array values
        //listValue=result[1];
        /*
        for(int i=0;i<result.length;i++) {

            listValue=result[0];
        }*/
    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;
}

当我使用String array named result列出视图的值时,我想list viewarray adapter的所有内容都是这样的

protected void onPostExecute(Void result) {

    ListView list=(ListView)findViewById(R.id.listShow);
    ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result);
    list.setAdapter(arrayAdapter);
    mProgressDialog.dismiss();
}

如何解决错误以及如何将字符串数组的值放入我提到的列表视图中。

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试将AsyncTask的实例作为参数传递给ArrayAdapter构造函数。您应该改为传递活动实例:

ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(PrimaryActivity.this, android.R.layout.simple_list_item_1, result);

<强> 编辑:

我注意到您遇到了另一个问题 - 您的AsyncTask返回Void类型,但您正在等待String[]类型。因此,您需要更改泛型定义,返回类型和参数类型中的最后一个参数,如下所示:

class FetchWebsiteData extends AsyncTask<Void, Void, String[]> {
    protected String[] doInBackground(Void... params) {
        ArrayList<String> hrefs=new ArrayList<String>();
        try {
            // Connect to website                
        } catch (IOException e) {
            e.printStackTrace();
        }

        //parsing first URL
        try {

            //listValue=colValue.text();
            String subString=colValue.text();
            String[] result=subString.split("(?<=[a-z])\\.(?=\\s*[A-Z])|[,:]");
            return result;
            //expose all array values
            //listValue=result[1];
            /*
            for(int i=0;i<result.length;i++)
            {
                 listValue=result[0];
             }*/

         } catch (IOException e) {
              e.printStackTrace();
         }
        return null;
    }
    protected void onPostExecute(String[] result) {            
        ListView list=(ListView)findViewById(R.id.listShow);
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(PrimaryActivity.this, android.R.layout.simple_list_item_1, result);
        list.setAdapter(arrayAdapter);
        mProgressDialog.dismiss();
    }
}

答案 1 :(得分:0)

问题是ArrayAdaptor构造函数中的“this”:它是对AsyncTask(PrimaryActivity.FetchWebsiteData)的引用,而不是Context。 您应该将其替换为Context(例如PrimaryActivity.this)