0

我有一個ListView,我已經自定義顯示從SQLite數據庫收集的信息。每個項目都是使用包含TextViews的CardView小部件表示的。我在.xml文件設置ListView控件屬性:如何從Android中的自定義ListView獲取信息

android:choiceMode="singleChoice"

允許用戶從列表中進行選擇。

當用戶單擊浮動操作按鈕時,我希望能夠將信息顯示在ListView中選定CardView的TextView中的一個TextView中。

我已經看了看主題的其他問題,他們建議使用的onClick方法,但我不明白怎麼會工作,因爲我不想檢索信息時,用戶從列表中選擇。相反,當他們選擇浮動操作按鈕時,我需要這些信息。

這裏是我的活動代碼的一部分:

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> 

回答

0

您可以在每個項目上分配一個onClick當選擇一個項目(點擊),你可以將數據保存爲字段成員。然後閱讀你想要的數據。

+0

因此,在適配器中設置onClick()以將信息保存在變量中。然後在適配器上寫一個getter來訪問它?我假設我可以通過對ListView使用findViewById來訪問適配器,並使用getAdapter方法來訪問適配器。這似乎有點迂迴的方式... – incapacitated

+0

你可以添加一個監聽器到列表視圖有一個setOnItemClickListener列表視圖,在那裏你可以得到位置並提取數據。 – Raz

+0

也可以將偵聽器傳遞給每次選擇新項目時調用的適配器。 – Raz

相關問題