2016-12-26 7 views
1

bleow投稿コードでは、私はビューを膨らませて、linearlayoutに追加しています。クリックされた各ビューは、関連する特定のドキュメントを開く必要があります。私がこれを達成するために考えているのは、各膨張したビューをキーとしてマップに追加し、マップの の値をドキュメントにすることです。オブジェクトに基づいてマップから値を取得する方法

私の質問は、各ビューをキーとして追加する方法と、キーが与えられたマップから特定の値を再取得する方法です。私はいくつかの記事を参照しかし、私は私のキーは、オブジェクトビュー

コード、特に 指定されたキーに基づいてマップから値を取得する方法がわからない:

まず
private void inflateView(String bez, String ges) { 
    LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container); 

    View viewDivider = new View(this); 
    viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    viewDivider.setBackgroundColor(Color.parseColor("#000000")); 

    LayoutInflater inflator = this.getLayoutInflater(); 
    View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key 

    ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo); 
    TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung); 
    TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs); 
    TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft); 
    Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more); 


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme())); 
    } else { 
     imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc)); 
    } 

    texVieBez.setText(bez); 
    texVieGes.setText(bez); 
    btnMore.setVisibility(View.VISIBLE); 

    linLay.addView(view); 

    linLay.addView(viewDivider); 
} 

答えて

0

地図のキーとしてViewへの参照を使用することはお勧めできません。これは、漏れた参照とメモリの問題を引き起こす可能性があります。代わりに、setTag(Object obj)メソッド経由でビューにタグを割り当てます。後で、クリックリスナーでmap.get(key)を呼び出すドキュメントを取得します。ここでkeyは、割り当てたタグです。 inflateView内部

String tag = "Something unique for a key" // can be any Object not necessarily a String 
view.setTag(tag); 
map.put(tag, referenceToDocument); 
view.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Object document = map.get(view.getTag()); 
      // do whatever you like with the document 
     } 
}); 
+0

はそのためのコードを入力してくださいでしょう? – user2121

+0

私のasnwerを更新しました –

関連する問題