2017-12-02 4 views
0

2つのテキストフィールドでカスタムリストビューを作成しました。名前と年齢を示します。 私は3つのオブジェクトのサンプル配列を与えました。しかし、たびにListViewのすべての位置の配列の最後の要素が表示されるだけです。ここでand List ListView最後の要素が繰り返し表示されています

は私の主な活動である:ここ

package com.sridatta.listview; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 



public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     details[] objects={new details("datta",19),new details("siva",20),new details("kd",18)}; 
    CustomArrayAdapter adapter=new CustomArrayAdapter(this,R.id.listtype,objects); 
    ListView lv=(ListView)findViewById(R.id.list_view); 
    lv.setAdapter(adapter); 

    } 
} 

は私のカスタムArrrayAdapterクラスである:ここ

public class CustomArrayAdapter extends ArrayAdapter<details> { 


public CustomArrayAdapter(@NonNull Context context, int resource, @NonNull details[] objects) { 
    super(context, resource, objects); 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    if(convertView==null){Context context=getContext(); 
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView=inflater.inflate(R.layout.list_type,null); 
     } 
    TextView tv1=(TextView)convertView.findViewById(R.id.name); 
    TextView tv2=(TextView)convertView.findViewById(R.id.age); 
    tv1.setText(getItem(position).name); 
    tv2.setText(Integer.toString(getItem(position).age)); 
    return convertView; 
} 
} 

詳細クラスは、すべてのヘルプは感謝するでしょう

public class details { 
static String name; 
static int age; 
public details(String name,int age){ 
    this.name=name; 
    this.age=age; 
} 
} 

です。

答えて

0

リサイクルビューを使用しています。アンドロイドが画面に表示されると思われるときに生成されます。たとえば、上にスクロールすると、最後の項目が画面から非表示になり、最後の項目が再表示されて表示されます。そして、レイアウトを初期化するときにのみテキストを設定します。あなたはこのようなコードを変更する必要があります。

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    if(convertView==null){Context context=getContext(); 
     LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView=inflater.inflate(R.layout.list_type,null); 
    } 
    //update the text when view show up 
    TextView tv1=(TextView)convertView.findViewById(R.id.name); 
    TextView tv2=(TextView)convertView.findViewById(R.id.age); 
    tv1.setText(getItem(position).name); 
    tv2.setText(Integer.toString(getItem(position).age)); 
    return convertView; 
} 

ちょうどあなたが別のオブジェクトとしてそれらをしたい、あなたのオブジェクトクラスに気づくが、静的には、同じメモリ位置にその値を満たしています。静的オブジェクトを一意のオブジェクトとして格納するには、その静的オブジェクトを削除する必要があります。

public class details { 
    private String name; 
    private int age; 
    public details(String name,int age){ 
     this.name=name; 
     this.age=age; 
    } 
} 
+0

私はあなたが言ったようにしましたが、変更はありません –

+0

が更新された理由は詳細クラスの静的です。ちなみに、JAVAでは、詳細の代わりにDetailsのようにクラス名に大文字を使用していました。 – CbL

+0

これは私の問題を解決しました。 –

関連する問題