Button listener and SimpleCursor adapter

41 views Asked by At

I have a Fragment class that can display everything in my database in a ListView, and I have an inner class that extends SimpleCursorAdapter to use buttons within my ListView.

So in each element of my list view, I have several TextView's and 2 button. With my following code, I can use a button listener for a button inside my ListView. But inside my listener, I want to get the content of the TextView inside the same element that the button I click on. But When I click on a button to display my TextView, it display the last element of my ListView and I don't know why !

For example if I have 3 elements in my ListView like this :

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

Each of these element are displayed with a button to display there ID, but if I click on the button inside bob, I get "id = 3". Same for john and bobby. And if I had a new element, I get his ID

My listener is in the bind function in my inner class MySimpleCursorAdapter.

Here is my Fragment Class :

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;

}
}

And here is my layout for my 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>

Thank you for help !

1

There are 1 answers

1
ρяσѕρєя K On BEST ANSWER

Probably vh.idList is containing last item view which is provided by last call of newView .

Get clicked row item id using findViewById and v parameter of onClick method:

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