自定义适配器中未定义构造函数

时间:2013-06-25 00:08:29

标签: android android-listview android-xml android-adapter

我按照本教程制作了一个自定义适配器:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

我更改了自定义适配器以匹配我的类,需要看起来像这样:

package com.example.beerportfoliopro;

public class BeerSearchAdapter extends ArrayAdapter<BeerData>{

    Context context; 
    int layoutResourceId;    
    BeerData data[] = null;

    public BeerSearchAdapter(Context context, int layoutResourceId, BeerData[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        beerHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new beerHolder();
            holder.txtBrewery = (TextView)row.findViewById(R.id.beerBreweryNameList);
            holder.txtTitle = (TextView)row.findViewById(R.id.beerNameList);

            row.setTag(holder);
        }
        else
        {
            holder = (beerHolder)row.getTag();
        }

        BeerData beer = data[position];
        holder.txtTitle.setText(beer.beerName);
        holder.txtBrewery.setText(beer.beerBreweryName);

        return row;
    }

    static class beerHolder
    {
        TextView txtBrewery;
        TextView txtTitle;
    }
}

我尝试拨打适配器时遇到的错误是:

The constructor BeerSearchAdapter(Search, int, List<BeerData>) is undefined

我正在调用适配器:

//update listview
BeerSearchAdapter adapter1 = new BeerSearchAdapter(Search.this ,R.layout.listview_item_row, beerList);
lv.setAdapter(adapter1);

我的完整Search.java是:

package com.example.beerportfoliopro;

public class Search extends Activity implements OnQueryTextListener {

private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("logIN", "starting activity");
        setContentView(R.layout.activity_search);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setOnQueryTextListener(this);
        return true;
    }

    //get entered search and pop up a toast to show query entered
    public boolean onQueryTextSubmit (String query) {

        //toast query 
        Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();

        //make json variables to fill

        // url to make request
        String url = "http://api.brewerydb.com/v2/search?key=myKey&format=json&type=beer&withBreweries=y&q=";



        try {
            query = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        String jsonUrl = url + query;



        //todo: get json 
        new ReadJSONResult().execute(jsonUrl);

        return false;
    }

    private class ReadJSONResult extends AsyncTask
    <String, Void, String> {
        protected String doInBackground(String... urls) {
            return readJSONFeed(urls[0]);
        }

        protected void onPostExecute(String result) {
            try {

                //start progress bar
                //requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
                //setProgressBarIndeterminateVisibility(true);


                ///get 


                JSONObject json = new JSONObject(result);

                //acces listview
                lv = (ListView) findViewById(android.R.id.list);

                //make array list for beer
                final List<BeerData> beerList = new ArrayList<BeerData>();



                //get json items
                for(int i = 0; i < json.getJSONArray("data").length(); i++) {

                    String beerId = GetBeerDataFromJSON(i,"id", json);
                    String beerName = GetBeerDataFromJSON(i,"name", json);
                    String beerDescription = GetBeerDataFromJSON(i,"description" , json);
                    String beerAbv = GetBeerDataFromJSON(i,"abv" , json);
                    String beerIbu = GetBeerDataFromJSON(i,"ibu" , json);
                    String beerIcon = GetBeerIconsFromJSON(i, "icon",json );
                    String beerMediumIcon = GetBeerIconsFromJSON(i, "medium",json );
                    String beerLargeIcon = GetBeerIconsFromJSON(i, "large",json );
                    String beerGlass = GetBeerGlassFromJSON(i, json );
                    String beerStyle = GetBeerStyleFromJSON(i,"name", json );
                    String beerStyleDescription = GetBeerStyleFromJSON(i,"description", json );
                    String beerBreweryId = GetBeerBreweryInfoFromJSON(i, "id", json );
                    String beerBreweryName = GetBeerBreweryInfoFromJSON(i, "name", json );
                    String beerBreweryDescription = GetBeerBreweryInfoFromJSON(i, "description", json );
                    String beerBreweryWebsite = GetBeerBreweryInfoFromJSON(i, "website", json );

                    //get long and latt
                    String beerBreweryLat = GetBeerBreweryLocationJSON(i, "longitude", json );
                    String beerBreweryLong = GetBeerBreweryLocationJSON(i, "latitude", json );

                    String beerBreweryYear = GetBeerBreweryInfoFromJSON(i, "established", json );
                    String beerBreweryIcon = GetBeerBreweryIconsFromJSON(i,"large",json);



                    //create beer object
                    BeerData thisBeer = new BeerData(beerName, beerId, beerDescription, beerAbv, beerIbu, beerIcon,
                            beerMediumIcon,beerLargeIcon, beerGlass, beerStyle, beerStyleDescription, beerBreweryId, beerBreweryName,
                            beerBreweryDescription, beerBreweryYear, beerBreweryWebsite,beerBreweryIcon, beerBreweryLat, beerBreweryLong);

                    //add beer to list
                    beerList.add(thisBeer);


                }

                //update listview
                BeerSearchAdapter adapter1 = new BeerSearchAdapter(Search.this ,R.layout.listview_item_row, beerList);
                lv.setAdapter(adapter1);


                //set up clicks
                lv.setOnItemClickListener(new OnItemClickListener() {    
                       @Override
                       public void onItemClick(AdapterView<?> arg0, View arg1,
                                                int arg2, long arg3) {
                              Intent i = new Intent(Search.this, BeerPage.class); 

                              BeerData beerInfo = beerList.get(arg2);


                              //Toast.makeText(MainActivity.this,  beerInfo.beerName, Toast.LENGTH_SHORT).show();

                              i.putExtra("myBeerObject",  beerInfo);   
                              i.setClass(Search.this, BeerPage.class);


                              startActivity(i); 



                       } 
                   });

                //end progress bar
                //setProgressBarIndeterminateVisibility(false);






            } catch (Exception e) {
                Log.d("ReadBeerDataTask", e.getLocalizedMessage());
            }          
        }
    }

    private String GetBeerDataFromJSON(int position, String whatToGet, JSONObject json ) {
        String whatIsTheKeyYouAreLookFor = whatToGet;
        int whereInTheJSONArrayForLoopIsTheData = position;
        String holder = "";
        try{
            holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getString(whatIsTheKeyYouAreLookFor);

        } catch (JSONException e) {
            holder = "N/A";
        }



        return holder;
    }


    //get icons
        private String GetBeerBreweryIconsFromJSON(int position, String whatToGet, JSONObject json ) {
            String whatIsTheKeyYouAreLookFor = whatToGet;
            int whereInTheJSONArrayForLoopIsTheData = position;
            String holder = "";
            try{
                holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONArray("breweries").getJSONObject(0).getJSONObject("images").getString(whatIsTheKeyYouAreLookFor);;
            } catch (JSONException e) {
                holder = "N/A";
            }



            return holder;
        }

    //get icons
    private String GetBeerIconsFromJSON(int position, String whatToGet, JSONObject json ) {
        String whatIsTheKeyYouAreLookFor = whatToGet;
        int whereInTheJSONArrayForLoopIsTheData = position;
        String holder = "";
        try{
            holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONObject("labels").getString(whatIsTheKeyYouAreLookFor);

        } catch (JSONException e) {
            holder = "N/A";
        }



        return holder;
    }

    //get style information
        private String GetBeerStyleFromJSON(int position, String whatToGet, JSONObject json ) {
            String whatIsTheKeyYouAreLookFor = whatToGet;
            int whereInTheJSONArrayForLoopIsTheData = position;
            String holder = "";
            try{
                holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONObject("style").getString(whatIsTheKeyYouAreLookFor);

            } catch (JSONException e) {
                holder = "N/A";
            }



            return holder;
        }

        //get location data
        private String GetBeerBreweryLocationJSON(int position, String whatToGet, JSONObject json ) {
            String whatIsTheKeyYouAreLookFor = whatToGet;
            int whereInTheJSONArrayForLoopIsTheData = position;
            String holder = "";
            try{
                holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONArray("breweries").getJSONObject(0).getJSONArray("locations").getJSONObject(0).getString(whatIsTheKeyYouAreLookFor);

            } catch (JSONException e) {
                holder = "N/A";
            }



            return holder;
        }

    //get brewery information
        //get style information
                private String GetBeerBreweryInfoFromJSON(int position, String whatToGet, JSONObject json ) {
                    String whatIsTheKeyYouAreLookFor = whatToGet;
                    int whereInTheJSONArrayForLoopIsTheData = position;
                    String holder = "";
                    try{
                        holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONArray("breweries").getJSONObject(0).getString(whatIsTheKeyYouAreLookFor);

                    } catch (JSONException e) {
                        holder = "N/A";
                    }



                    return holder;
                }

    //get glass
    private String GetBeerGlassFromJSON(int position, JSONObject json ) {

        int whereInTheJSONArrayForLoopIsTheData = position;
        String holder = "";
        try{
            holder = json.getJSONArray("data").getJSONObject(whereInTheJSONArrayForLoopIsTheData).getJSONObject("glass").getString("name");

        } catch (JSONException e) {
            holder = "N/A";
        }



        return holder;
    }


    //gets the json from the inputed url
    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }        
        return stringBuilder.toString();
    }


    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }




}

3 个答案:

答案 0 :(得分:3)

根据错误,beerList显然是List<BeerData>。您的构造函数不会使用List<BeerData>。它需要BeerData[]。转换适配器的构造函数,改为使用List<BeerData>

答案 1 :(得分:0)

您可以改用

BeerSearchAdapter adapter1 = new BeerSearchAdapter(Search.this 
,R.layout.listview_item_row, (BeerData[])beerList.toArray()); 
lv.setAdapter(adapter1);

答案 2 :(得分:0)

这将是未定义的,因为您将构造函数声明为此

BeerSearchAdapter(Context context, int layoutResourceId, BeerData[] data)
相关问题