Android ListView:getTag() 返回 null

发布于 2024-09-05 06:22:52 字数 1480 浏览 3 评论 0原文

大家好,

我有一个 ListView,其中每行都包含一个 Button。以下代码是

public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        TextView tv;
        Button saveA_button;
        EditText edittext;
        FITB_ViewWrapper wrapper;

  if (row == null) {
       LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Training")) { 
             row = li.inflate(R.layout.exercise_for_training_fitb,parent, false);
       }else {
              row = li.inflate(R.layout.exercise_for_exam_fitb,parent, false);
       }
       wrapper=new FITB_ViewWrapper(row);
       row.setTag(wrapper);
       if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Exam")) {
           saveA_button=wrapper.getSaveAnswer_Button();
           OnClickListener l=new OnClickListener() {

               @Override
               public void onClick(View v) {
                     Integer mp=(Integer)v.getTag();
                     Log.i("mp","my Position is: "+mp);
               }
          };
          saveA_button.setOnClickListener(l);
       }
   }else {
           wrapper=(FITB_ViewWrapper) row.getTag();
 }

我的应用程序的 getView() 方法的一部分,我需要知道按钮属于哪个项目,所以我尝试检测它。代码

Log.i("mp","my Position is: "+mp);

输出一条消息: mp myPosition is: null

我不明白,为什么我得到一个“null”而不是一个 Integer?如何找出 ListView 中项目的位置?

多谢。

Hallo all,

I have a ListView which contains a Button in each line. The following code is part of the getView() Method

public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        TextView tv;
        Button saveA_button;
        EditText edittext;
        FITB_ViewWrapper wrapper;

  if (row == null) {
       LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Training")) { 
             row = li.inflate(R.layout.exercise_for_training_fitb,parent, false);
       }else {
              row = li.inflate(R.layout.exercise_for_exam_fitb,parent, false);
       }
       wrapper=new FITB_ViewWrapper(row);
       row.setTag(wrapper);
       if (ChooseMode_Act.modeInfo.equalsIgnoreCase("Exam")) {
           saveA_button=wrapper.getSaveAnswer_Button();
           OnClickListener l=new OnClickListener() {

               @Override
               public void onClick(View v) {
                     Integer mp=(Integer)v.getTag();
                     Log.i("mp","my Position is: "+mp);
               }
          };
          saveA_button.setOnClickListener(l);
       }
   }else {
           wrapper=(FITB_ViewWrapper) row.getTag();
 }

For my App i need to known to which item the Button belongs to, so i try to detect it. The code

Log.i("mp","my Position is: "+mp);

puts out a message: mp myPosition is: null

I can't understand, why do i get a "null" but not an Integer? How can i find out the Position of an Item in a ListView?

Thanks a lot.

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

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

发布评论

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

评论(2

蓝海似她心 2024-09-12 06:22:52

Log.i("mp","我的位置是:"+position);

你已经有这个位置了!

public View getView(final int position, View convertView, ViewGroup parent) {

Log.i("mp","my Position is: "+position);

you have the position already !

public View getView(final int position, View convertView, ViewGroup parent) {
爱*していゐ 2024-09-12 06:22:52

当您上下滚动时,ListView 中的视图会被重用。因此,在 getView 中设置值通常会产生意想不到的后果,例如您打算为项目编号 5 设置的图像也出现在项目编号 10、15 和 20 中。

为了避免这种情况,如果您想在 getView 中设置属性,您需要确保为每个视图设置或取消设置它们。

我不确定您到底想用代码完成什么,但将 setTag 移到 if 语句之外可能会有所帮助,以确保每次使用视图时都对其进行设置。

The Views in a ListView are reused as you scroll up and down. Thus, setting values in getView often has unintented consequences, like the image that you meant to set for item number 5 appearing in item number 10, 15 and 20 also.

To avoid this, if you want to set properties in getView you need to make sure you set or unset them for each view.

I'm not sure what exactly you are trying to accomplish with your code, but it might help to move the setTag outside of the if statement, to make sure you are setting it each time that a view is used.

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