2

我有一个名为 ResultsAdapter 的自定义 ArrayAdapter,在用户选择特定行后,它应该打开一个详细说明该特定行的新活动。当我使用的适配器是一个简单的 ArrayAdapter 时,这工作得很好,但由于扩展了它并创建了我自己的(以允许每行中有多个数据),响应每行点击的能力已经消失。

我已将其范围缩小(我认为),我需要onClickListener在我的客户适配器中指定它以及它需要做什么,但我不确定要指定什么。以前,大部分“可点击性”是在我使用的自定义 ListFragment 中处理的。

这是我正在使用的自定义适配器类:

public class ResultsAdapter extends ArrayAdapter<String> {

Context myContext;

public ResultsAdapter(Context context, int textViewResourceId) {
   super(context, textViewResourceId);
   myContext = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    if(convertView == null){            
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null); //must be overall layout  
    }

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                //this is where the clicking actions need to be defined, right?
        }
    });

    TextView venName = (TextView) convertView.findViewById(R.id.rowName);
    TextView venAddress = (TextView) convertView.findViewById(R.id.rowAdd);

    venName.setText(VenueList.getVenueName(position)); 
    venAddress.setText(VenueList.getVenueAddress(position));

    return convertView;
}

}

这是自定义 ListFragment 类:

public class ListFragmentClickable extends ListFragment{

  private OnItemSelectedListener listener;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.activity_results, container, false);
      return view;
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
      updateDetail(position);
  } 

  public interface OnItemSelectedListener {
      public void onItemSelected(String name, String geo, String id, String address);
  }


  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof OnItemSelectedListener) {
        listener = (OnItemSelectedListener) activity;
      } else {
        throw new ClassCastException(activity.toString()
            + " must implement ListFragmentClickable.OnItemSelectedListener");
      }
  }

  public void updateDetail(int position) {
      // Get data from VenueList
      String name = VenueList.getVenueName(position);
      String geo = VenueList.getVenueGeo(position);
      String id = VenueList.getVenueId(position);
      String address = VenueList.getVenueAddress(position);

      listener.onItemSelected(name, geo, id, address);
  }

}

谁能帮我指出为什么失去点击的能力?我怎样才能恢复它?

谢谢!

4

1 回答 1

0

显然这与自定义适配器无关,而是与应用程序中片段的实现有关。我必须将 xml 设置为包含android:clickable="true"活动的 ListView 部分。

于 2013-05-30T04:12:58.680 回答