点击自定义ListView
中的项目时出现问题,其中每行包含ToggleButton
,Button
,TextView
和Spinner
。问题:当我单独点击每个视图时,如何为每个视图调用不同的操作?我的代码:
public class CustomListAdapter extends BaseAdapter {
Context _context;
private ArrayList<String> _list;
public CustomListAdapter(Context context,
ArrayList<String> listItems) {
_context= context;
_list = listItems;
}
public int getCount(){
return _list.size();
}
public long getItemId(int position){
return position;
}
public Object getItem(int position){
return _list.get(position);
}
public View getView(int position,View convertView,ViewGroup parent)
{
ViewContainer container = new ViewContainer();
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.elements,parent, false);
convertView.setTag(container);
convertView.setClickable(true);
container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
container.but=(Button)convertView.findViewById(R.id.button1);
convertView.setOnClickListener(new OnClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
**How can I do that when I click each view individually, different action is called for each view???**
}
public void onClick(View v) {
}
}
);
}
container = (ViewContainer) convertView.getTag();
ToggleButton myListIcon = container.listIcon;
myListIcon.setChecked(true);
Button MyButton=container.but;
TextView myListText = container.listText;
myListText.setText(_list.get(position));
Spinner MySpinner=container.spinner;
MySpinner.setAdapter(adapter);
container = null;
return convertView;
}
private class ViewContainer
{
private ToggleButton listIcon;
private TextView listText;
private Spinner spinner;
private Button but;
}
}
答案 0 :(得分:3)
我假设你在Listview上有按钮,textview和..你需要在listview上为按钮编写动作。
这是编码格式:
.....
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.calllog_row, null);
button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener(this);
}
else {
view = convertView;
}
Button1 = (Button) view.findViewById(R.id.Button1);
Button1.setTag(position); // this is important
return view;
}
public void onClick(View v) {
Integer position = (Integer) v.getTag();
switch (v.getId()) {
case R.id.button1:
Log.e("click position ", "" + position);
break;
}
......
如果我的假设是正确的,我的编码对你有帮助。
答案 1 :(得分:0)
而不是为OnClickListener
添加convertView
添加每个View
的侦听器,而不需要为您需要执行不同操作的行添加<{1}}:
//...
container.listIcon.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//action for the toggle Button
}
});
container.but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//action for the simple Button
}
});
container.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//something was selected on the spinner
}
public void onNothingSelected(AdapterView parent) {
// for interface
}
});
//...
答案 2 :(得分:0)
你需要修改你的getView,就像我在下面的代码中所示。
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ViewContainer container;
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(_context);
convertView = inflater.inflate(R.layout.elements,parent, false);
container = new ViewContainer();
//convertView.setClickable(true);
container.listIcon = (ToggleButton)convertView.findViewById(R.id.togle_item);
container.listText = (TextView)convertView.findViewById(R.id.text_listitem);
container.spinner=(Spinner)convertView.findViewById(R.id.spinner1);
container.but=(Button)convertView.findViewById(R.id.button1);
convertView.setTag(container);
}else{
container=convertView.getTag();
}
//Now for set Click event for your Induavidual item in list you can do like below
//use postion to set id of each item
container.but.setId(position);
container.but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//This will give you position of item
int id=v.getId();
}
});
//same you can do for spinner and textview and ToggleButton ,use appropriate method that you want to use
return convertView;
}
答案 3 :(得分:0)
vh.orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ---Starting new intent
Intent in = new Intent(activity.getApplicationContext(), ActivityOrder.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ;
// ---sending product no to next activity
in.putExtra("product_category", productNo);
in.putExtra("business_id", businessId);
// ---starting new activity and expecting some response back
activity.getApplicationContext().startActivity(in);
Toast.makeText(activity, productNo + " " + businessId, Toast.LENGTH_SHORT).show();
}
});