如何在BaseAdapter中使用onListItemClick()?

时间:2012-02-17 21:00:38

标签: android android-layout android-widget android-listview

我创建了一个扩展BaseAdapter的类。同时,需要为每行的每个按钮设置一个点击功能。但是,onListItemClick()需要ListActivity扩展名。我使用工具,但它报告错误为"ListActivity cannot be resolved to a type"。我怎么能解决这个问题?

package panda.com.db;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;



public class MyAdapter extends BaseAdapter implements ListActivity{

        private LayoutInflater mInflater;
        private List<Map<String, String>> mData;

        @Override
        protected void onListItemClick(ListView l,View v, int position,long id){
           Log.v("MyListView4", (String)mData.get(position).get("TITLE"));
        }

        public MyAdapter(Context context, List<Map<String, String>> m){
            this.mInflater = LayoutInflater.from(context);
            mData=m;
        }

        public final class ViewHolder{
            public TextView id;
            public TextView title;
            public TextView isbn;
            public TextView publisher;
            public Button viewBtn;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder=null;

            if(convertView == null){
                holder=new ViewHolder();

                convertView = mInflater.inflate(R.layout.vlist, null);
                holder.title = (TextView)convertView.findViewById(R.id.TITLE);
                holder.id = (TextView)convertView.findViewById(R.id.ID);
                holder.isbn = (TextView)convertView.findViewById(R.id.ISBN);
                holder.publisher = (TextView)convertView.findViewById(R.id.PUBLISHER);
                holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
                convertView.setTag(holder);
            }else{
                holder=(ViewHolder)convertView.getTag();
            }

            //holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
            holder.title.setText((String)mData.get(position).get("TITLE"));
            holder.id.setText((String)mData.get(position).get("ID"));
            holder.isbn.setText((String)mData.get(position).get("ISBN"));
            holder.publisher.setText((String)mData.get(position).get("PUBLISHER"));
            holder.viewBtn.setGravity(Gravity.RIGHT);
            holder.viewBtn.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DbActivity().showInfo();
                }
            });
            return convertView;
        }

    }

1 个答案:

答案 0 :(得分:0)

你不会拥有BaseAdapter工具ListActivity ......这有点奇怪。

相反,你会有类似的东西:

public class MyActivity extends ListActivity implements OnClickListener {

    @Override
    public void onCreate(){

        ListView lv = getListView();
        lv.setOnItemClickListener(this);
        MyAdapter adapter = new MyAdapter(getApplicationContext(), null);
        // Replace null with your application data, in whichever way you choose to pass it.
        lv.setAdapter(adapter);

    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // do something
    }
}