4

現在Androidのアプリで行っているカスタムビューに問題があります。インフレーターに関連する質問がたくさんあることは知っていますが、この問題を回避することはできません。

インフレータは問題なく動作していますが、ループを3回実行する必要があり、1回しか実行していないため、最終的なレイアウトで1つのビューしか取得できません。

コードの関連部分はこれです

 void populate(String strcline, String url){
lLfD = (LinearLayout)findViewById(R.id.lLfD);

    try{

    JSONArray a1 = new JSONArray(strcline);

    for(int i = 0; i < a1.length(); i++){

        JSONArray a2 =  a1.getJSONArray(i);

        final String fUserId = a2.getString(0);
        String userName = a2.getString(1);
        String userPicture = url + a2.getString(2);


        View child = getLayoutInflater().inflate(R.layout.cellevery, lLfD);
        ImageView avatar = (ImageView)findViewById(R.id.cellAvatar);
        downloadFile(userPicture, avatar);
        TextView cellName = (TextView)findViewById(R.id.cellName);
        cellName.setText(userName);


        lLfD.addView(child);

    }
    }catch(Exception e){

    }
    pDialog.dismiss();

}

4

1 に答える 1

3

膨張したビューでのみfindViewByIdを実行する必要があるように見えます。そうしないと、ループ内の最初のビューのみである最初のビューが検索されます。

   View child = getLayoutInflater().inflate(R.layout.cellevery, lLfD);
    ImageView avatar = (ImageView)child.findViewById(R.id.cellAvatar);
    downloadFile(userPicture, avatar);
    TextView cellName = (TextView)child.findViewById(R.id.cellName);
    cellName.setText(userName);

ループ内のfindViewByIdの説明は次のとおりです。

Loop 1:
1LfD->child1->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds this one)

Loop 2:

1Lfd->
   child1->R.id.cellAvatar
   child2->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds the child1.cellAvatar again)

Loop 3:
1LfD->
   child1->R.id.cellAvatar 
   child2->R.id.cellAvatar 
   child3->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds the child1.cellAvatar again)

を使用child.findViewById(R.id.cellAvatar)することにより、ループの実行ごとに正しいR.id.cellAvatarを見つけることができます。

それは理にかなっていますか?

アップデート2:

電話をかけるとき:

getLayoutInflater().inflate(R.layout.cellevery, lLfD);

すでに親ビューを2番目の引数として設定しているため、次を呼び出す必要はありません。

lLfD.addView(child);
于 2012-12-21T02:31:46.427 に答える