-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 に答える