使用Array适配器在listView中获取数据库ID

时间:2016-07-12 15:59:42

标签: java android listview android-arrayadapter

我只是想知道如何使用setOnItemClickListener从listView获取数据库ID(配方ID)?

这是我的listRecipeAdapter扩展ArrayAdapater ...

public class ListRecipeAdapter extends ArrayAdapter {

List list = new ArrayList();

public ListRecipeAdapter(Context context, int resource) {
    super(context, resource);
}
static class LayoutHandler {
    TextView RECIPENAME, RECIPECONTRIBUTOR, RECIPEPREPTIME, RECIPECOOKTIME;
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutHandler layoutHandler;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.recipe_view_layout, parent, false);
        layoutHandler = new LayoutHandler();
        layoutHandler.RECIPENAME = (TextView)row.findViewById(R.id.recipe_name);
        layoutHandler.RECIPEPREPTIME = (TextView) row.findViewById(R.id.recipe_preptime);
        layoutHandler.RECIPECOOKTIME = (TextView) row.findViewById(R.id.recipe_cooktime);
        layoutHandler.RECIPECONTRIBUTOR = (TextView) row.findViewById(R.id.recipe_contributor);
        row.setTag(layoutHandler);
    } else {
        layoutHandler = (LayoutHandler) row.getTag();
    }
    Recipe recipe = (Recipe) this.getItem(position);
    layoutHandler.RECIPENAME.setText(recipe.getRecipeName());
    layoutHandler.RECIPEPREPTIME.setText(recipe.getRecipePreparationTime());
    layoutHandler.RECIPECOOKTIME.setText(recipe.getRecipeCookTime());
    layoutHandler.RECIPECONTRIBUTOR.setText(recipe.getRecipeContributor());
    return row;
}}

这是我的观看活动...

listRecipeAdapter = new ListRecipeAdapter(getApplicationContext(),  R.layout.recipe_view_layout);
    listView.setAdapter(listRecipeAdapter);
grabRecipeSQLOperations = new      GrabRecipeSQLOperations(getApplicationContext());

sqLiteDatabase = grabRecipeSQLOperations.getReadableDatabase();

    String recipeName, recipeContributor, recipePrepTime, recipeCookTime;
    int recipeID;
    cursor = grabRecipeSQLOperations.getRecipeRecords(sqLiteDatabase, storeTag);
    if (cursor.moveToFirst()) {
        do {

            recipeID = cursor.getInt(0);
            recipeName = cursor.getString(1);
            recipeContributor = cursor.getString(2);
            recipeCookTime = cursor.getString(3);
            recipePrepTime = cursor.getString(4);
            Recipe recipe = new Recipe(recipeID,recipeName, recipePrepTime, recipeCookTime, recipeContributor);
            recipe.recipeView(recipeID,recipeName, recipePrepTime, recipeCookTime, recipeContributor);
            listRecipeAdapter.add(recipe);
           //how to see IDs
            String list = Integer.toString(recipe.getRecipeId());
            Log.e("IDS:", list);
        } while (cursor.moveToNext());
    }

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("Clicked item id", String.valueOf(id));
        }
    });

单击侦听器后,它将仅显示数据库ID但仅显示 ListView的位置记录在Logcat中。

0 个答案:

没有答案