2013-08-29 18 views
16

Napisałem swój niestandardowy widok i chcę zaktualizować niektóre inne widoki po interakcji z moim widokiem niestandardowym.Android findViewById() w widoku niestandardowym

Główny Układ:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/display_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:layout_centerHorizontal="true" 
     android:tag="name" 
     android:ems="10" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="number" 
     android:ems="10" 
     android:id="@+id/id_number" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/display_name"/> 

    <ge.altasoft.custom_views.IdNumber 
     android:id="@+id/custom_id_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/id_number" 
     android:paddingLeft="35dip" 
     custom:firstName="@id/display_name"/> 
</RelativeLayout> 

Widok niestandardowy układ:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/id_number_custom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="number" 
     android:ems="10" 
     android:paddingRight="35dip" /> 

    <ImageButton 
     android:id="@+id/load_data_button" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:layout_centerVertical="true" 
     android:src="@drawable/load_data" 
     android:layout_toRightOf="@id/id_number_custom" /> 

</RelativeLayout> 

Widok niestandardowy klasy, konstruktor i słuchacz:

private int firstNameViewID; 

public IdNumber (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initViews(); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber); 
     final int N = a.getIndexCount(); 
     for(int i = 0; i < N; i++){ 
      int attr = a.getIndex(i); 
      switch(attr){ 
       case R.styleable.IdNumber_firstName: 
        firstNameViewID = a.getResourceId(attr, -1); 
        break; 
      } 
     } 
     a.recycle(); 
    } 



private void initViews() { 
     inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.id_number_edit_text_custom, this, true); 
     editText = (EditText) findViewById(R.id.id_number_custom); 
     loadButton = (ImageButton) findViewById(R.id.load_data_button); 
     loadButton.setVisibility(RelativeLayout.INVISIBLE); 
     loadData(); 
    } 

private void loadData(){ 
     loadButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       EditText firstName = (EditText) findViewById(R.id.display_name); 
       firstName.setText("Some Text"); 
      } 
     }); 
    } 

Problem polega na tym, że EditText firstName = (EditText) findViewById(R.id.display_name); zwraca wartość null. Wiem, że po prostu wywołanie findViewById() przeszuka widok w układzie, który nadmuchałem.

Jeśli to możliwe, jak mogę uzyskać widok EditText o id: display_name z głównego layoutu?

Dzięki z góry.

+0

Nie, nie chcę uzyskać mojego widoku niestandardowego, chcę uzyskać inny widok z mainLayout, o identyfikatorze diplay_name. – Jilberta

+0

Nadpisujesz xml, ale nie zapisujesz zwróconego widoku. możesz findview by id w tym widoku. – Siddhesh

Odpowiedz

11
View Custmv; 

private void initViews() { 
     inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     Custmv = inflater.inflate(R.layout.id_number_edit_text_custom, this, true); 
     editText = (EditText) findViewById(R.id.id_number_custom); 
     loadButton = (ImageButton) findViewById(R.id.load_data_button); 
     loadButton.setVisibility(RelativeLayout.INVISIBLE); 
     loadData(); 
    } 

private void loadData(){ 
     loadButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       EditText firstName = (EditText) Custmv.getParent().findViewById(R.id.display_name); 
       firstName.setText("Some Text"); 
      } 
     }); 
    } 

Spróbuj tego.

+0

Nie dostałeś tego @Siddesh, editText = (EditText) findViewById (R.id.id_number_custom); loadButton = (ImageButton) findViewById (R.id.load_data_button); działa to dobrze, mogę pobierać widoki z układu CustomView, chcę uzyskać widoki z MainLayout. – Jilberta

+0

ok, ale dlaczego chcesz obsłużyć główny układ z niestandardowej klasy widoku? – Siddhesh

+0

, ponieważ chcę zaktualizować te widoki po interakcji z moim niestandardowym widokiem. – Jilberta

1

zmienić metodę w następujący sposób i sprawdzić to będzie działać

private void initViews() { 
     inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.id_number_edit_text_custom, this, true); 
View view = (View) inflater.inflate(R.layout.main, null); 
     editText = (EditText) view.findViewById(R.id.id_number_custom); 
     loadButton = (ImageButton)view. findViewById(R.id.load_data_button); 
     loadButton.setVisibility(RelativeLayout.INVISIBLE); 
     loadData(); 
    } 
+0

Próbowałem tego Usman, ale to nie działa, widok zwraca zerowy. – Jilberta

+0

Nie ma potrzeby rzutowania na widok, gdy LayoutInflater.inflate zwraca widok. – vilpe89

0

Jeśli jest to stały układ można zrobić tak:

public void onClick(View v) { 
    ViewGroup parent = (ViewGroup) IdNumber.this.getParent(); 
    EditText firstName = (EditText) parent.findViewById(R.id.display_name); 
    firstName.setText("Some Text"); 
} 

Jeśli chcesz znaleźć EditText w elastyczny układ, będę pomóż ci później. Mam nadzieję, że to pomoże.

+0

Element nadrzędny ma wartość null, pod warunkiem, że nadmuchuję układ, który reprezentuje widok niestandardowy. – Jilberta

0
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    ImageHolder holder = null; 
    if (row == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 
     holder = new ImageHolder(); 
     editText = (EditText) row.findViewById(R.id.id_number_custom); 
     loadButton = (ImageButton) row.findViewById(R.id.load_data_button); 
     row.setTag(holder); 
    } else { 
     holder = (ImageHolder) row.getTag(); 
    } 


    holder.editText.setText("Your Value"); 
    holder.loadButton.setImageBitmap("Your Bitmap Value"); 
    return row; 
} 
+1

Myślę, że odpowiadasz na inne pytanie tutaj. . . – Jilberta

+0

Jeśli otrzymasz błąd jak pusty wskaźnik, to popełniasz błąd podczas inicjalizacji edittext i Imagebutton zgodnie z kodem ... –

+0

problem nie jest w editText ani w imageButton – Jilberta

0

Spróbuj to w konstruktorze

MainActivity maniActivity = (MainActivity)context; 
EditText firstName = (EditText) maniActivity.findViewById(R.id.display_name); 
0

Można spróbować czegoś takiego:

Inside konstruktora CustomView:

mContext = context; 

Następny wewnątrz CustomView można nazwać:

((MainActivity) mContext).updateText(text); 

Wewnątrz MainAcivity określić:

public void updateText(final String text) { 

    TextView txtView = (TextView) findViewById(R.id.text); 
    txtView.setText(text); 
} 

To działa na mnie.

+0

wywołanie 'findViewById()' kilka razy jest złe i nie jest wydajny. Jeśli zamierzasz używać go wielokrotnie, przechowuj odniesienie do niego w polu "final". –

Powiązane problemy