Shubham Kumar
Shubham Kumar

Reputation: 295

Custom ListVIew Is not displayed an empty screen is displaying

An empty view doesn't appear when no items in the list . what is the problem?

Main_Activity

package com.example.android.customadapter;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ArrayList<Players> objects = new ArrayList<>();


        objects.add(new Players("Shikhar Dhawan",R.drawable.shikhar));
        objects.add(new Players("Virat Kohli",R.drawable.kohli));

    }
}

My model class

Players.java

package com.example.android.customadapter;

public class Players {

    private String mName;
    private int image;


    Players(String name,int res)
    {
        mName = name;
        image = res;
    }

    public String getName()
    {
        return mName;
    }

    public int getImage()
    {
        return image;
    }
}

CustomAdapter class

package com.example.android.customadapter;

My custom array list adapter

public class CustomAdapter extends ArrayAdapter<Players> {

    public CustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Players> objects) {
        super(context, resource, objects);
    }


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

        View v = convertView;
        if(v==null)
        {
            v = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }

        Players p = getItem(position);

        TextView t = (TextView)v.findViewById(R.id.miwok_text_view);

        t.setText(p.getName());

        ImageView i = (ImageView)v.findViewById(R.id.image);

        i.setImageResource(p.getImage());


        return v;
    }

}

Upvotes: 2

Views: 55

Answers (3)

Shubham Kumar
Shubham Kumar

Reputation: 295

I have solved it at the end of the ArrayList in the main_activity init. the Listview in OnCreate method and set the array adapter to the listView

CustomAdapter adapter = new CustomAdapter(this, words);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);

Upvotes: 1

SAVVY
SAVVY

Reputation: 850

you are getting the error because you are not taking the value from the class. Here is the code that I use

 @NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view =convertView;
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    if (convertView==null){
        view=layoutInflater.inflate(R.layout.customplaylist,parent,false);
    }
    Playlistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position);
    TextView textView =(TextView)view.findViewById(R.id.textView7);
    textView.setTypeface(typeface);
    textView.setText(playlistfileinfo.getName());
    return view;
}

you can see thatPlaylistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position); hope this helps you.

Upvotes: 0

vishakha yeolekar
vishakha yeolekar

Reputation: 3494

get listView from your activity_main.xml. And them set adapter for that list. For more details : http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

Upvotes: 0

Related Questions