2012-05-22 8 views
6

Próbuję wypełnić listę, w której wiersze zawierają pole wyboru, cztery widoki tekstu i dwie obroty. Wartości w przędzarkach w każdym rzędzie muszą być różne, ponieważ odnoszą się bezpośrednio do rzeczywistej pozycji w tym wierszu. Aby to nieco bardziej skomplikować, spinnery i dwa pola tekstowe obok nich nie są widoczne (setVisibility (View.GONE)), chyba że pole wyboru jest zaznaczone. Chcę tylko kliknąć pozycję listy (nie pole wyboru), a błystki muszą być klikalne, gdy zostaną wyświetlone.Android onListItemClick nie działa, gdy włączana jest spinner w obrębie listy

Cały mój kod działa poprawnie, z wyjątkiem tego, że gdy zapełni się błystki, tracę możliwość kliknięcia elementu listy w tym wierszu. Wciąż mogę wybrać wartości z przędzarek w tym rzędzie, ale dotknięcie nigdzie indziej w rzędzie nic nie robi. Istnieje kilka wierszy, które nie zwracają danych do spinnerów i działają poprawnie, więc uważam, że ma to coś wspólnego z adapterami podłączonymi do spinnerów.

Wiem również, że będę musiał wziąć pod uwagę recykling listy, ale nie mam jeszcze tak daleko.

Mam nadzieję, że ktoś może wskazać, co jest nie tak, zanim przejdę do szaleństwa.

Oto XML ListView

<ListView android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

<TextView android:id="@+id/android:empty" 
      android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/empty"/> 
</LinearLayout> 

A oto XML rzędzie

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <CheckBox android:id="@+id/pl_selected" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:focusableInTouchMode="false" 
      android:clickable="false" 
      android:focusable="false" 
      android:layout_weight="1"/> 

     <TextView android:id="@+id/name" 
       android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:focusableInTouchMode="false" 
     android:clickable="false" 
     android:focusable="false"/> 

    <TextView android:id="@+id/name2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:focusableInTouchMode="false" 
     android:clickable="false" 
     android:focusable="false"/> 

</LinearLayout> 

<LinearLayout android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:id="@+id/pl_tv1" 
      android:text="@string/pl_tv1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:focusableInTouchMode="false" 
      android:clickable="false" 
      android:focusable="false" 
      android:visibility="gone"/> 

    <Spinner android:id="@+id/pl_spin1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:visibility="gone" 
      android:focusableInTouchMode="false" 
      android:focusable="false"/> 
</LinearLayout> 
<LinearLayout android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:id="@+id/pl_tv2" 
      android:text="@string/pl_tv2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:focusableInTouchMode="false" 
      android:clickable="false" 
      android:focusable="false" 
      android:visibility="gone"/> 

    <Spinner android:id="@+id/pl_spin2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:visibility="gone" 
      android:focusableInTouchMode="false" 
      android:focusable="false"/> 
</LinearLayout> 
</LinearLayout> 

A oto aktywność w większości nieistotne rzeczy wyjęte:

import android.widget.SimpleCursorAdapter; 
import mdhsoft.band.Tab.DbAdapter; 
import mdhsoft.band.Tab.R; 
import android.os.Bundle; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.ListView; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class PLActivity extends ListActivity { 
    private static final int DONE_ID = Menu.FIRST; 
    private DbAdapter mDbHelper; 
private int mPlId; 
private CheckBox mCheckBox; 
private Spinner mSpin1; 
private Spinner mSpin2; 
private TextView mtv1; 
private TextView mtv2; 
private int mItemId; 
private SimpleCursorAdapter mAllItems; 
private Cursor mSCursor; 
private Cursor mcurSpin1; 
private Cursor mcurSpin2; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pl_list); 

     Bundle extras = getIntent().getExtras(); 
     mPlId = extras.getInt(DbAdapter.KEY_PLID); 
     mDbHelper = new DbAdapter(this); 
     mDbHelper.open(); 

     fillData(); 
     registerForContextMenu(getListView()); 
    } 

private void fillData() { 
    mSCursor = mDbHelper.fetchAllPl(mPlId); 
    startManagingCursor(mSCursor); 
    String[] from = new String[] 
     {"pl_selected", DbAdapter.KEY_SNAME, DbAdapter.KEY_SNAME2}; 
    int[] to = new int[]{R.id.pl_selected, R.id.name, R.id.name2}; 
    mAllItems = new SimpleCursorAdapter(this, R.layout.pl_row, mSCursor, from, to); 
    mAllItems.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      if(columnIndex == 3) { 
        CheckBox cb = (CheckBox) view; 
        cb.setChecked(cursor.getInt(3) > 0); 
        ViewGroup row = (ViewGroup)view.getParent(); 
        if (cursor.getInt(3) > 0) { 
         mcurSpin1 = (Spinner) row.findViewById(R.id.pl_spin1); 
         mcurSpin2 = (Spinner) row.findViewById(R.id.pl_spin2); 
         mtv1 = (TextView) row.findViewById(R.id.pl_tv1); 
         mtv2 = (TextView) row.findViewById(R.id.pl_tv2); 
         mcurSpin1.setVisibility(View.VISIBLE); 
         mtv1.setVisibility(View.VISIBLE); 
         mcurSpin2.setVisibility(View.VISIBLE); 
         mtv2.setVisibility(View.VISIBLE); 
         PopulateSpinner1(); 
         PopulateSpinner2(); 
        } 
        return true; 
      } 
      return false; 
     } 
    }); 
    setListAdapter(mAllItems); 
} 

private void PopulateSpinner1(){ 
    mcurSpin1 = mDbHelper.fetchSByItemId(mItemId); 
    startManagingCursor(mcurSpin1); 
    String[] from = new String[]{DbAdapter.KEY_SNAME}; 
    int[] to = new int[]{android.R.id.text1}; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,     
      android.R.layout.simple_spinner_item, mcurSpin1, from, to); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    mSpin1.setAdapter(adapter); 

} 

private void PopulateSpinner2(){ 
    mcurSpin2 = mDbHelper.fetchMByItemId(mItemId); 
    startManagingCursor(mcurSpin2); 
    String[] from = new String[]{DbAdapter.KEY_MNAME}; 
    int[] to = new int[]{android.R.id.text1}; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_spinner_item, mcurSpin2, from, to); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    mSpin2.setAdapter(adapter); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    ViewGroup row = (ViewGroup)v; 
    mSCursor.moveToPosition(position); 
    mItemId = mSCursor.getInt(mSCursor.getColumnIndex(DbAdapter.KEY_SID)); 
    mCheckBox = (CheckBox) row.findViewById(R.id.pl_selected); 
    mcurSpin1 = (Spinner) row.findViewById(R.id.pl_spin1); 
mcurSpin2 = (Spinner) row.findViewById(R.id.pl_spin1); 
mtv1 = (TextView) row.findViewById(R.id.pl_tv1); 
mtv2 = (TextView) row.findViewById(R.id.pl_tv2); 
    if (mCheckBox.isChecked()) { 
     mCheckBox.setChecked(false); 
     mcurSpin1.setVisibility(View.GONE); 
     mtv1.setVisibility(View.GONE); 
     mcurSpin2.setVisibility(View.GONE); 
     mtv2.setVisibility(View.GONE); 
} 
    else { 
     mCheckBox.setChecked(true); 
     mcurSpin1.setVisibility(View.VISIBLE); 
     mtv1.setVisibility(View.VISIBLE); 
     mcurSpin2.setVisibility(View.VISIBLE); 
     PopulateSpinner1(); 
     PopulateSpinner2(); 

    } 
    } 

} 

Dzięki w dvance dla jakiejkolwiek pomocy, którą możesz dać.

+0

Spróbuj ustawić błystki, aby uniemożliwić ustawienie ostrości. W xml 'android: focusableInTouchMode =" false "' i 'android: focusable =" false "'. W ten sposób nie mogą wykraść fokusu z listy, ale możesz je "kliknąć", aby otworzyć. – Barak

+1

Dzięki za odpowiedź, już ustawiłem fokus na dotyk i ustawię ostrość na obroty. Jeśli to zrobię, wiersze, w których nic nie jest zapełnione, rozłączą się, więc jest to na pewno potrzebne, ale nie rozwiązuje problemu. – collusionbdbh

Odpowiedz

11

Możesz spróbować dodać android:descendantFocussability=blocksDescendants do najbardziej liniowego układu. To może być pomocne.

+0

Dzięki Andro, składnia nie była całkiem poprawna, ale rozwiązała problem: – collusionbdbh

+3

poprawna składnia: android: descendantFocusability = "blocksDescendants". Dzięki jeszcze raz. – collusionbdbh

+1

Awesome, to naprawdę pomogło! :) – user1051505

Powiązane problemy