2017-10-31 6 views
-4

私は、他のアクティビティを通じて項目を追加したリストビューを持っています。ここでの私の目標は、それがアイテムに固有のクリックダイアログボックスを起動し、特定のListView項目がをクリックされたとき、それは、どこにすることです。ここで特定のListViewアイテムをクリックしてダイアログボックスを作成する方法は?

は、リストビュー

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

     <ListView 
      android:id="@+id/inventoryListView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:dividerHeight="1dp" 
      android:headerDividersEnabled="true" 
      android:onClick="f"> 

     </ListView> 
    </LinearLayout> 

</LinearLayout>] 

そして、ここでは、アダプタ

public class inventory extends AppCompatActivity { 

    public static ArrayList<String> items; 
    public static ArrayAdapter<String> adapter; 
    public static ListView inventoryListView; 

    SharedPreferences sharedPreferences; 

    protected void onCreate(Bundle savedInstanceSate){ 
     super.onCreate(savedInstanceSate); 
     setContentView(R.layout.inventory); 

     inventoryListView = (ListView)findViewById(R.id.inventoryListView); 

     items = new ArrayList<String>(); 
     adapter = new ArrayAdapter<String>(this,R.layout.inventory_list_view,items); 
     inventoryListView.setAdapter(adapter); 
     sharedPreferences = getSharedPreferences("my_prefs", 0); 
     updateInventory(); 
     adapter.notifyDataSetChanged(); 


    } 

リストとの活動が保持している1、それが唯一の他の活動を通じて取り込むことができるので、人口ではなく、ではないのですリストビュー。

答えて

0

使用viewholderパターン

Example

そして、あなたはあなたのビューにonclicklistener設定し、それぞれの行のためのあなたのダイアログを起動することができ、あなたのgetView方法インチ

0

まず第一に、あなたがRecyclerViewの代わりに、リストビューを使用することができ、それはメモリ管理とパフォーマンスで、より効率的です。 @ghostが言ったように

、あなたはViewHolderパターンを使用して、ビュー・ホルダーとアダプタにあなたの活動のコンテキストをプッシュすることができます。クリックロジックをviewHolder(アダプタの内部)に置くだけでよいのです。

+0

例や擬似コードを取得できますか? –

関連する問題