Android 如何在我的自定义列表视图中找到微调器位置

发布于 2024-12-13 23:05:21 字数 3443 浏览 0 评论 0原文

我有扩展 BaseAdapter 的简单 ListView。我的每一行都有一个旋转器和按钮。如何在 ListView 中找到微调器的位置,以便我必须获取每个微调器的 selectedItem() ?

private class MyCustomAdapter extends BaseAdapter {
private Activity activity;
private String[] estimated, price, image;
private  LayoutInflater inflater=null;
public ViewHolder holder;
public ProductImageLoader imageLoader;
 private ArrayList<String> arraylist4;
SQLiteDatabase db;
View vi;

public MyCustomAdapter(SubMenu subMenu, String[] estimated, String[] price, String[] image) {
// TODO Auto-generated constructor stub
this.activity = subMenu;
this.estimated = estimated;
this.price = price;
this.image = image;
inflater =    (LayoutInflater)getParent().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ProductImageLoader(activity.getParent());

}
@Override
public int getCount() {
// TODO Auto-generated method stub
return estimated.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

 @Override
 public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
 }
 public  class ViewHolder{
public TextView text;
public TextView text1;
public ImageView image_view;
public Button button, add_btn;
public Spinner spinner;
}
@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  vi=convertView;

 if(convertView==null){

    vi = inflater.inflate(R.layout.submenu_items, null);
    holder=new ViewHolder();
    holder.text=(TextView)vi.findViewById(R.id.food_name);
    holder.text1=(TextView)vi.findViewById(R.id.prize);
    holder.image_view=(ImageView)vi.findViewById(R.id.imageview);
    holder.button=(Button)vi.findViewById(R.id.detail_btn);
    holder.add_btn=(Button)vi.findViewById(R.id.addorder_btn);
    holder.spinner=(Spinner)vi.findViewById(R.id.spinner);
    vi.setTag(holder);
  }
 else
    holder=(ViewHolder)vi.getTag();

holder.text.setText(estimated[position]);
holder.text1.setText(price[position]);
holder.image_view.setTag(image[position]);
imageLoader.DisplayImage(image[position], activity, holder.image_view);
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString("url", image[position]);
bundle.putString("description", description[position]);
Log.d("url",""+description[position]);

Intent intent = new Intent(SubMenu.this,FoodDetailPage.class);
intent.putExtras(bundle);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("FoodDetailPage", intent);
}

});
holder.add_btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.d("row spinner", ""+holder.spinner.getChildAt(position));
        try {
            db = SQLiteDatabase.openDatabase("data/data/com.android.restaurant1   /Restaurant", null, SQLiteDatabase.CREATE_IF_NECESSARY);
            db.beginTransaction();
            db.execSQL("insert into table1(MenuName, Count, Price)  values('"+estimated[position]+"', '"+holder.spinner.getSelectedItem()+"', '"+price[position]+"')");
            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
        }
        catch(SQLException e) {

        }
    }
});
return vi;

}

I have simple ListView that extends BaseAdapter. I have a spinner and button in each row. How to find the position of the spinner in the ListView so that i have to get each spinner's selectedItem() ??

private class MyCustomAdapter extends BaseAdapter {
private Activity activity;
private String[] estimated, price, image;
private  LayoutInflater inflater=null;
public ViewHolder holder;
public ProductImageLoader imageLoader;
 private ArrayList<String> arraylist4;
SQLiteDatabase db;
View vi;

public MyCustomAdapter(SubMenu subMenu, String[] estimated, String[] price, String[] image) {
// TODO Auto-generated constructor stub
this.activity = subMenu;
this.estimated = estimated;
this.price = price;
this.image = image;
inflater =    (LayoutInflater)getParent().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ProductImageLoader(activity.getParent());

}
@Override
public int getCount() {
// TODO Auto-generated method stub
return estimated.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

 @Override
 public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
 }
 public  class ViewHolder{
public TextView text;
public TextView text1;
public ImageView image_view;
public Button button, add_btn;
public Spinner spinner;
}
@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  vi=convertView;

 if(convertView==null){

    vi = inflater.inflate(R.layout.submenu_items, null);
    holder=new ViewHolder();
    holder.text=(TextView)vi.findViewById(R.id.food_name);
    holder.text1=(TextView)vi.findViewById(R.id.prize);
    holder.image_view=(ImageView)vi.findViewById(R.id.imageview);
    holder.button=(Button)vi.findViewById(R.id.detail_btn);
    holder.add_btn=(Button)vi.findViewById(R.id.addorder_btn);
    holder.spinner=(Spinner)vi.findViewById(R.id.spinner);
    vi.setTag(holder);
  }
 else
    holder=(ViewHolder)vi.getTag();

holder.text.setText(estimated[position]);
holder.text1.setText(price[position]);
holder.image_view.setTag(image[position]);
imageLoader.DisplayImage(image[position], activity, holder.image_view);
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString("url", image[position]);
bundle.putString("description", description[position]);
Log.d("url",""+description[position]);

Intent intent = new Intent(SubMenu.this,FoodDetailPage.class);
intent.putExtras(bundle);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("FoodDetailPage", intent);
}

});
holder.add_btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.d("row spinner", ""+holder.spinner.getChildAt(position));
        try {
            db = SQLiteDatabase.openDatabase("data/data/com.android.restaurant1   /Restaurant", null, SQLiteDatabase.CREATE_IF_NECESSARY);
            db.beginTransaction();
            db.execSQL("insert into table1(MenuName, Count, Price)  values('"+estimated[position]+"', '"+holder.spinner.getSelectedItem()+"', '"+price[position]+"')");
            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
        }
        catch(SQLException e) {

        }
    }
});
return vi;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原野 2024-12-20 23:05:21

在您的自定义适配器类中,尝试在 getView() 方法中实现旋转侦听器并将其存储到一个整数数组中。

   final int[] positions=new int[2]; //[0] for listitem position, [1]  for spinner postion
        Spinner sp=findViewByID(R.id.spinner);

        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                positions[0]=listItemSelectedposition;
                positions[1]=arg2;


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

In your custom adapter class ,try to implement spinner listener in getView() method and stores into one integer array.

   final int[] positions=new int[2]; //[0] for listitem position, [1]  for spinner postion
        Spinner sp=findViewByID(R.id.spinner);

        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                positions[0]=listItemSelectedposition;
                positions[1]=arg2;


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
梦里°也失望 2024-12-20 23:05:21
Spinner user_type user_type = (Spinner) findViewById(R.id.spinner1);
user_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {                           
    Log.i("Position ", "is "+pos);
  }

  public void onNothingSelected(AdapterView<?> parent) {
  }
});
Spinner user_type user_type = (Spinner) findViewById(R.id.spinner1);
user_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {                           
    Log.i("Position ", "is "+pos);
  }

  public void onNothingSelected(AdapterView<?> parent) {
  }
});
我ぃ本無心為│何有愛 2024-12-20 23:05:21

您必须在 getView 方法中使用以下代码:

int spinnerPosition;

user_type
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int pos, long id) {
                            spinnerPosition=pos;
                            String usertype;
                            usertype = parent.getItemAtPosition(spinnerPosition).toString();


                        Log.i("Position ", "is "+spinnerPosition);
                        Log.i("Item ", "is "+usertype);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });

然后您可以在这行代码中使用 spinnerPosition:

Log.d("row spinner", ""+holder.spinner.getChildAt(spinnerPosition));

以及其他地方也可以。

You have to use the below code within the getView method:

int spinnerPosition;

user_type
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int pos, long id) {
                            spinnerPosition=pos;
                            String usertype;
                            usertype = parent.getItemAtPosition(spinnerPosition).toString();


                        Log.i("Position ", "is "+spinnerPosition);
                        Log.i("Item ", "is "+usertype);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });

And then you can use spinnerPosition at this line of code:

Log.d("row spinner", ""+holder.spinner.getChildAt(spinnerPosition));

And other places also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文