简体   繁体   English

带有活动按钮的Android Listview,用于更改行中的文本

[英]Android Listview with active buttons which change text in the row

Hi I need help with active buttons in listview row. 嗨我需要listview行中的活动按钮的帮助。 Idea is then i pressed button in the row it change textview. 想法是我在行中按下按钮它改变textview。 This list row must be avtivated in setOnItemLongClickListener (); 必须在setOnItemLongClickListener ()中激活此列表行; Can anyone show me example or tutorial how make something like this or some example or show there is my problem? 任何人都可以向我展示示例或教程如何制作这样的东西或一些例子或显示我的问题? I tired for google it and wont get answer... I new in android and sorry for my language and tnx for any help ;) my Idea. 我厌倦谷歌它不会得到答案...我新的Android和抱歉我的语言和tnx任何帮助;)我的想法。

+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview | textview | Button |
+-----------------------------------------+

Then button pressed textview become number like this: 然后按下按钮textview成为这样的数字:

+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview |     1    | Button | <- this button was pressed
+-----------------------------------------+

I made then button change it but problem is it change more rows. 我做了然后按钮更改它但问题是它更改更多行。 Like 喜欢

+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview |     1    | Button | <- this button was pressed
+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview |     1    | Button | <- this button was **not** pressed
+----------+----------+----------+--------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview |     1    | Button | <- this button was **not** pressed
+-----------------------------------------+

I'm using SimpleAdapter to get list. 我正在使用SimpleAdapter来获取列表。

ListView lv = getListView();   
lv.setOnItemLongClickListener(listener);

OnItemLongClickListener listener = new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) {
        btn1 = (Button) view.findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener(){
            int number= 0;
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView textView = (TextView) v.findViewById(R.id.textView_must_be_changed);
                number++;
                textView.setText(String.valueOf(number)); 
            }                   
        });
    }
};

My Layout XML is 我的布局XML是

  <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:id="@+id/gridas"
      android:layout_height="fill_parent"
      android:choiceMode="multipleChoice"
      android:columnCount="10"
      android:descendantFocusability="beforeDescendants" >


      <TextView
          android:id="@+id/Textview"
          android:layout_column="5"
          android:layout_columnSpan="3"
          android:layout_gravity="left|bottom"
          android:layout_row="1"
          android:layout_rowSpan="2"
          android:text="4.3 kg" />

      <TextView
          android:id="@+id/Textview"
          android:layout_column="7"
          android:layout_gravity="left"
          android:layout_row="1"
          android:layout_rowSpan="2"
          android:text="35 cm" />



      <TextView
          android:id="@+id/Textview"
          android:layout_width="163dp"
          android:layout_height="49dp"
          android:layout_column="9"
          android:layout_gravity="left"
          android:layout_row="2"
          android:gravity="left"
          android:text="949.00 Lt"
          android:textSize="32sp"
          android:textStyle="bold" />



      <TextView
          android:id="@+id/**textView_must_be_changed**"
          android:layout_column="9"
          android:layout_gravity="left|top"
          android:layout_row="1"
          android:text="tekstukas"
           />

      <Button
          android:id="@+id/button1"
          android:layout_column="9"
          android:layout_gravity="left|top"
          android:layout_row="3"
          android:text="Button" 
          android:focusable="false"
          android:focusableInTouchMode="false"/>

  </GridLayout>    

you need to create custom adapter for your list view. 您需要为列表视图创建自定义适配器。 And in getView() method of this adapter your should set on click listener for button. 在这个适配器的getView()方法中,你应该设置click click for button。 This listener will call when you've pressed row button. 按下行按钮时,此侦听器将调用。 And in this listener you have to get row data which you will change. 在这个监听器中,你必须得到你将要改变的行数据。 Mm.. I think you should get it from tag of view, which in parametor of onClick of your listener. 嗯..我想你应该从视图标签中获取它,它在你的听众的onClick的参数中。 And also you should set this tag. 而且你应该设置这个标签。 In following example I show how.. 在下面的例子中我展示了如何..

@Override
    public View getView( int position, View convertView, ViewGroup parent ){
        PhoneContactListItem item = getItem( position );
        TextView contactName;
        TextView contactPhone;
        CheckBox isCheckedCheckBox;

        if( convertView == null ) {
            convertView = LayoutInflater.from( context).inflate( R.layout.project_contact_list_item_layout, null );
            contactName = (TextView) convertView.findViewById( R.id.projectContactRow_contactName );
            contactPhone = (TextView) convertView.findViewById( R.id.projectContactRow_contactPhone );
            isCheckedCheckBox = (CheckBox) convertView.findViewById( R.id.projectContactRow_checkBox );

            convertView.setTag( new ViewHolder( contactName, contactPhone, isCheckedCheckBox ) );

            //setting on click listener for isCheckedCheckBox
            isCheckedCheckBox.setOnClickListener( new OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox)v;
                    PhoneContactListItem i = (PhoneContactListItem)cb.getTag();
                    i.toggleChecked();
                    cb.setChecked( i.isChecked() );
                }               
            });
        } else {
            ViewHolder vh = (ViewHolder)convertView.getTag();
            contactName = vh.getContactName();
            contactPhone = vh.getContactPhone();
            isCheckedCheckBox = vh.getIsCheckedCheckBox();
        }

        //initializing views
        isCheckedCheckBox.setTag( item );
        contactName.setText( item.getPhoneContact().getContactName() );
        contactPhone.setText( item.getPhoneContact().getContactPhone() );

        //initializing checkbox
        if( item.isChecked() )
            isCheckedCheckBox.setChecked( true );
        else 
            isCheckedCheckBox.setChecked( false );

        //enabling or disabling check box
        if( item.isCheckDisabled() ) 
            isCheckedCheckBox.setClickable( false );
        else 
            isCheckedCheckBox.setClickable( true );

        //showing checkboxes
        if( showCheckBoxes ) 
            isCheckedCheckBox.setVisibility( View.VISIBLE );
        else 
            isCheckedCheckBox.setVisibility( View.GONE );

        return convertView;
    }

    private class ViewHolder {
        private TextView contactName;
        private TextView contactPhone;
        private CheckBox isCheckedCheckBox;

        public ViewHolder( TextView contactName, TextView contactPhone, CheckBox isCheckedCheckBox ) {
            this.contactName = contactName;
            this.contactPhone = contactPhone;
            this.isCheckedCheckBox = isCheckedCheckBox;
        }

        /*
         * getters
         */
        public TextView getContactName() {
            return contactName;
        }
        public TextView getContactPhone() {
            return contactPhone;
        }
        public CheckBox getIsCheckedCheckBox() {
            return isCheckedCheckBox;
        }
    }

it's not your situation but the same.. 这不是你的情况,但相同..

Not really sure why you are setting an onclick listener inside your longclick listener. 不确定为什么要在longclick监听器中设置onclick监听器。

What you should be doing is creating a custom adapter and inside your custom adapter override the getView method and inflate the xml file which contains the 你应该做的是创建一个自定义适配器,并在自定义适配器中覆盖getView方法并膨胀包含

textview textview textview button textview textview textview按钮

then get references to the button and textview using 然后使用获取对按钮和textview的引用

buttonView = inflatedView.findViewById(R.id.button);
textView = inflatedView.findViewById(R.id.textView)

and bind an onClicklistener to this button reference which edits the text in this textView reference. 并将onClicklistener绑定到此按钮引用,该引用编辑此textView引用中的文本。

ListView re-use rows as an optimization, that's why you see several row with the changed text once you clicked on a button. ListView重复使用行作为优化,这就是为什么在单击按钮后会看到包含已更改文本的多行。

You need to set your textView value for all rows, even if the text is the same as the one in your layout. 您需要为所有行设置textView值,即使文本与布局中的文本相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM