我可以不使用基本适配器的数组(只是普通的旧数组而不是数组列表)用于列表视图吗?

时间:2012-11-19 17:38:14

标签: android arrays android-layout listview baseadapter

我想为listview使用基本适配器,但我的数据在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
        }

}

1 个答案:

答案 0 :(得分:1)

取消注释第lv.setAdapter(asl);

您需要设置适配器。