2012-02-17 48 views
0

我創建了一個類BaseAdapter。同時,每行的每個按鈕都需要具有點擊功能。但是,onListItemClick()需要ListActivity擴展。我使用工具,但它報告錯誤爲"ListActivity cannot be resolved to a type"。我如何解決這個問題?如何在BaseAdapter中使用onListItemClick()?

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; 
     } 

    } 

回答

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 
    } 
} 
相關問題