2015-08-14 41 views
0

我的應用程序在dialogFragment裏面有listview,當用戶選擇它應該Toast,在我的情況下項目不可點擊。DialogueFragment裏面的Listview不可點擊

DialogueFragment

public class HistoryDialogue extends DialogFragment { 

ListView list ; 
Cursor c ; 
HistoryAdapter adapter ; 
Context context ; 



@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    View view = inflater.inflate(R.layout.history,null); 
    list = (ListView)view.findViewById(R.id.listView); 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    ContentResolver resolver = getActivity().getContentResolver(); 
    c=resolver.query(Contract.SaveRunning.CONTENT_URI,null,null,null,null); 
    adapter= new HistoryAdapter(getActivity(),c,0); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getActivity(),"keep going ",Toast.LENGTH_LONG).show(); 
     } 
    }); 

    builder.setView(list); 
    return builder.create(); 

} 

}

的CursorAdapter

public class HistoryAdapter extends CursorAdapter { 
public HistoryAdapter(Context context, Cursor c, int flags) { 
    super(context,c, flags); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView text = (TextView) view.findViewById(R.id.text); 
    String body = cursor.getString(cursor.getColumnIndex("name")); 
    text.setText(body); 

} 

}

history.xml

<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView" 
    android:layout_gravity="center_horizontal" /> 

historylist.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="60dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/text" 
     android:layout_margin="5dp" 
     android:layout_weight="1"/> 

    <Button 
     android:layout_width="30dp" 
     android:layout_height="35dp" 
     android:background="@drawable/delete" 
     android:id="@+id/btn" 
     android:layout_margin="10dp" 
     android:layout_weight="0" 
     /> 

+0

你可以發表你的'R.layout.history' XML代碼? –

+1

上面編輯的帖子 – Error

+0

看到這個鏈接http://www.edumobile.org/android/custom-listview-in-a-dialog-in-android/ – Aditya

回答

1

在您的ListView(在history.xml)和項目主佈局(在historylist.xml)中添加android:focusable="false"

<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:focusable="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView" 
    android:layout_gravity="center_horizontal" /> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:focusable="false" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="60dp"> 
... 
1

此行添加到您的CursorAdapter的bindView:

((ViewGroup)row).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 

如果這還不夠,加clickable="false"到您的historylist.xml的按鈕