EditText w ListView - Zapisanie wartości i znikająca klawiatura

0

Cześć,
mam następujący problem z EditTextem umieszczonym w ListView.
Mam w pojedynczym wierszu listy kilka EditTextów, które mają przyjmować wartości liczbowe. Po pierwszym kliknięciu na każdego z nich pojawia się zwykła klawiatura qwerty, która za chwilę znika. Po drugim kliknięciu pojawia się już klawiatura numeryczna. Nie wiem dlaczego tak się dzieje.

Kolejnym problem polega na tym, że gdy wpiszę wartość w dane pole i kliknę gdziekolwiek na ekran, to pole wraca do stanu początkowego (Te wartości są pobierane z bazy przy ładowaniu listy). Domyślam się, że przy każdym "unfocusie" lista ładuje dane ponownie. Pomyślałem, że muszę od razu zaktualizować dane do bazy z edytowanego pola ale nie do końca wiem jak to zrobić. W klasie adaptera nie potrafię się połączyć z bazą za pomocą klasy, którą do tego stworzyłem.

Chciałem znaleźć obiekt nasłuchujący na zdarzenie UNfocus ale nie znalazłem. Istnieje takie? Znalazłem tylko onFocusChange - być może da się z niego skorzystać aby rozwiązać mój problem. Czy to dobra droga?

Dzięki za pomoc!

Na początek xml pojedynczego wiersza z listy:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Nazwa pokoju"
        android:id="@+id/room_name"
        android:textColor="#a11b1b"
         />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Szerokość"
                android:id="@+id/textView"
                android:textColor="#a11b1b"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_marginRight="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Długość"
                android:id="@+id/textView2"
                android:textColor="#a11b1b"
                android:layout_column="16"
                android:layout_marginRight="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Wysokość"
                android:id="@+id/textView3"
                android:textColor="#a11b1b"
                android:layout_column="23"
                android:layout_gravity="right" />

            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:text="Zapisz"
                android:id="@+id/button2"
                android:layout_gravity="right"
                android:layout_column="24"
                android:layout_span="2" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/szer_e"
                android:layout_marginRight="20dp"
                android:textColor="#000000"
                android:numeric="integer|decimal"
                android:inputType="number|numberDecimal" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dlug_e"
                android:layout_column="16"
                android:layout_marginRight="20dp"
                android:textColor="#000000"
                android:inputType="number|numberDecimal"
                android:numeric="integer|decimal" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/wys_e"
                android:layout_column="23"
                android:textColor="#000000"
                android:inputType="number|numberDecimal"
                android:numeric="integer|decimal" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:text="Usuń"
                android:id="@+id/delete_btn"
                android:layout_column="25" />
        </TableRow>

    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Powierzchnia podłogi"
                android:id="@+id/textView6"
                android:textColor="#a11b1b"
                android:layout_marginRight="20dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Powierzchnia sufitu"
                android:id="@+id/textView7"
                android:textColor="#a11b1b"
                android:layout_marginRight="20dp"
                android:layout_column="1"
                android:layout_gravity="right" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/floor"
                android:layout_gravity="left"
                android:editable="true"
                android:numeric="integer" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/ceiling"
                android:layout_gravity="right" />
        </TableRow>
    </TableLayout>

</LinearLayout> 

Tak wygląda adapter a dokładniej jego metoda getView():

@Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        View v = View.inflate(context, R.layout.rooms_list_item, null);

        TextView name_ = (TextView)v.findViewById(R.id.room_name);
        EditText szer_e = (EditText)v.findViewById(R.id.szer_e);
        EditText wys_e = (EditText)v.findViewById(R.id.wys_e);
        EditText dlug_e = (EditText)v.findViewById(R.id.dlug_e);

        Button delete_btn = (Button)v.findViewById(R.id.delete_btn);
        delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(onItemDeleteClickListener != null) onItemDeleteClickListener.onClick(v);
            }
        });

        szer_e.setTag(listItems.get(position));

        
        String szer = String.valueOf(listItems.get(position).getWidth());
        String wys = String.valueOf(listItems.get(position).getHeight());
        String dlug = String.valueOf(listItems.get(position).getLength());
        String name = String.valueOf(listItems.get(position).getName());
        
        name_.setText(name);
        szer_e.setText(szer);
        wys_e.setText(wys);
        dlug_e.setText(dlug);

        delete_btn.setTag(listItems.get(position).getId());
        v.setTag(listItems.get(position).getId());

        return v;


    } 
0

Nikt nic :( ?

0

wywal edit texty z ListView. To nigdy nie jest dobry pomysł.

Zrób sobie nad listą jedną edytowalną komórkę albo dialog do edycji

0

Ze znikającą klawiaturą sobie poradziłem. Trzeba było dodać w AndroidManifest poniższy kod do aktywności z listą:
android:windowSoftInputMode="adjustPan"

oraz do samej listy dorzucić:
android:descendantFocusability="beforeDescendants"

Wojtek, już któryś raz widzę, "ostrzeżenie" przed używaniem EditTexta w ListView. Dlaczego to "nigdy nie jest dobry pomysł". Wydaje mi się, że to akurat się nadaje do tego, czego oczekuję od aplikacji. Co jest złego w tym połączeniu?

0

Właśnie to że nie masz kontroli nad focusem, bo tak naprawdę nie możesz go mieć. Zwróc uwagę co się stanie jak masz focus na jakimś elemencie , po czym zrobisz swipe tak żeby Ci przesunęło dużo elementów.

Naprawdę radzę zrobić sobie dialog do edycji komórki, a na listView nie używać edit textów. Pozbędziesz się masy kłopotu, no i nie będzie problemu czy chcesz listę przesunąć, czy akurat chciałeś na edit texta kliknąć czy jeszcze dodasz swipa do tego i w ogóle już wszystko zdurnieje

0

Hm. No tak. Rozumiem o co Ci chodzi. Z drugiej strony, już przy kilku rekordach (z punktu widzenia użytkownika) wrzucenie pól tekstowych na ListView jest dużo wygodniejsze.

Tak naprawdę na samym początku myślałem o wyskakującym okienku do edycji ale potem spodobało mi się rozwiązanie "szybsze" czyli właśnie Edity dostępne od razu...

Muszę to w takim razie jeszcze przemyśleć :) Ale mam nadzieję, że ExpandableListView zagnieżdżone w ListView nie będzie dramatem :) Potrzebuję mieć listę na obiekty, do których też przyporządkowane będzie dużo paramatrów, które chcę pokazać w formie listy - stąd pomysł na zagnieżdżenie :)

0

Nie możesz zagnieździć ExpandebleListView w ListView
Nie możesz dwóch scroll contentów jednego w drugim wrzucić

0

OK. Czyli kolejne PODrekorody powinienem dodawać pętlą z poziomu pliku Javy?

0

Nie wiem co chcesz zrobić tak naprawdę. ListView powinien być prosty i mieć tylko potrzebne informacje.

Jeżeli każdy z elementów listy ma w sobie następne list to otwórz nową aktywnośc/fragmen/dialog ze szczegółami tego elemntu i z Listą.

Jak bardzo chcesz możesz korzystać z ExpandebleListView ale z doświadczenia wiem że ludzie nie są wielkimi fanami takiego rozwązania

1 użytkowników online, w tym zalogowanych: 0, gości: 1