1

大家。我对这段代码的这段摘录有疑问:

cocktailListView.setAdapter(adapter);

鸡尾酒列表视图无法解析

我的整个代码是:

public class SearchCustomListViewActivity extends ListActivity {
    //ArrayList thats going to hold the search results
    ArrayList<HashMap<String, Object>> searchResults;

    //ArrayList that will hold the original Data
    ArrayList<HashMap<String, Object>> originalValues;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        final EditText searchBox=(EditText) findViewById(R.id.searchBox);
        ListView playerListView=(ListView) findViewById(android.R.id.list);

        //get the LayoutInflater for inflating the customomView
        //this will be used in the custom adapter
        inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //these arrays are just the data that
        //I'll be using to populate the ArrayList
        //You can use our own methods to get the data
        String names[]={"Ronaldo","Messi","Torres","Iniesta",
                "Drogba","Gerrard","Rooney","Xavi"};

        String teams[]={"Real Madrid","Barcelona","Chelsea",
                "Barcelona","Chelsea","Liverpool",
                "ManU","Barcelona"};
        Integer[] photos={R.drawable.amazonas,R.drawable.amazonas,
                R.drawable.amazonas,R.drawable.amazonas,
                R.drawable.amazonas,R.drawable.amazonas,
                R.drawable.amazonas,R.drawable.amazonas};

        originalValues=new ArrayList<HashMap<String,Object>>();

        //temporary HashMap for populating the
        //Items in the ListView
        HashMap<String , Object> temp;

        //total number of rows in the ListView
        int noOfPlayers=names.length;

        //now populate the ArrayList players
        for(int i=0;i<noOfPlayers;i++)
        {
            temp=new HashMap<String, Object>();

            temp.put("name", names[i]);
            temp.put("team", teams[i]);   
            temp.put("photo", photos[i]);

            //add the row to the ArrayList
            originalValues.add(temp);       
        }
        //searchResults=OriginalValues initially
        searchResults=new ArrayList<HashMap<String,Object>>(originalValues);

        //create the adapter
        //first param-the context
        //second param-the id of the layout file
        //you will be using to fill a row
        //third param-the set of values that
        //will populate the ListView
        final CustomAdapter adapter=new CustomAdapter(this, R.layout.players_layout,searchResults);

        //finally,set the adapter to the default ListView
        cocktailListView.setAdapter(adapter);
        searchBox.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //get the text in the EditText
                String searchString=searchBox.getText().toString();
                int textLength=searchString.length();
                searchResults.clear();

                for(int i=0;i<originalValues.size();i++)
                {
                    String playerName=originalValues.get(i).get("name").toString();
                    if(textLength<=playerName.length()){
                        //compare the String in EditText with Names in the ArrayList
                        if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
                            searchResults.add(originalValues.get(i));
                    }
                }

                adapter.notifyDataSetChanged();
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            public void afterTextChanged(Editable s) {}
        });
    }

    //define your custom adapter
    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
    {
        public CustomAdapter(Context context, int textViewResourceId,
                ArrayList<HashMap<String, Object>> Strings) {
            //let android do the initializing :)
            super(context, textViewResourceId, Strings);
        }

        //class for caching the views in a row 
        private class ViewHolder
        {
            ImageView photo;
            TextView name,team;
        }
        ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null)
            {
                convertView=inflater.inflate(R.layout.players_layout, null);
                viewHolder=new ViewHolder();

                //cache the views
                viewHolder.photo=(ImageView) convertView.findViewById(R.id.photo);
                viewHolder.name=(TextView) convertView.findViewById(R.id.name);
                viewHolder.team=(TextView) convertView.findViewById(R.id.team);

                //link the cached views to the convertview
                convertView.setTag(viewHolder);
            }
            else
                viewHolder=(ViewHolder) convertView.getTag();

            int photoId=(Integer) searchResults.get(position).get("photo");

            //set the data to be displayed
            viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
            viewHolder.name.setText(searchResults.get(position).get("name").toString());
            viewHolder.team.setText(searchResults.get(position).get("team").toString());

            //return the view to be displayed
            return convertView;
        }
    }
}
4

2 回答 2

1

你还没有实例化cocktailListView

您需要在您的onCreate(...). 您可以在实例化playerListView.

来自埃斯皮安德夫

ListView cocktailListView = getListView();

或者

ListView cocktailListView = 
    (ListView) this.findViewById(/* id of your list view*/);
于 2012-08-02T16:58:33.857 回答
0

当您ListActivity在布局中使用 ListView 时,应该有一个精确的 id:

  android:id="@id/android:list"

只有在这种情况下,您才能真正使用getListView而无需获得 npe。

从文档:

ListActivity 有一个默认布局,它由屏幕中央的单个全屏列表组成。但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个 ID 为“@android:id/list”的 ListView 对象(如果它在代码中,则为列表)

于 2012-08-02T17:53:36.863 回答