Как определить, какая кнопка и в какой позиции нажата в CustomList
Я использую пользовательский просмотр списка, в котором у меня есть две кнопки для каждого элемента. Следующим является мой элемент XML.
и следующее это Addapter, который я использую.
public class CustomListAdapter extends BaseAdapter implements OnClickListener {
Context mContext;
String[] mArrayList;
public CustomListAdapter(Context context, String[] SoundNames) {
this.mContext = context;
this.mArrayList = SoundNames;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater1 = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.list_item, null);
holder.btn_sound = (Button) convertView.findViewById(R.id.btn_sound);
holder.ib_info = (ImageButton) convertView.findViewById(R.id.ib_info);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.btn_sound.setText(mArrayList[position]);
//holder.btn_sound.setOnClickListener(this);
return convertView;
}
private static class Holder {
Button btn_sound;
ImageButton ib_info;
}
@Override
public int getCount() {
return mArrayList.length;
}
@Override
public Object getItem(int position) {
return mArrayList[position];
}
@Override
public long getItemId(int position) {
return position;
}
}
и я устанавливаю вид списка в следующем коде.
public class SectionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
ListView mListView;
Bundle args = getArguments();
int currentView = args.getInt(Constants.ARG_SECTION_NUMBER) - 2;
if (currentView == 0) {
rootView = inflater.inflate(R.layout.deer, container, false);
//mListView = (ListView) rootView.findViewById(R.id.listView_deer);
CustomListAdapter mListAdapter = new CustomListAdapter(MainActivity.appContext, Constants.deer_Sound_Names);
//mListView.setAdapter(mListAdapter);
((AdapterView) rootView.findViewById(R.id.listView_deer)).setAdapter(mListAdapter);
((AdapterView) rootView.findViewById(R.id.listView_deer)).setOnItemClickListener(mListner);
} else if (currentView == 1) {
rootView = inflater.inflate(R.layout.guide_layout, container, false);
Toast.makeText(MainActivity.appContext, "Loading Guide...", Toast.LENGTH_LONG).show();
WebView guideViewer = (WebView) rootView.findViewById(R.id.webView_guide);
guideViewer.loadUrl("file:///android_asset/deer_html/guide.htm");
} else {
rootView = inflater.inflate(R.layout.activity_main, container,
false);
}
return rootView;
}
OnItemClickListener mListner = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position,long id) {
Log.d("HFI","Item Clicked: "+Constants.deer_Sound_Index[position]);
switch (view.getId()) {
case R.id.btn_sound:
Log.d("HFI","Sound: "+Constants.deer_Sound_Index[position]);
break;
case R.id.ib_info:
Log.d("HFI","Info: "+Constants.deer_Sound_Index[position]);
break;
default:
break;
}
}
};
}
Проблема: пользовательский список не регистрируетсяsetOnItemClickListener(mListner);
На самом деле я хочу определить, какая кнопка и в какой позиции была нажата вSectionFragment
учебный класс.