我已经实现了一个列表视图。
我的列表视图中的每个项目都有两个元素 - 一个 TextView 和一个 Button。
现在我已经实现了 ArrayList 适配器以使我的列表视图可点击。
目前发生的情况是,每当我单击列表视图中的任何元素时,都会调用 OnItemClickListener。
现在我想做的是我只想让我的按钮点击而不是整个元素。
这是我已经实现的代码。
public class SurveyListActivity extends ListActivity {
static private ArrayList<Survey> EU=new ArrayList<Survey>();
static {
EU.add(new Survey(R.string.Survey1, R.drawable.completed,R.string.Survey1));
EU.add(new Survey(R.string.Survey2, R.drawable.completed,R.string.Survey2));
EU.add(new Survey(R.string.Survey3, R.drawable.inprogress,R.string.Survey3));
EU.add(new Survey(R.string.Survey4, R.drawable.inprogress,R.string.Survey4));
EU.add(new Survey(R.string.Survey5,R.drawable.inprogress,R.string.Survey5));
EU.add(new Survey(R.string.Survey6, R.drawable.start,R.string.Survey6));
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new SurveyAdapter());
}
@Override
protected void onListItemClick(ListView l, View v,int position, long id)
{
Toast.makeText(SurveyListActivity.this, "You Selected :- " + EU.get(position).name, Toast.LENGTH_LONG).show();
}
static class Survey
{
int name;
int status;
int result;
Survey(int name, int status, int result)
{
this.name=name;
this.status=status;
this.result=result;
}
}
class SurveyAdapter extends ArrayAdapter<Survey>
{
SurveyAdapter()
{
super(SurveyListActivity.this, R.layout.row, R.id.name, EU);
}
@Override
public View getView(int position, View convertView,
ViewGroup parent)
{
SurveyWrapper wrapper=null;
if (convertView==null)
{
convertView=getLayoutInflater().inflate(R.layout.row, null);
wrapper=new SurveyWrapper(convertView);
convertView.setTag(wrapper);
}
else
{
wrapper=(SurveyWrapper)convertView.getTag();
}
wrapper.populateFrom(getItem(position),position);
return(convertView);
}
}
class SurveyWrapper
{
private TextView name=null;
private ImageView status=null;
private View row=null;
SurveyWrapper(View row)
{
this.row=row;
}
TextView getName()
{
if (name==null)
{
name=(TextView)row.findViewById(R.id.name);
}
return(name);
}
ImageView getstatus()
{
if (status==null)
{
status=(ImageView)row.findViewById(R.id.flag);
}
return(status);
}
void populateFrom(Survey survey, int i)
{
if((i%2)!=0)
{
row.setBackgroundColor(Color.LTGRAY);
getName().setBackgroundColor(Color.LTGRAY);
}
getName().setText(survey.name);
getstatus().setImageResource(survey.status);
}
}
}