Android Studio: Simple BaseAdapter Setup Fail - android

I would like to create a simple populated GridView using BaseAdapter but I keep getting empty list - application runs but there are no Views displayed.
Below are all files are use for this task:
Main:
String[] items = {"Some", "items", "to", "display"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.grid);
final TextView text = (TextView)findViewById(R.id.text);
MyAdapter adapter = new MyAdapter(getApplicationContext(), items);
grid.setAdapter(adapter);
MyAdapter:
LayoutInflater inflater;
Context context;
String[] myData;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.myData = myData;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return myData[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid, parent, false);
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(myData[position]);
return convertView;
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.random.MainActivity">
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="40dip"
android:horizontalSpacing="5dip"
android:numColumns="auto_fit"
android:columnWidth="100dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</android.support.constraint.ConstraintLayout>
grid.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
I have checked in various sources countles times and I did not manage to find an error in my code. It would be amazing if someone would take a look at it and find a reason for empty activity as the reason is probably obvious.

Your getCount method always return 0. Return the number of elements in your myData array instead.
#Override
public int getCount() {
return myData.length;
}

You can try this my friend
public class MyAdapter extends BaseAdapter {
Context context;
String[] data;
public MyAdapter(Context context, String[] myData) {
this.context = context;
this.data = myData;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView);
text.setText(data[position]);
return convertView;
}
}

Related

Android Custom ListView has no items

I tried to set up a custom ListView as a table to display Product Details. As you see in my Code I use a custom adapter and a additional xml file to populate the ListView. My problem is that the ListView ist empty. There are no items displayed and I do not see my mistake. Can you help me?
Fragment:
public class ProductDetail1Fragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product_detail1,container,false);
HashMap<String,String> mapProduct = new HashMap<String,String>();
for(int i=0;i<10;i++) {
mapProduct.put("Key" + i, "Value" + i);
}
ListView listView=(ListView) view.findViewById(R.id.productListview);
ProductDetailAdapter adapter = new ProductDetailAdapter(getActivity(),mapProduct);
listView.setAdapter(adapter);
return view;
}
Adapter:
public class ProductDetailAdapter extends BaseAdapter {
private HashMap<String,String> list;
private Context context;
public ProductDetailAdapter(Context c, HashMap<String,String> list){
super();
this.context = c;
this.list=list;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView=inflater.inflate(R.layout.product_detail_data_row,null);
}
TextView textViewKey = (TextView)convertView.findViewById(R.id.productDataKey);
TextView textViewValue = (TextView)convertView.findViewById(R.id.productDataValue);
textViewKey.setText("tst");
textViewValue.setText("ddfadfs");
return convertView;
}
}
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parker.tfdeapp.ProductDetail2Fragment">
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Listview_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/productDataKey"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold" />
<TextView
android:id="#+id/productDataValue"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
Add getCount method in your adapter
#Override
public int getCount() {
return list.size();
}
Add this Override methods in your adapter class
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Missing class implementations of BaseAdapter.
See the documentation
#Override
public int getCount() {
return list.size();
}
#Override
public ListData getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
Implementing getCount() only makes items visible. Because it provides
item counts.
1) Need to change ListView to match parent height (its bad performance):
<ListView
android:id="#+id/productListview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2) Just to override missing functions in your BaseAdapte:
#Override
public int getCount() {
return mList.size();
}
#Override
public ListData getItem(int index) {
return mList.get(index);
}
#Override
public long getItemId(int index) {
return index;
}
3) Use ViewHolder... its very good pattern

Adding Textviews to ViewPager present in a ListView but the textviews are not visible

Layout of the row of list view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/linearLayout_template3">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/template3_textView"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation ="horizontal" />
</LinearLayout>
ListView Adapter:
Here I am using Holder class to save the Tag information of convertView.
and also giving a unique ID to viewPager.I basically intend to show Images in ViewPager but to test the code I am working with TextViews.
public class ListViewAdapter extends ArrayAdapter<ParsedItems> {
LinearLayout mainLinnerLayout;
ViewPagerAdapter pagerAdapter;
Context context;
ArrayList<ParsedItems> mObject;
ViewPagerAdapter adapter;
private ProgressDialog progressDialog;
public ListViewAdapter(Context context,int resource, ArrayList<ParsedItems> objects) {
super(context,resource,objects);
this.context = context;
this.mObject = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
ParsedItems items = mObject.get(position);
String template = items.getTemplate();
ParsedItemsImage mInnerItem;
ArrayList<ParsedItemsImage> mInnerItems = items.getItems();
LinearLayout innerLinearLayout=null;
if((Holder)convertView.getTag() == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout_template3, parent,false);
ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.pager);
holder = new Holder();
holder.viewPager = viewPager;
holder.viewPager.setId(NotificationID.getID());
holder.textView = (TextView)convertView.findViewById(R.id.template3_textView);
convertView.setTag(holder);}
else
{
holder = (Holder)convertView.getTag();
}
holder.textView.setText("oyi!");
pagerAdapter = new ViewPagerAdapter(mInnerItems,context,convertView);
holder.viewPager.setAdapter(pagerAdapter);
return convertView;
}
class Holder
{
TextView textView;
ViewPager viewPager;
}
}
ViewPagerAdapter class:
public class ViewPagerAdapter extends PagerAdapter {
ArrayList<ParsedItemsImage> mInnerItems;
Context context;
private final WeakReference<View> ref;
public ViewPagerAdapter(ArrayList<ParsedItemsImage> items,Context context, View view)
{
mInnerItems = new ArrayList<ParsedItemsImage>(items);
this.context = context;
ref = new WeakReference<View>(view);
}
#Override
public int getCount() {
//return mInnerItems.size();
return 6;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View collection;
ParsedItemsImage mParsedItemsImage = mInnerItems.get(position);
String url=mParsedItemsImage.getImage();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
collection = inflater.inflate(R.layout.template3_imageview,container,false);
TextView textView = (TextView)collection.findViewById(R.id.text_temp3);
ViewPager vp = (ViewPager)container;
textView.setText("test");
vp.addView(collection);
return textView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((TextView) object);
}
}
Template3_imageview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text_temp3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/imageView_template3"/>
</LinearLayout>
Output:
SO the output shows the textview added in the ArrayAdapter's getView() method :
holder.textView.setText("oyi!");
but doesn't show the one in the ViewPager Adapter:
textView.setText("test");
Please help.
P.S.: I intend to add images finally but i was testing if textviews are getting added in pager or not.So you will see some extra code in the post.
The error was in isViewFromObject() method. I changed this
#Override
public boolean isViewFromObject(View view, Object o) {
return false;
}
to
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
and it worked!

Adding A button and a TextView for each Line of a ListView

I am currently working on a very basic android application and have encountered an obstacle I cannot solve on my own.
In my application I want to have a start screen with a ListView. In each Line of this ListView there should be a Button and a TextView. I want to have approximately 5 Lines. When you click on each of the Button you should be able to get to different Activities. How do I do that? I read something about adapters but I am still not sure how to build this.
Here's my xml code for the TextView and the Button:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:id="#+id/rl01">
<TextView
android:layout_width= "wrap_content"
android:layout_height="50dp"
android:id="#+id/text01"
android:text="hello"
android:textSize="24dp"
android:textColor="#color/abc_search_url_text_normal"
android:paddingRight="#dimen/activity_horizontal_margin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_toRightOf="#id/text01"
android:text="Press Me!"
/>
</RelativeLayout>
The xml layout you have posted, will be used as each listview item.
Step 1:
Create a class which extends BaseAdapter;
public class CustomAdapter extends BaseAdapter
{
Context con;
String[] data;
public CustomAdapter (Context context, String[] data)
{
this.con = context;
this.data = data;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return 0;
}
//this method will be called for every item of your listview
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.customview, parent, false);
TextView text = (TextView) view.findViewById(your text view id); //recognize your view like this
text.setText(data[position]);
return convertView;
}
}
Step 2:
In your activity, recognize your listview:
yourListViewReference = (ListView) findViewById(R.id.your_list_view_id);
And initialize String array:
String[] data = {"item 1", "item2", "item3"}; //how many items you want
And then create instance of custom adapter you created, and set it to listview:
CustomAdapter ad = new CustomAdapter(this, data);
yourListViewReference.setAdapter(ad);
And sorry for my bad english. I am actually working on it.
This article from Vogella is really useful for what you want to do.
Basically, you'll create an Adapter that extends the BaseAdapter class as follows:
public class Adapter extends BaseAdapter {
private List<Item> mItems;
private Context mContext;
public EventAdapter(Context context, List<Event> items) {
mContext = context;
mItems = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
// This recycles your view and prevents constant inflation, which can really hit your performance.
View rowView = convertView;
if (rowView == null) {
ViewHolder viewHolder = new ViewHolder();
rowView = inflater.inflate(R.layout.yourLayout, parent, false);
viewHolder.text = (TextView) rowView.findViewById(R.id.yourTextViewId);
viewHolder.button = (Button) rowView.findViewById(R.id.yourButtonId);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
// Get the correct item by position
Item item = item.get(position);
// Update the row layout with your item data
holder.text.setText(item.text);
holder.button.setButton(item.button);
// Return your row view
return rowView;
}
#Override
public int getCount() {
return mItems.size();
}
#Override
public Object getItem(int position) {
}
#Override
public long getItemId(int position) {
}
static class ViewHolder {
public TextView text;
public Button button;
}
Afterwards you just need to set this adapter to your ListView or RecyclerView by doing
listView.setAdapter(new Adapter(this, items));
I would write a simple example:
You dont need a button in listview row, just implement onItemClick();
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/custom_list"
android:longClickable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
list_row.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:id="#+id/rl01">
<TextView
android:layout_width= "wrap_content"
android:layout_height="50dp"
android:id="#+id/text01"
android:text="hello"
android:textSize="24dp"
android:textColor="#color/abc_search_url_text_normal"
android:paddingRight="#dimen/activity_horizontal_margin"
/>
MainActivity.java
oncreate()
{
....
lv1 = (ListView) result.findViewById(R.id.custom_list);
String[] listdata = {"txt1", "txt2", "txt3", "txt4", "txt5"};
ListAdapter listAdapt = new ListAdapter(this, listdata );
lv1.setAdapter(listAdapt);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//do ur work here when list row selected
}
});
..
}
ListAdapter.java
public class ListAdapter extends BaseAdapter {
String[] listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context aContext, String[] listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row, null);
TextView tv1 = (TextView)convertView.findViewById(R.id.text01);
tv1.setText(listData.[position]);
return convertView;
}

SherlockFragment and BaseAdapter: never call getView and getItem

I already saw a bunch of questions but i didn't found an solution! What im doing wrong? Why getView is never called?!
Here`s my adapter:
public class ConteudoAdapter<T extends IProgramacao> extends BaseAdapter {
private Context context;
private List<T> list;
private int resourceId;
public ConteudoAdapter(Context context, int resourceId, List<T> list){
this.context = context;
this.list = list;
this.resourceId = resourceId;
}
#Override
public int getCount() {
Log.v("test", "Returning count " + (this.list.size() != 0 ? this.list.size() : 0));
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
ViewHolder holder = null;
T entidade = list.get(position);
if(row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
holder.tvTitle.setText(entidade.getTitulo());
holder.tvConteudo = (TextView) row.findViewById(R.id.tvConteudo);
holder.tvConteudo.setText(entidade.getChamada());
row.setTag(holder);
}
// holder.ivImg = (ImageView) layout.findViewById(R.id.nivImg);
// holder.ivImg.setImageResource(entidade.);
// IMAGE VIEW
// holder.ivImg.setVisibility(View.VISIBLE);
// il.get(list.get(position).getUrlImage(), il.getImageListener(holder.ivImg, R.drawable.load, R.drawable.error));
//tvTitle.setText(entidade.getTitulo());
return view;
}
public static class ViewHolder{
//public NetworkImageView nivImg;
//public ImageView ivImg;
public TextView tvTitle;
public TextView tvConteudo;
}
}
Here`s how i call my adapter (inside an asynctask because im getting values from web (JSON):
public class CategoriaEventoFragment extends SherlockFragment {
private View rootView;
private EventoBO evento;
private ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = (View) inflater.inflate(R.layout.categoria, container, false);
CarregaDadosCategoriaEventoCaller carregaDadosCategoriaEventoCaller = new CarregaDadosCategoriaEventoCaller();
carregaDadosCategoriaEventoCaller.execute((Void[]) null);
return super.onCreateView(inflater, container, savedInstanceState);
}
public class CarregaDadosCategoriaEventoCaller extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
evento = new EventoBO("http://urlhere");
evento.popularListaImagemChamada("http://urlhere");
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
lv = (ListView) rootView.findViewById(R.id.listViewCategoria);
lv.setAdapter(new ConteudoAdapter<Evento>(rootView.getContext(), R.layout.item, evento.getLista()));
}
}
}
}
XML FILES:
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/home_bg_branco"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvConteudo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
categoria.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewCategoria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/home_bg_branco" >
</ListView>
</LinearLayout>
My LogCat Returns this for my verbose test:
09-02 17:13:27.322: V/test(24961): Returning count 5
09-02 17:13:27.322: V/test(24961): Returning count 5
Thanks in advance!!!
In onCreateView() you should be returning the rootView instead of the super.

Customize List Item on ListActivity (Android)

I'd like to customize some settings (color, margin) for some items on a listview in a listactivity after or before setting the adapter. How can I do that? Is there any function that can I override?
Thank you.
you can use an own listadapter.. http://www.vogella.com/articles/AndroidListView/article.html
This is how you can do to customize item of your list:
Layout of your list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Layout of each item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
And finaly your activity:
public class MyActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activitty);
final List<String> list = new ArrayList<String>();
list.add("test");
list.add("test");
list.add("test");
final CustomAdapter adapter = new CustomAdapter(this, list);
final ListView listView = getListView();
listView.setAdapter(adapter);
}
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private List<String> mList;
public CustomAdapter(Context context, List<String> list) {
mContext = context;
mList = list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
// if it is the first time you create the row
// you get the layout of each row here
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
// you keep your layout in a holder
holder = new Holder();
holder.mText = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
// if the row has already been created, you get it from the holder
holder = (Holder) convertView.getTag();
}
// you do what you want with the content
holder.mText.setText(getItem(position));
holder.mText.setTextColor(Color.BLUE);
return convertView;
}
private class Holder {
public TextView mText;
}
}
}

Categories

Resources