Skip to content Skip to sidebar Skip to footer

Custom Listview Is Not Showing Up The String

Listview is not showing the string list rather showing only the custom list view text My string declaration n the main String[] Example = new String[] { 'Android Introducti

Solution 1:

Listview is not showing the string list rather showing only the custom list view text

Becuase you are not setting text for TextView in each row of ListView just setting color of row.

so customize getView method and set TextView text from items String Array. see following example for more help: Using an ArrayAdapter with ListView


Solution 2:

You have to declare your CustomAdapter object in Activity As:

CustomAdapter ExampleArrayAdapter = new CustomAdapter(this, Example);

    ListView ExampleListView = (ListView)findViewById(R.id.listView);
    ExampleListView.setAdapter(ExampleArrayAdapter);

And Your Adapter Code should be:

public class CustomAdapter extends ArrayAdapter{
private LayoutInflater inflater;
private String[] items;
public CustomAdapter (Activity activity, String[] items){
          super(activity, 0, items);
    inflater = activity.getWindow().getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
 Random rnd = new Random();

   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));

  View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
    rowView.setBackgroundColor(color);
TextView tv = (TextView) rowView .findViewById(R.id.textView1);
tv.setText(items[position]);
    return rowView;


}
}

Post a Comment for "Custom Listview Is Not Showing Up The String"