Button侦听器和SimpleCursor适配器

时间:2017-01-02 19:21:54

标签: android listview listener textview simplecursoradapter

我有一个Fragment类,可以在ListView中显示我数据库中的所有内容,并且我有一个扩展SimpleCursorAdapter的内部类,以使用ListView中的按钮

因此,在列表视图的每个元素中,我有几个TextView和2个按钮。使用我的以下代码,我可以使用按钮监听器来查看ListView内的按钮。但是在我的监听器中,我希望将TextView的内容放在与我单击的按钮相同的元素内。但是,当我点击按钮显示我的TextView时,它会显示我ListView的最后一个元素,我不知道为什么!

例如,如果我的ListView中有3个元素,就像这样:

_id = 1, name = "bob" 
_id = 2, name = "john"
_id = 3, name = "bobby"

这些元素中的每一个都显示一个按钮以显示ID,但如果我点击bob内的按钮,我会得到“id = 3”。约翰和鲍比也一样。如果我有一个新元素,我会得到他的ID

我的侦听器位于内部类MySimpleCursorAdapter的绑定函数中。

这是我的Fragment班级:

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;

}
}

这是我TextView的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeList"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:text="@string/difficult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView7"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <TextView
        android:text="@string/r_ponse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/textView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/idList"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/themeList"
        android:layout_alignStart="@+id/themeList"
        tools:ignore="HardcodedText" />

    <TextView
        android:text="Question :"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView6"
        android:layout_marginTop="14dp"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        tools:ignore="HardcodedText" />

    <TextView
        android:text="@string/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView4"
        android:layout_alignEnd="@+id/textView4" />

    <TextView
        android:text="Thème :"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignRight="@+id/textView6"
        android:layout_alignEnd="@+id/textView6"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView6"
        android:id="@+id/questionList"
        android:layout_toRightOf="@+id/themeList"
        android:layout_toEndOf="@+id/themeList" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView8"
        android:layout_alignLeft="@+id/questionList"
        android:layout_alignStart="@+id/questionList"
        android:id="@+id/reponseList" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView8"
        android:layout_alignRight="@+id/reponseList"
        android:layout_alignEnd="@+id/reponseList"
        android:id="@+id/difficultList"
        android:layout_toEndOf="@+id/reponseList"
        android:layout_toRightOf="@+id/reponseList" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/themeList"
        android:layout_marginLeft="33dp"
        android:layout_marginStart="33dp"
        tools:ignore="HardcodedText"
        android:layout_below="@+id/idList"
        android:layout_toRightOf="@+id/textView8"
        android:layout_toEndOf="@+id/textView8" />

    <Button
        android:text="@string/supprimerb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/buttonDelete"/>

    <Button
        android:text="@string/modifierB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonModifier"
        android:layout_alignTop="@+id/questionList"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignLeft="@+id/buttonDelete"
        android:layout_alignStart="@+id/buttonDelete" />
</RelativeLayout>

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

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

使用findViewById方法的vonClick参数获取点击的行项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());
  ...
}