Android 按钮侦听器和SimpleCursor适配器

Android 按钮侦听器和SimpleCursor适配器,android,listview,listener,textview,simplecursoradapter,Android,Listview,Listener,Textview,Simplecursoradapter,我有一个片段类,它可以在列表视图中显示数据库中的所有内容,我还有一个内部类,它扩展了SimpleCursorAdapter以使用我的列表视图中的按钮 因此,在列表视图的每个元素中,我都有几个文本视图和两个按钮。使用下面的代码,我可以在我的列表视图中为按钮使用按钮侦听器。但是在我的监听器中,我想在我单击按钮的同一元素中获取TextView的内容。但是当我点击一个按钮来显示我的文本视图时,它会显示我的列表视图的最后一个元素,我不知道为什么 例如,如果我的列表视图中有3个元素,如下所示: _id =

我有一个
片段
类,它可以在
列表视图
中显示数据库中的所有内容,我还有一个内部类,它扩展了
SimpleCursorAdapter
以使用我的
列表视图
中的按钮

因此,在列表视图的每个元素中,我都有几个
文本视图和两个按钮。使用下面的代码,我可以在我的
列表视图中为按钮使用按钮侦听器。但是在我的监听器中,我想在我单击按钮的同一元素中获取
TextView
的内容。但是当我点击一个按钮来显示我的
文本视图时,它会显示我的
列表视图的最后一个元素,我不知道为什么

例如,如果我的
列表视图中有3个元素,如下所示:

_id = 1, name = "bob" 
_id = 2, name = "john"
_id = 3, name = "bobby"
这些元素中的每一个都显示有一个按钮来显示它们的ID,但是如果我单击bob中的按钮,就会得到“ID=3”。约翰和鲍比也一样。如果我有一个新元素,我会得到他的ID

我的侦听器在我的内部类
MySimpleCursorAdapter
中的绑定函数中

这是我的
片段
类:

public class ViewCardEditor extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String authority = "com.example.jean.cartememoire.CardContentProvider";
public String[] from;
public final int[] to = {R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList};
StockCard stock;
ViewGroup container;
ListView listView;
MySimpleCursorAdapter adapter;
private ArrayList<String> data = new ArrayList<String>();



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
                         Bundle savedInstanceState) {
    container = c;
    View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);
    stock = new StockCard(c.getContext());
    from = new String[]{stock._ID,
            stock.THEME,
            stock.QUESTION,
            stock.REPONSE,
            stock.DIFFICULTE};

    // Inflate the layout for this fragment
    if (container != null) {
        container.removeAllViews();
    }

    //listView.setAdapter(new MyListAdapter(container.getContext(), R.layout.card_stock, data));


    //View view_cs = LayoutInflater.from(container.getContext()).inflate(R.layout.card_stock, null);

    //Supprimer = (Button) view_cs.findViewById(R.id.buttonDelete);
    //Modifier = (Button) view_cs.findViewById(R.id.buttonModifier);


    databaseView(view);

    return view;
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Uri.Builder builder = new Uri.Builder();
    Uri uri = builder.scheme("content").authority(authority)
            .appendPath(stock.STOCK_TABLE).build();
    return new CursorLoader(container.getContext(), uri, from,
            null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);
}



public void databaseView(View view)
{
    ArrayList<String> list;
    Cursor cursor = stock.getData();

    listView = (ListView) view.findViewById(R.id.listView);

    adapter = new MySimpleCursorAdapter(container.getContext(), R.layout.card_stock, null, from, to,0);
    listView.setAdapter(adapter);

    LoaderManager manager = getLoaderManager();
    manager.initLoader(0, null, this);

}


public void deleteOneCard(int id)
{
    Uri.Builder builder = new Uri.Builder();
    builder.scheme("content").authority(authority).appendPath(stock.STOCK_TABLE);

    ContentUris.appendId(builder, id);
    Uri uri = builder.build();
    ContentResolver resolver = container.getContext().getContentResolver();
    resolver.delete(uri, null, null);

}

private class MySimpleCursorAdapter extends SimpleCursorAdapter
{
    ViewHolder vh;
    public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);

    }

    public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.card_stock, parent, false);
        vh = new ViewHolder();
        vh.idList = (TextView) view.findViewById(R.id.idList);
        vh.themeList = (TextView) view.findViewById(R.id.themeList);
        vh.questionList = (TextView) view.findViewById(R.id.questionList);
        vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
        vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
        vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
        vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);


        view.setTag(vh);
        return view;
    }

    public void bindView(View view, Context Context, Cursor cursor) {
        vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
        vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
        vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
        vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
        vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));

        vh.Supprimer.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {
                int id = Integer.parseInt(vh.idList.getText().toString());
                Toast.makeText(container.getContext(), "bouton delete : "+id, Toast.LENGTH_LONG).show();
                //Here everytime I hit the button, the last ID i put on the listView is displayed, no matter what Supprimer button I click

            }
        });


    }
}



public class ViewHolder
{
    Button Supprimer, Modifier;
    TextView idList, themeList, questionList, reponseList, difficulteList;

}
}

可能
vh.idList
包含最后一个项目视图,该视图由上次调用
newView
提供

使用
onClick
方法的
findViewById
v
参数获取单击的行项目id:

public void onClick(View v) {
  View parentView = (View)v.getParent();
  TextView idList = parentView.findViewById(R.id.idList);
  int id = Integer.parseInt(idList.getText().toString());
  ...
}

我完全忘记了onClick中的参数视图,谢谢,它很有效!