Search code examples
androidlayoutlistadapterandroid-gridviewlayout-inflater

Gridview inside listview in android facing android.view.InflateException problems


I have to create the custom gridview within text and listview in android application.

Here i have to display the category name on gridview and if the categoryname is matches which means have to display the list is display on listview below the corresponding categoryname.here i have referred the below tutorial:my reference tutorial

I have displayed category name on gridview.here if the catagory name is notchbolly which means the gridview list items display on horizontal listview above the notchbolly category name.

But here i have to run the app means the else part only executed on all category name.whats wrong in my code.pls give me solution for these...

I have used below code:

public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
ListAdapter adapter;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView) vi.findViewById(R.id.title); 
    ListView imageView = (ListView) vi
            .findViewById(R.id.grid_item_image);

            HashMap<String, String> item = new HashMap<String, String>();
    item = data.get(position);
    // Setting all values in listview
    title.setText(item.get(MainActivity.KEY_TITLE));

        if (item.equals("notchbolly")) {
                 listview.setAdapter(this);
            }  else {
                    listview.setBackgroundResource(R.drawable.rihanna);
                } 
         return vi;
               }

MainActivity.java:

public class MainActivity extends Activity {
  static final String URL = "http://webservices/new_feed_articls.xml";
 static String KEY_CATEGORY = "Categories";
 static final String KEY_TITLE = "Category";
 static final String KEY_TITLENAME = "name";

 LazyAdapter adapter;

 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);

        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>   ();
            Element e = (Element) nl.item(i);
             map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute(KEY_TITLENAME));
             // map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            songsList.add(map);
        }
        GridView list = (GridView) findViewById(R.id.listView1);
    adapter = new LazyAdapter(this, songsList);
     list.setAdapter(adapter);

please give me solution for these.whats wrong in my code ????

EDIT:

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
   >

  <com.example.notch.HorizontalListView
android:id="@+id/grid_item_image"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
 />
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    >
</TextView>

</LinearLayout>

activity_main.xml

<RelativeLayout 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="#FFFFFF"
android:orientation="vertical">

 <GridView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
/>

I have displayed category name on gridview.here if the catagory name is notchbolly which means the gridview list items display on horizontal listview above the notchbolly category name.

But here i have to run the app means the else part only executed on all category name.whats wrong in my code.pls give me solution for these...

Here the else part only executed...


Solution

  • I think there are a few details you may want to revise.

    Number 1: XML file line #11: Error inflating class

    This means that on your XML layout file, not sure which since you deleted the log from the question, there is a problem when inflating a class, most likely this happens when inflating your com.example.notch.HorizontalListView on list_row.xml. Make sure you check the Graphical Layout tab on Eclipse, it usually gives an error indicating it was unable to create the custom view. Something along the lines:

    The following classes could not be found:
    - com.example.custom.view (Change to android.view.View, Fix Build Path, Edit XML, Create Class)
    

    Number 2: You seem to be casting a ListView to your custom view. Taken from your getView() method:

    ListView imageView = (ListView) vi
            .findViewById(R.id.grid_item_image);
    

    Where on your list_row, grid_item_image is a com.example.notch.HorizontalListView. This could be causing some minor issues too.

    Number 3: Why are you setting your adapter inside your adapter's item? :crazyFace: If you do plan to have a listview inside your gridview, create two different adapters, and two different layout for the views.

    Again, from your getView():

    if (item.equals("notchbolly")) {
                 listview.setAdapter(this);
            } 
    

    And btw, you have a null pointer exception on those three lines above, listview is an item that is not defined in your adapter. The ListView you defined, is with the a different identifier: imageView, so you want to check on that aswell.