user2108957
user2108957

Reputation:

my listview is not working

My script is not working. My goal is to make a listview with some clickable items. but the app keeps crashing. How to fix this? Am i using the adapter right, or not?

I'm using a title, subtitle and an image as the item in the listview.

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter{
    // Define variables
    ArrayList<HashMap<String, String>> data;
    Activity activity;
    private static LayoutInflater inflater=null;

    public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    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); // title
        TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // subtitle

    HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);

        // Setting all values in listview
        title.setText(item.get(Index.TITLE));
        subtitle.setText(item.get(Index.SUBTITLE));
        return vi;
    }

}

This is my index class:

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class Index extends Activity {

    public static final Object TITLE = "title";
    public static final Object SUBTITLE = "subtitle";
    public static final Object THUMBNAIL = "thumbnail";

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

        buildMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    //Builds the menu for listview
    public void buildMenu(){
        ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
        //Arrays for info
        String[] menuTitleArray = {"Gallerij"}; 
        String[] menuSubtitleArray = {"Bekijk foto's en geef reacties"};
        String[] menuThumbnailArray = {"gallery"};
        for(int i=0; i < menuTitleArray.length; i++){
            HashMap<String, String> item = new HashMap<String, String>();
            item.put((String) TITLE, menuTitleArray[i]);
            item.put((String) SUBTITLE, menuSubtitleArray[i]);
            item.put((String) THUMBNAIL, menuThumbnailArray[i]);
            menu.add(item);
        }
        MenuAdapter adapter = new MenuAdapter(this, menu);
        ListView list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);   
    }
}

log file: http://pastebin.com/kzeVMLuk

Upvotes: 1

Views: 119

Answers (2)

R.F
R.F

Reputation: 312

After you used the .put and inserted the strings. To make them clickable, try using the onItemClickListener. It will help.

Upvotes: 0

A--C
A--C

Reputation: 36449

One problem:

private static LayoutInflater inflater=null,

It is never instantiated in your code:

In your constructor do:

private LayoutInflater inflater=null;
public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = LayoutInflater.from (a);
}

As you can see, I also took out the static modifier. I'd advise against the static modifier for anything related with Contexts, especially since this is bound to an Activity.

Upvotes: 1

Related Questions