【问题标题】:How to get information from a customized ListView in Android如何从 Android 中的自定义 ListView 获取信息
【发布时间】:2018-01-28 12:03:51
【问题描述】:

我有一个自定义的 ListView,用于显示从 SQLite 数据库收集的信息。每个项目都使用包含 TextViews 的 CardView 小部件表示。我已将 .xml 文件中的 ListView 属性设置为:

android:choiceMode="singleChoice"

允许用户从列表中进行选择。

当用户单击浮动操作按钮时,我希望能够获取在 ListView 中选择的 CardView 的 TextView 之一中显示的信息。

我查看了有关该主题的其他问题,他们建议使用 onClick 方法,但我不明白它是如何工作的,因为我不想在用户从列表中进行选择时检索信息。相反,我想要他们选择浮动操作按钮时的信息。

这是我的 Activity 代码的一部分:

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);
}
}

这是我的每个 ListView 项目的 xml:

<?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>

【问题讨论】:

    标签: android android-layout listview android-cursoradapter floating-action-button


    【解决方案1】:

    您可以在每个项目上分配onclick以及选择项目(单击)时,可以将数据保存为现场成员。然后随时随地读取数据。

    【讨论】:

    • 因此在适配器中设置 onClick() 以将信息保存在变量中。那么在adapter中写一个getter来访问呢?我假设我可以通过使用 ListView 的 findViewById 和 getAdapter 方法来访问适配器来访问适配器。好像有点绕路……
    • 您可以在列表视图中添加一个监听器,列表视图有一个 setOnItemClickListener,您可以在那里获取位置并提取数据。
    • 您还可以将侦听器传递给适配器,每次选择新项目时都会调用它。
    猜你喜欢
    • 2014-12-07
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多