2012-09-13 88 views
0

我有一個工作正常,並顯示從表查詢獲得的數據的活動。使用OnItemClick方法,我已經設置了它,因此如果選擇了一行,新的活動將以新的ListView開始,但原始ListView中的很多元素都在新的列表視圖中,包括每行上的切換按鈕,和一個文本視圖。任何想法可能會造成這種情況?舊的listview仍然有

這是最初的ListView。的按鈕顯示不出來,只是在ListView一切:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#81BEF7" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/btnAddNurseToRoster" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/add" 
    android:focusable="true"/> 

<Button 
    android:id="@+id/btnRemoveNurseFromRoster" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/btnAddNurseToRoster" 
    android:layout_toRightOf="@+id/btnAddNurseToRoster" 
    android:text="@string/delete" 
    android:focusable="true"/> 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/btnAddNurseToRoster" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

</RelativeLayout> 

新的ListView:

<?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" 
android:background="#81BEF7" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tvAssignmentsText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="25dp" 
    android:textColor="#FFFFFF" 
    android:text="Assignments" 
    android:gravity="center_horizontal" 
    android:background="#663399" 
    /> 

<ListView 
    android:id="@+id/lvAssignmentsList" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/tvAssignmentsText" 
    android:paddingTop="20dp" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

</ 

而這裏的調用新的ListView代碼:

package com.deadEddie.staffingmanagement; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.util.Log; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

public class ShowAssignments extends Activity { 

DbCommunicator getAssignmentsList; 
ListView assignmentsList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.assignments); 
    displayList(); 

} 

private void displayList() { 

    // instantiate ListView object 
    assignmentsList = (ListView) findViewById(R.id.lvAssignmentsList); 

    // instantiate variables for CursorAdapter 
    int [] to = new int [] {R.id.rbAssignmentsList }; 
    String [] from = new String [] {DbCommunicator.KEY_ROOM_NUMBER}; 

    // instantiate getAssignmentsList with new DbCommunicator object and open 
    getAssignmentsList = new DbCommunicator(this); 
    getAssignmentsList.open(); 

    // get and manage cursor 
    Cursor assignmentsCursor = getAssignmentsList.getAssignments(this); 
    startManagingCursor(assignmentsCursor); 

    // list adapter 
    ListAdapter assignmentsListAdapter = new SimpleCursorAdapter(this,  R.layout.nurse_list, assignmentsCursor, from, to, 0); 

    assignmentsCursor.moveToNext(); 

    // set ListView 
    assignmentsList.setAdapter(assignmentsListAdapter); 
    assignmentsList.setItemsCanFocus(true); 
} 

} 

添加用於填充原始ListView的代碼:

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

<ToggleButton 
    android:id="@+id/rosterDutyStatus" 
    android:layout_width="50dp" 
    android:layout_height="40dp" 
    android:layout_alignParentLeft="true" 
    android:textOff="Off Duty" 
    android:textOn="On Duty" 
    android:textSize="10dp" 
    android:focusable="false"/> 

<TextView 
    android:id="@+id/rosterListLname" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/rosterDutyStatus" 
    android:layout_toRightOf="@+id/rosterDutyStatus" 
    android:paddingTop="6dp" 
    android:textColor="#FFFFFF" 
    android:textSize="18dp" 
    android:textStyle="bold" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 

<TextView 
    android:id="@+id/rosterListFname" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/rosterListLname" 
    android:layout_toRightOf="@+id/rosterListLname" 
    android:paddingTop="6dp" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/rosterListMI" 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/rosterListFname" 
    android:layout_toRightOf="@+id/rosterListFname" 
    android:paddingTop="6dp" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/rosterListID" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/rosterListMI" 
    android:layout_toRightOf="@+id/rosterListMI" 
    android:visibility="invisible" /> 

<TextView 
    android:id="@+id/rosterViewAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/rosterDutyStatus" 
    android:paddingRight="50dp" 
    android:text="@string/assigned" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/firstAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/rosterViewAssignment" 
    android:layout_toRightOf="@+id/rosterViewAssignment" 
    android:text="@string/noAssignments" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

<TextView 
    android:id="@+id/secondAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/firstAssignment" 
    android:layout_toRightOf="@+id/firstAssignment" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

<TextView 
    android:id="@+id/thirdAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/secondAssignment" 
    android:layout_toRightOf="@+id/secondAssignment" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

<TextView 
    android:id="@+id/fourthAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/thirdAssignment" 
    android:layout_toRightOf="@+id/thirdAssignment" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

<TextView 
    android:id="@+id/fifthAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/fourthAssignment" 
    android:layout_toRightOf="@+id/fourthAssignment" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/> 

<TextView 
    android:id="@+id/sixthAssignment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/fifthAssignment" 
    android:layout_toRightOf="@+id/fifthAssignment" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold"/>" 
--> 

</RelativeLayout> 
調用原始佈局

代碼我試圖改變:

package com.deadEddie.staffingmanagement; 


import android.app.Dialog; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.AdapterView.OnItemClickListener; 


public class EditRoster extends ListActivity implements OnClickListener { 

String TAG = "EditRoster"; 
Button addNurse; 
Button deleteNurse; 
DbCommunicator rosterView; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit_roster); 
    addNurse = (Button) findViewById(R.id.btnAddNurseToRoster); 
    deleteNurse = (Button) findViewById(R.id.btnRemoveNurseFromRoster); 
    displayNurseRoster(); 
    displayDialog(); 

    addNurse.setOnClickListener(this); 
    deleteNurse.setOnClickListener(this); 
} 

// method to put timer on dialog 
public void timerDelayRemoveDialog(long time, final Dialog dialogView){ 
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     public void run() { 
      dialogView.dismiss(); 
     } 
    }, time); 
} 

private void displayDialog() { 
    Dialog d = new Dialog(this); 
    d.setTitle("Select Nurse to Adjust Assignments"); 
    d.show(); 
    timerDelayRemoveDialog(2000, d); 
} 

public void displayNurseRoster(){ 

    listView = (ListView) findViewById(android.R.id.list); 

    // int variables filled with NURSE_TABLE data 
    int[] to_nurseTable = new int[] { 

      // int list for data from NURSE_TABLE fields 
      R.id.rosterDutyStatus, 
      R.id.rosterListLname, 
      R.id.rosterListFname, 
      R.id.rosterListMI, 
      R.id.rosterListID, 
      // int list for data from ASSIGNMENTS_TABLE fields 
      R.id.firstAssignment}; 

    // String array holding data fields from NURSE_TABLE 
    String[] from_nurseTable = new String [] { 

      // fields from NURSE_TABLE 
      DbCommunicator.KEY_DUTY_STATUS, 
      DbCommunicator.KEY_LNAME, 
      DbCommunicator.KEY_FNAME, 
      DbCommunicator.KEY_MI, 
      DbCommunicator.KEY_NURSE_ROWID, 
      DbCommunicator.KEY_ROOM_NUMBER}; 

    // instantiate instance of DbCommunicator object 
    rosterView = new DbCommunicator(this); 

    // open instance 
    rosterView.open(); 

    // get & manage cursor for NURSE_TABLE data 
    Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this); 
    startManagingCursor(nurseTableCursor); 

    // instantiate cursor adaptor 
    ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this, 
      R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable); 

    nurseTableCursor.moveToNext(); 

    // set listView 
    listView.setAdapter(nurseTableAdapter); 
    rosterView.close(); 
    listView.setItemsCanFocus(true); 
    listView.setOnItemClickListener(new OnItemClickListener(){ 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      Log.i(TAG, "onItemClickListener set"); 
      Intent showAssignments = new Intent("com.deadEddie.staffingmanagement.SHOWASSIGNMENTS"); 
      startActivity(showAssignments); 
     } 

    }); 

}// displayNurseRoster() 


@Override 
public void onClick(View v) 
{ 
    switch (v.getId()) 
    { 
    case R.id.btnAddNurseToRoster: 
     Intent add = new Intent("com.deadEddie.staffingmanagement.ADDNURSETOROSTER"); 
     startActivity(add); 
     break; 

    case R.id.btnRemoveNurseFromRoster: 

     break; 
    } // switch 
} 

} 
+0

好吧,按鈕不會顯示在你的'RelativeLayout'中,因爲你的列表視圖在它們之上。您需要將按鈕項目移動到XML中的ListView下方以供它們顯示。 – hwrdprkns

+0

好的,謝謝,但我不想按鈕,我只是想在新列表視圖中的項目,這是所有新的內容。我不希望任何舊的listview出現在那裏 – deadEddie

+0

我們需要查看調用舊列表的ListActivity代碼。新代碼似乎不是問題。 – hwrdprkns

回答

0

好。得到它了。剛剛意識到,當我在每個方法中實例化ListAdapter時,我錯誤地使用了相同的佈局(nurse_list),所以它試圖將我的新數據查詢應用到原始佈局(當然這已被搞砸了)。謝謝你看看,真的很感激它。

對不起,如果這浪費任何人的時間。也許是其他新手的教訓。

相關問題