0
我的應用有ListView
帶小孩TextView
和Button
。 (參考圖片)如何獲得Listview兒童位置,如果孩子被點擊?
我怎樣才能獲得按鈕的位置,如果點擊它?
適配器
public class HistoryAdapter extends CursorAdapter {
public HistoryAdapter(Context context, Cursor c, int flags) {
super(context,c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
TextView text = (TextView) view.findViewById(R.id.text);
String body = cursor.getString(cursor.getColumnIndex("name"));
text.setText(body);
}
}
的ListView onClick事件
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setCancelable(false);
getDialog().setTitle("History");
View view = inflater.inflate(R.layout.history,null);
back = (Button) view.findViewById(R.id.button);
back.setOnClickListener(this);
list = (ListView) view.findViewById(R.id.listView);
ContentResolver resolver = getActivity().getContentResolver();
c = resolver.query(Contract.SaveRunning.CONTENT_URI, null, null, null, null);
adapter = new HistoryAdapter(getActivity(), c, 0);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setChart(position);
dismiss();
}
});
return view ;
}
你想要的究竟是什麼,你想設置不同的行動對不同的孩子點擊 – AndroidHacker
是的,每個視圖有不同的行動 – Error
你好@Error你嘗試過'list.getItemAtPosition(position);'獲取當前位置(點擊),如果你想要它作爲字符串,你可以添加.toString()。讓我知道如果它的工作:P –