[英]Android Listview with active buttons which change text in the row
嗨我需要listview行中的活動按鈕的幫助。 想法是我在行中按下按鈕它改變textview。 必須在setOnItemLongClickListener ()中激活此列表行; 任何人都可以向我展示示例或教程如何制作這樣的東西或一些例子或顯示我的問題? 我厭倦谷歌它不會得到答案...我新的Android和抱歉我的語言和tnx任何幫助;)我的想法。
+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview | textview | Button |
+-----------------------------------------+
然后按下按鈕textview成為這樣的數字:
+-----------------------------------------+
| textview | textview | textview | Button |
+----------+----------+----------+--------+
| textview | textview | 1 | Button | <- this button was pressed
+-----------------------------------------+
我做了然后按鈕更改它但問題是它更改更多行。 喜歡
+-----------------------------------------+
| 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
+-----------------------------------------+
我正在使用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));
}
});
}
};
我的布局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>
您需要為列表視圖創建自定義適配器。 在這個適配器的getView()方法中,你應該設置click click for button。 按下行按鈕時,此偵聽器將調用。 在這個監聽器中,你必須得到你將要改變的行數據。 嗯..我想你應該從視圖標簽中獲取它,它在你的聽眾的onClick的參數中。 而且你應該設置這個標簽。 在下面的例子中我展示了如何..
@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;
}
}
這不是你的情況,但相同..
不確定為什么要在longclick監聽器中設置onclick監聽器。
你應該做的是創建一個自定義適配器,並在自定義適配器中覆蓋getView方法並膨脹包含
textview textview textview按鈕
然后使用獲取對按鈕和textview的引用
buttonView = inflatedView.findViewById(R.id.button);
textView = inflatedView.findViewById(R.id.textView)
並將onClicklistener綁定到此按鈕引用,該引用編輯此textView引用中的文本。
ListView
重復使用行作為優化,這就是為什么在單擊按鈕后會看到包含已更改文本的多行。
您需要為所有行設置textView
值,即使文本與布局中的文本相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.