J'ai un ListView que j'ai personnalisé pour afficher des informations collectées à partir d'une base de données SQLite. Chaque élément est représenté à l'aide d'un widget CardView contenant TextViews. J'ai mis la propriété ListView dans le fichier .xml à:Comment obtenir des informations à partir d'un ListView personnalisé dans Android
android:choiceMode="singleChoice"
permettant à l'utilisateur de faire une sélection dans la liste.
Lorsque l'utilisateur clique sur le bouton d'action flottante, je souhaite pouvoir prendre les informations affichées dans l'une des vues de texte de CardView sélectionnées dans ListView. J'ai examiné d'autres questions sur le sujet et suggère d'utiliser la méthode onClick, mais je ne comprends pas comment cela fonctionnerait parce que je ne veux pas récupérer les informations lorsque l'utilisateur fait la sélection dans la liste. Au contraire, je veux l'information quand ils ont choisi le bouton d'action flottante.
Voici une partie de mon code d'activité:
public class SetViewerActivity extends AppCompatActivity {
private static final String TAG = "SetViewerActivity";
private boolean fabState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set contents of activity
setContentView(R.layout.activity_set_viewer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// set action for main floating action button
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fabState = false;
fab.setOnClickListener(new View.OnClickListener() {
// get set info and edit in new Activity
@Override
public void onClick(View view) {
editSelectedSet(view);
}
});
// set up list view and populate with custom set info from database
SQLiteDatabase db = new MemCardSQLiteHelper(this).getReadableDatabase();
Cursor cursor = db.rawQuery(MemCardDbContract.Sets.SELECT_ALL_SETS,
null);
ListView lvSets = (ListView) findViewById(R.id.listViewSets);
SetViewAdapter adapter = new SetViewAdapter(this, cursor);
lvSets.setAdapter(adapter);
}
private void editSelectedSet(View view) {
// HOW DO I FIND THE SET INFORMATION HERE ??
// start activity with selected set information for editing
Intent intent = new Intent(this, CreateNewSet.class);
intent.putExtra(MainActivity.CREATE_SET_NUM, selectedSet);
startActivity(intent);
}
}
et voici mon xml pour chaque élément ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:contentPadding="2dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewName" />
<TextView
android:id="@+id/textViewColour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toBottomOf="@+id/textViewAmount" />
<TextView
android:id="@+id/textViewSetId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Définissez donc le onClick() dans l'adaptateur pour enregistrer les informations dans une variable. Puis écrire un getter dans l'adaptateur pour y accéder? Je suppose que je peux accéder à l'adaptateur en utilisant findViewById pour ListView et la méthode getAdapter pour accéder à l'adaptateur. Cela peut sembler un peu détourné ... – incapacitated
Vous pouvez ajouter un écouteur à la liste, il y a un setOnItemClickListener pour listview et là vous pouvez obtenir la position et extraire les données. – Raz
Vous pouvez également passer un écouteur à l'adaptateur qui est appelé chaque fois qu'un nouvel élément est sélectionné. – Raz