监听器似乎不适用于ListView

时间:2013-04-26 16:51:12

标签: android listview onitemclicklistener

我试图通过onItemClickListener

ListView中设置一个简单的ArrayAdapter
package it.polito.mad.task2;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class listFragment extends ListFragment {

public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b){

        View v = li.inflate(R.layout.fraglist , vg, false); //inflate populates a view with desired content

        //use an adapter to load the imageviews in the fragment
        ListView listView = (ListView)v.findViewById(R.id.listViewListFrag); //get the resource id for the view to fill with data from adapter

        ArrayList<opera> pics = JsonLoad("list.json"); 
        operaAdapter adapter = new operaAdapter(this.getActivity(), R.layout.opera, R.id.element, pics);//create the adapter passing the parameter needed to load content to elements in the opera layout
        listView.setAdapter(adapter); //set the adapter for the view in the fragment

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                AppObj app = (AppObj) getActivity().getApplication();
            Intent goToDet = new Intent(getActivity(),ActivityDetails.class);

            app.setItem(position);

            startActivity(goToDet);

            }
        });


        return v; 
    }
//more methods 
}

这是适配器类

package it.polito.mad.task2;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class operaAdapter extends ArrayAdapter<opera> {
    private Activity act;
    ArrayList<opera> list;
    private static LayoutInflater inflater=null;

    public operaAdapter(Activity a, int layout, int element, ArrayList<opera> res){//save in local variables content of parameters

        super(a, layout, res);
        act = a;
        list = res;

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

    @Override
    public opera getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView holder;

        if(convertView==null){
           inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = inflater.inflate(R.layout.opera, null);

            holder = (TextView) convertView.findViewById(R.id.element);
            convertView.setTag(holder);
        }
        else{
            holder = (TextView) convertView.getTag();
        }

        holder.setText(list.get(position).getTitle());

        Drawable d;
        try {
            d = Drawable.createFromStream(act.getAssets().open(list.get(position).getImage()), null);
            d.setBounds(0, 0, 200, 200); //set size of the drawable on the left of the text 
            holder.setCompoundDrawables(d, null, null, null);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return convertView;
    }

}

我已经尝试了几件事,但听众似乎没有得到任何事件,我错过了什么?

1 个答案:

答案 0 :(得分:1)

您正在设置OnItemClickListener,但您没有对onItemClick回调做任何事情。尝试这样的事情来表明正在看到点击次数:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), "Position " + position 
                    + " clicked", Toast.LENGTH_SHORT).show();
        }
    });