Jos Sat
Jos Sat

Reputation: 83

Error while string array into list view in android

I am working on putting string array into list view.When I do this I got an error

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)

My java code is

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;
}

all I want to do that String array named resultto list view while I doing the value to list view using an array adapter be like this

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

How to solve the error and how to get the value of string array into that list view I mentioned.

Upvotes: 1

Views: 835

Answers (2)

romtsn
romtsn

Reputation: 11982

The problem is that you are trying to pass the instance of the AsyncTask as a parameter to ArrayAdapter constructor. You should pass the activity instance instead:

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

EDIT:

I noticed another problem that you have - your AsyncTask returns a Void type, but you are waiting for String[] type. So you need to change the last parameter in generic definition, return type, and parameter type like this:

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

Upvotes: 1

otranzer
otranzer

Reputation: 326

The problem is the "this" in your ArrayAdaptor constructor: it is a reference to your AsyncTask (PrimaryActivity.FetchWebsiteData) and not a Context. You should replace it with a Context (PrimaryActivity.this for instance)

Upvotes: 0

Related Questions