Ich habe eine ListView, die ich angepasst habe, um Informationen aus einer SQLite-Datenbank gesammelt anzuzeigen. Jedes Element wird mit einem CardView-Widget dargestellt, das TextViews enthält.Wie bekomme ich Informationen von einem benutzerdefinierten ListView in Android
android:choiceMode="singleChoice"
ermöglicht dem Benutzer eine Auswahl aus der Liste zu machen: Ich habe die Listview-Eigenschaft in der XML-Datei zu setzen.
Wenn der Benutzer auf die unverankerte Aktionsschaltfläche klickt, möchte ich Informationen in einem der TextViews der CardView ausgewählt in der ListView angezeigt werden können.
Ich habe andere Fragen zu diesem Thema angeschaut und sie schlagen vor, die onClick-Methode zu verwenden, aber ich verstehe nicht, wie das funktionieren würde, weil ich die Informationen nicht abrufen möchte, wenn der Benutzer die Auswahl aus der Liste trifft. Eher möchte ich die Informationen, wenn sie die schwebende Aktionsschaltfläche ausgewählt haben.
Hier ist ein Teil meiner Tätigkeit Code:
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);
}
}
und hier ist mein xml für jede Listview-Element:
<?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>
Setzen Sie also onClick() im Adapter, um die Informationen in einer Variablen zu speichern. Dann schreibe einen Getter in den Adapter, um darauf zuzugreifen? Ich gehe davon aus, dass ich auf den Adapter zugreifen kann, indem ich findViewById für die ListView und die getAdapter-Methode für den Zugriff auf den Adapter verwende. Es scheint ein bisschen umständlich ... – incapacitated
Sie können einen Listener zur Listenansicht hinzufügen, es gibt einen setOnItemClickListener für listview und dort können Sie die Position abrufen und die Daten extrahieren. – Raz
Sie können auch einen Listener an den Adapter übergeben, der jedes Mal aufgerufen wird, wenn ein neues Element ausgewählt wird. – Raz