-1

我想为列表视图使用基本适配器,但我的数据在 String[] 中,这是不允许的吗?似乎无法让项目在视图中膨胀……我的 XML 布局构建正确。下面发布了有关活动的 java。我没有得到任何例外,只是没有显示列表。

public class SuggestedRest extends Activity{
    MediaPlayer clickSound;
    Context context;
    private String[] restaurantSuggestions = {
            "Applebees",
            "Arby's",
            "Baskin Robbins",
            "Ben & Jerry's",
            "Bojangles'",
            "Bonefish Grill",
            "Buffalo Wild Wings",
            "Burger King",
            "Captain D's",
            "Carl's Jr.",
            "Carrabba's",
            "Checkers",
            "Cheeburger Cheeburger",
            "Cheesecake Factory",
            "Chick-Fil-A",
            "Chili's",
            "Church's",
            "Cold Stone Creamery",
            "Cracker Barrel",
            "Dairy Queen",
            "Dave & Buster's",
            "Denny's",
            "Domino's Pizza",
            "Dunkin' Donuts",
            "Five Guys",
            "Hardee's",
            "Hooters",
            "Huddle House",
            "IHOP",
            "Jack in the Box",
            "Jack's",
            "Jason's Deli",
            "Jersey Mike's Subs",
            "Jimmy John's",
            "KFC",
            "Krystal",
            "Little Caesars",
            "Logan's Roadhouse",
            "Lone Star",
            "Long John Silver's",
            "LongHorn Steakhouse",
            "Macaroni Grill",
            "McAlister's Deli",
            "McDonald's",
            "Moe's Southwest Grill",
            "Olive Garden",
            "Outback",
            "P. F. Chang's China Bistro",
            "Panda Express",
            "Panera Bread",
            "Papa John's",
            "Pizza Hut",
            "Popeyes Chicken & Biscuits",
            "Quiznos",
            "Rally's",
            "Red Lobster",
            "Red Robin",
            "Ruby Tuesday",
            "Ruth's Chris Steak House",
            "Sbarro",
            "Schlotzsky's",
            "Shoney's",
            "Smashburger",
            "Sonic",
            "Starbucks",
            "Steak 'n Shake",
            "Subway",
            "Taco Bell",
            "TGI Friday",
            "Waffle House",
            "Wendy's",
            "Whataburger",
            "White Castle",
            "Zaxby's"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.suggested_rest_layout);
        context = getApplicationContext();
        final AdapterSuggestionsList asl = new AdapterSuggestionsList(context, restaurantSuggestions);
        final ListView lv = (ListView) findViewById(R.id.list_view_suggested);
        //lv.setAdapter(asl);

    }


    public class AdapterSuggestionsList extends BaseAdapter {

        private final Context context;
        private final String[] sugArray;

        public AdapterSuggestionsList(Context context, String[] sugArray) {
                    super();
                    this.context = context;
                    this.sugArray = sugArray;
                }

                @Override
                public View getView(int position, View convertView , ViewGroup parent){
                    LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final ViewHolder holder;

                    if (convertView == null){  // if null then inflate view. if not null then get the view from the stored ViewHolder class object  ("holder")
                        // 
                    convertView = inflator.inflate(R.layout.suggested_row, parent, false);
                    holder = new ViewHolder();
                    // holder is used as a recycle bin of sorts. once we inflate the view we store it as a holder object and just update the values since inflating is expensive
                        holder.tv1 = (TextView) convertView.findViewById(R.id.suggested_name);
                        holder.btnAdd= (Button) convertView.findViewById(R.id.btn_add_sugg_to_fav);

                        convertView.setTag(holder);
                        }  
                    else{
                        holder = (ViewHolder) convertView.getTag();
                    }

                    // this is the restaurant name
                    holder.name_value = sugArray[position];
                    holder.tv1.setText(holder.name_value);

                    holder.btnAdd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            clickSound.start();
                            Intent intent  = new Intent(getApplicationContext(), AddMoreRestaurant.class);
                            intent.putExtra("TAG_THENAME", holder.name_value);  // this tag is opened by the AddMoreRestaurants activity when it is started by this intent
                            startActivity(intent);
                        }
                    });
                    return convertView;
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return sugArray.length;
                }
                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return position;
                }
                @Override
                public long getItemId(int position) {
                    // TODO Auto-generated method stub
                    return position;
                }
            }

        private static class ViewHolder {
            private TextView tv1;
            private Button btnAdd; // the button to add to the favorites
            private String name_value;  // set this to the name of the restaurant and pass it to the intent if needed
        }

}
4

1 回答 1

1

取消注释该行lv.setAdapter(asl);

您需要设置适配器。

于 2012-11-19T17:49:46.030 回答