public class ChooseFavorites extends Activity implements OnItemClickListener
{
StationManager st;
MyCustomAdapter arr;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_favorites);
st = new StationManager();
list = (ListView)findViewById(R.id.stationsList1);
arr = new MyCustomAdapter(this, android.R.layout.simple_list_item_multiple_choice, st.getNamesOfStations());
list.setAdapter(arr);
list.setOnItemClickListener(this);
}
class MyCustomAdapter extends ArrayAdapter<String> implements OnClickListener
{
LayoutInflater inflater;
Context ctx;
CheckBox cb;
String[] stationNames;
TextView stationName1;
public MyCustomAdapter(Context context, int textViewResourceId, String[] stationNames) {
super(context, textViewResourceId, textViewResourceId, stationNames);
ctx = context;
this.stationNames = stationNames;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 23;
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return stationNames[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row = convertView;
if(row==null)
{ // Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
}
stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName1.setText(stationNames[position]);
cb=(CheckBox)row.findViewById(R.id.cbCheck);
row.setOnClickListener(this);
return row;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT);
toast.show();
}
}
我不知道你是否只能通过代码看到这个......
但如果我的服装适配器中没有onClick
,那么什么都不会发生......
Android不会使用&#34;项目点击监听器&#34;
但它确实适用于&#34;点击&#34;方法....
问题是:我不具备我真正需要的位置......
希望有人能帮助我!谢谢大家!
答案 0 :(得分:0)
您可以使用此事件获得有关选中/取消选中复选框的通知
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
答案 1 :(得分:0)
您可以使用以下内容:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
或者您可以将位置保存为checkbox
的标记并检查checkbox clicklistener
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// ur code...
**cb.setTag(position);**
row.setOnClickListener(this);
return row;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
**int position = v.getTag();** //cast & check for Tag not null too.
Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
toast.show();
}
答案 2 :(得分:0)
使用Checkbox setOnCheckedChangeListener事件。
试试这个......
CheckBox ChkBx = ( CheckBox ) findViewById( R.id.checkbox);
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
答案 3 :(得分:0)
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row = convertView;
if(row==null)
{ // Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
}
stationName1 = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName1.setText(stationNames[position]);
cb=(CheckBox)row.findViewById(R.id.cbCheck);
cb.setOnCheckedChangeListener(new CompoundButton.
OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked)
{
cb.setChecked(isChecked);
Toast toast = Toast.makeText(getApplicationContext(), "checked" + position, Toast.LENGTH_SHORT);
toast.show();
}
else
{
cb.setChecked(isChecked);
Toast toast = Toast.makeText(getApplicationContext(), "unChecked" + position, Toast.LENGTH_SHORT);
toast.show();
}
}
});
return row;
}
}
答案 4 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder holder;
// Reuse an existing view, if one is supplied, otherwise create a new one
// Check to see if this one is created yet ?
if (convertView == null){
// Get inflater
LayoutInflater inflater = (LayoutInflater)
parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create a view for it
convertView = inflater.inflate(R.layout.test_reprot_lv_items, parent, false);
// Creates a holder class to contain all objects of a row
holder = new viewHolder();
holder.equipmentID = (TextView) convertView.findViewById(R.id.equipmentID);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
// Sets the tag associated with this view for later reuse.
convertView.setTag(holder);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
holder.checkBox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox c = (CheckBox) v;
int location = (Integer) c.getTag();
// Set selected state with the location of the row in the listview
itemsList.get(location).setSelectedState(c.isChecked());
}
});
}
else{
// In order to reuse the preallocated memory, get the holder from View's tag,
// which is allocated and saved in this view object. When a view requests
// to be rendered and it has not been instantiated, we new a holder and save
// it to View's 'Tag' for later reuse.
holder = (viewHolder) convertView.getTag();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkBox's Tag with the position of the row in the listview
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
CheckBox c = (CheckBox) v;
int location = (Integer) c.getTag();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set selected state with the location of the row in the listview
// The method setSelectedState, which provided by your own class,
// is called to update the checkbox state.
itemsList.get(location).setSelectedState(c.isChecked());
}
});
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Set checkbox state with calling the method getSelectedState which
// provied by your own class.
holder.checkBox.setChecked(itemsList.get(position).getSelectedState());
return convertView;
}