从ListAdapter / ListView获取对象

时间:2015-04-19 17:19:44

标签: android listview listadapter dialogfragment

好的,所以我正在尝试创建一个使用日历视图的应用程序来创建膳食日历并生成购物清单。我可以在我的个人生活中使用的东西。我遇到的是尝试从列表适配器中获取配方并将其添加到主活动中。任何有关如何做到这一点的帮助。我从各种教程中尝试了几种方法,但没有成功。这是我的代码:

MainActivity:

package com.cannon_inc.monthlymenu;

import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.Toast;


public class MainActivity extends Activity 
{
CalendarView calendarView = (CalendarView) findViewById(R.id.menuCalendar);
private RecipeListDialogFragment recipeDialog = new RecipeListDialogFragment();
private ArrayList<MenuItems> monthlyMenu;
private ShoppingList shoppingList;
private Date startDate = new Date();
@SuppressWarnings("deprecation")
private Date endDate = new Date(startDate.getDay() + 7);
private Date selectedDate = new Date(calendarView.getDate());   

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


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

    int screenOrientation = getResources().getConfiguration().orientation;

        // use landscape for extra large tablets; otherwise, use portrait
        if (screenOrientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            getMenuInflater().inflate(R.menu.monthly_menu_menu, menu);
        }
        else if(screenOrientation == Configuration.ORIENTATION_PORTRAIT)
        {
            getMenuInflater().inflate(R.menu.shopping_list_menu, menu);
            shoppingList = new ShoppingList(monthlyMenu, startDate, endDate);
        }
   return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // switch based on the MenuItem id
    switch (item.getItemId()) {
    case R.id.startDate:
        return true;
    case R.id.endDate:
        return true;
    case R.id.selectMeal:
        updateDate();
        recipeDialog.show(getFragmentManager(), "Recipe Dialog");
        monthlyMenu.add(new MenuItems(new Recipe(), selectedDate));
        return true;
    case R.id.clearMenu:
        monthlyMenu.clear();
        Toast.makeText(this, "Menu has been cleared!", Toast.LENGTH_SHORT).show();
        return true;
    } // end switch
    return true;
}

public void updateDate()
{
    selectedDate = new Date(calendarView.getDate());
}
}

的ListView / AlertDialog:

package com.cannon_inc.monthlymenu;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class RecipeListDialogFragment extends DialogFragment implements OnClickListener
{
    RecipeAdapter recipeAdapter = new RecipeAdapter();
    private Recipe selectedRecipe;


    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    final ListView recipeListView = (ListView) getActivity().getLayoutInflater().inflate(R.layout.fragment_recipe_name_list, null);
        recipeListView.setAdapter(recipeAdapter);
        recipeListView.setClickable(true);



        builder.setView(recipeListView);
        builder.setTitle("Recipe List");
        builder.setCancelable(true);

        return builder.create();
    }

    public Recipe getSelectedRecipe()
    {
        return selectedRecipe;
    }

}

ListAdapter:

package com.cannon_inc.monthlymenu;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class RecipeAdapter extends BaseAdapter
{
    private static ArrayList<Recipe> menuList = new ArrayList<Recipe>();

    public RecipeAdapter()
    {
        menuList.add(new Recipe());
    }

    @Override
    public int getCount() 
    {
        return menuList.size();
    }

    @Override
    public Object getItem(int index) 
    {
        return getItem(index);
    }

    @Override
    public long getItemId(int index) 
    {
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) 
    {
        if (view == null)
        {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.fragment_recipe_list, parent, false);
        }

        Recipe recipe = menuList.get(index);

        TextView recipeNameView = (TextView) view.findViewById(R.id.recipeName);
        recipeNameView.setText(recipe.getName());

        return view;
    }   
}

感谢您的帮助!!!!

1 个答案:

答案 0 :(得分:0)

更改此

@Override
public Object getItem(int index) 
{
    return getItem(index);
}

到这个

@Override
public Recipe getItem(int index) 
{
    return menuList.get(index);
}

并在您需要的地方致电Recipe demo = adapter.getItem(123);