2016-06-21 97 views
0

this is the image of layout tile.xmlthis is the image of layout tile.xml 我已經創建了一個使用自定義適配器的列表裏面有2個textviews和2個按鈕裏面現在我想要的是改變按鈕點擊textview其中之一的可見性。改變文本視圖內容按鈕點擊內部列表視圖

我處理按鈕點擊外的定製adapter.I要切換ID爲第二TextView的可視性切換tvstatus使用ON和OFF按鈕。

這是customadapter

package slide.apptech.com.rpiconnect; 

/** 
* Created by MOHIT on 09-06-2016. 
*/ 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 

//custom adapter class extends a arrayadapter 
public class customadapter2 extends ArrayAdapter<String> { 
    private final Context context; 
    private final ArrayList values; 
    private String stv = "Ststus"; 

    public customadapter2(Context context, ArrayList values) { 

     //for super constructor pass 
     // context files 
     //layout file required for list 
     //arraylist that has strings to be displayed 
     super(context, R.layout.tile, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final int pos = position; 
     final ViewGroup par = parent; 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.tile, parent, false); 
     final TextView textView = (TextView) rowView.findViewById(R.id.tvappname); 
     Button on = (Button) rowView.findViewById(R.id.bon); 
     Button off = (Button) rowView.findViewById(R.id.boff); 
     final TextView status = (TextView) rowView.findViewById(R.id.tvstatus); 

     on.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       status.setVisibility(View.VISIBLE); 
       notifyDataSetChanged(); 
       ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick() 

      } 
     }); 

     off.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) ; 
       ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick() 
      } 
     }); 

     //get(position method is used to access the elements of arraylist) 
     String val = (String) values.get(position); 
     textView.setText(val); 
     // Change the icon for Windows and iPhone 
     String s = (String) values.get(position); 

     return rowView; 
    } 

    public void myClickHandler(View v) 
    { 

    } 
} 

這是我的XML文件中的代碼,我使用的listelement

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 


    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/card_view" 
     android:layout_gravity="center" 
     android:layout_width="match_parent" 
     android:layout_margin="2dp" 
     android:layout_height="100dp" 
     card_view:cardCornerRadius="4dp" 
     android:layout_alignParentStart="true" 
     android:background="#611818" 
     android:paddingBottom="10dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="OFF" 
      android:id="@+id/boff" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="37dp" 
      android:focusable="false"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/bon" 
      android:text="ON" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginEnd="28dp" 
      android:focusable="false"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Ttile" 
      android:id="@+id/tvappname" 
      android:textSize="20sp" 
      android:layout_above="@+id/boff" 
      android:layout_centerHorizontal="true" 
      android:textColor="@color/abc_input_method_navigation_guard" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="ON" 
       android:id="@+id/tvstatus" 
       android:layout_below="@+id/tvappname" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="20dp" 
       android:textColor="#060505" 
       android:visibility="invisible"/> 


     </RelativeLayout> 
    </android.support.v7.widget.CardView> 


</RelativeLayout> 

請評論,如果您想要的任何其他文件

回答

0

無碼,不要這樣做。直接更改listview中任何視圖的值是一個壞主意。只需在您的數組列表中進行更改,然後調用notifyDataSetChanged()以反映更改。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final int pos = position; 
    final ViewGroup par = parent; 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.tile, parent, false); 
    final TextView textView = (TextView) rowView.findViewById(R.id.tvappname); 
    Button on = (Button) rowView.findViewById(R.id.bon); 
    Button off = (Button) rowView.findViewById(R.id.boff); 
    final TextView status = (TextView) rowView.findViewById(R.id.tvstatus); 
    //get(position method is used to access the elements of arraylist) 
    String val = (String) values.get(position); 
    textView.setText(val); 
    if(val.equalsIgnoreCase("off")){ 
     status.setVisibility(View.INVISIBLE); 
    }else{ 
     status.setVisibility(View.VISIBLE); 
    } 
    on.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      values.set(position, "On"); 
      notifyDataSetChanged(); 
      ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick() 
     } 
    }); 

    off.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      values.set(position, "Off"); 
      notifyDataSetChanged(); 
      ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick() 
     } 
    }); 

    // Change the icon for Windows and iPhone 
    String s = (String) values.get(position); 

    return rowView; 
} 

希望這將有助於:)

+0

我相信你正在使用ArrayList的保持狀態,如果狀態爲關閉,那麼你要隱藏你的EDITTEXT? – Neo

+0

我不使用ArrayList中存儲有默認值「開」的狀態沒有看到XML文件 – M2skills

+0

右鍵,然後更改知名度,通過檢查你的狀態。如果它關閉,則使其不可見,否則使其可見。對於在關閉按鈕的點擊改變你的ArrayList和notifyDatasetchange()的值,等我在你的代碼進行補正:) – Neo

相關問題