Search code examples
javaandroidarraylistadapter

The data in my ArrayList repeats the last entered data of the array. What is the reason for this?


When I'm using the ArrayList using an custom adapter for listing the images in a list view the code shows no error. The problem is that while using the code only my last entered data is showimg in the list view. Why can't the previously entered data be viewed?

My activity class:

public class SocialActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_social);
        String json;
        ImageView babu = (ImageView) findViewById(R.id.babu);
        ListView lvsoc = (ListView) findViewById(R.id.list_social);
        LinearLayout l1 = (LinearLayout) findViewById(R.id.layhead);
        LayoutInflater inflater = null;
        ArrayList<SubjectData> arrayList = new ArrayList<SubjectData>();
        ArrayAdapter<SubjectData> a;
        /*ArrayList<String> allcategory = new ArrayList<>();
        ArrayList<String> allcategory1 = new ArrayList<>();
        ArrayList<String> img = new ArrayList<>();
        ArrayList<String> img1 = new ArrayList<>();*/
        arrayList.add(new SubjectData("JAVA", "https://www.tutorialspoint.com/java/", "https://s18955.pcdn.co/wp-content/uploads/2017/11/Facebook-share-icon.png"));
        arrayList.add(new SubjectData("Python", "https://www.tutorialspoint.com/python/", "https://www.tutorialspoint.com/python/images/python-mini.jpg"));
        /*arrayList.add(new SubjectData("Javascript", "https://www.tutorialspoint.com/javascript/", "https://www.tutorialspoint.com/javascript/images/javascript-mini-logo.jpg"));
        arrayList.add(new SubjectData("Cprogramming", "https://www.tutorialspoint.com/cprogramming/", "https://www.tutorialspoint.com/cprogramming/images/c-mini-logo.jpg"));
        arrayList.add(new SubjectData("Cplusplus", "https://www.tutorialspoint.com/cplusplus/", "https://www.tutorialspoint.com/cplusplus/images/cpp-mini-logo.jpg"));
        arrayList.add(new SubjectData("Android", "https://www.tutorialspoint.com/android/", "https://www.tutorialspoint.com/android/images/android-mini-logo.jpg"));*/

        //SocialAdapter customAdapter = new SocialAdapter(SocialActivity.this, arrayList);
        lvsoc.setAdapter((ListAdapter) arrayList);

        //arrayList.add(new SubjectData("JAVA", "https://www.tutorialspoint.com/java/", "https://www.tutorialspoint.com/java/images/java-mini-logo.jpg"));
        babu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i;
                i = new Intent(SocialActivity.this, MainActivity.class);
                startActivity(i);
            }
        });






    }
    @Override
    public void onBackPressed() {
        Intent i = new Intent(SocialActivity.this, MainActivity.class);
        startActivity(i);
        super.onBackPressed();
    }
}

My adapter class:

import java.util.ArrayList;

public class SocialAdapter implements ListAdapter {
    ArrayList<SubjectData> arrayList;
    Context context;
    public SocialAdapter(Context context, ArrayList<SubjectData> arrayList) {
        this.arrayList=arrayList;
        this.context=context;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) { }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) { }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SubjectData subjectData=arrayList.get(position);
        if(convertView==null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView=layoutInflater.inflate(R.layout.list_row, null);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(subjectData.Link));
                    context.startActivity(i);
                    //Toast.makeText(context,subjectData.Link,Toast.LENGTH_LONG).show();
                }
            });
            TextView tittle=convertView.findViewById(R.id.title);
            ImageView imag=convertView.findViewById(R.id.list_image);
            tittle.setText(subjectData.SubjectName);
            Picasso.get()
                    .load(subjectData.Image)
                    .into(imag);

        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return arrayList.size();
    }


    @Override
    public boolean isEmpty() {
        return false;
    }
}

My class for holding subject data:

package com.gjinfotech.eschoolsolution.ClassAdapter;

public class SubjectData {
    public static  String SubjectName;
    public static String Link;
    public static String Image;
    public SubjectData(String subjectName, String link, String image) {
        this.SubjectName = subjectName;
        this.Link = link;
        this.Image = image;
    }
}

Solution

  • Your SubjectData has all static fields. So every instance of SubjectData has the same information.

    Instead:

    public class SubjectData {
        public String SubjectName;
        public String Link;
        public String Image;
        public SubjectData(String subjectName, String link, String image) {
            this.SubjectName = subjectName;
            this.Link = link;
            this.Image = image;
        }
    }