旧的listview还在那里

时间:2012-09-13 22:05:10

标签: android listview android-listview

我有一个正常运行的活动,并显示从表查询中获取的数据。使用OnItemClick方法,我进行了设置,以便在选择行时,新活动以新的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
}

}

1 个答案:

答案 0 :(得分:0)

好。得到它了。刚刚意识到我在每个方法中实例化ListAdapter时错误地使用了相同的布局(nurse_list),所以它试图将我的新数据查询应用到原始布局(当然这已被搞砸了)。谢谢你看一看,真的很感激。

很抱歉,这会浪费任何人的时间。也许是其他新手的教训。