2013-10-04 22 views
0

我正在構建一個原型應用程序,幫助研究人員跟蹤受試者患者的一個小羣體(8-10)。他們需要定期更新患者信息。我希望能夠觸摸一個按鈕,以提供所有患者的列表視圖,即使不是每一行中的患者信息都是如此。我創建了一個基本類病人:在ListView,arraylist和arrayadapter中的對象的Android生命週期

public class Patient { 

public String name; 
public String gender; 
public int painLevel; 
public String risk; 
public int score; 

public Patient() { 
    super(); 
} 

public Patient(String name, String gender, int painLevel, String risk, 
     int score) { 
    super(); 
    this.name = name; 
    this.gender = gender; 
    this.location = painLevel; 
    this.risk = risk; 
    this.score = score; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getGender() { 
    return gender; 
} 

public void setGender(String gender) { 
    this.gender = gender; 
} 

public int getpainLevel() { 
    return painLevel; 
} 

public void setpainLevel(int painLevel) { 
    this.painLevel = painLevel; 
} 

public int getScore() { 
    return score; 
} 

public void setScore(int score) { 
    this.score = score; 
} 

public void setRisk(String risk) { 
    this.risk = risk; 
} 

public String getRisk() { 
    return risk; 
} 

}

起初我進入他們進入這樣一個數組:

Patient[] cohort= new Patient[maxPatients]; 

現在我發現,爲了在ListView顯示我必須使用arrayadapter。我發現了很多例子和教程,如Vogella's教程,非常有幫助。不幸的是,大多數示例使用字符串數組來創建示例。

我已經能夠把我的列表與我需要的所有信息。現在我需要在一個longpress之後使用彈出式對話框來選擇一個病人來顯示/編輯所有數據。我已經能夠做到這一點,但這裏是我卡住的地方。

我可以編輯數據,它似乎改變了arrayList和arrayadapter中的Patient字段。我需要幫助的是如何反映患者的原始陣列隊列[]中的這些變化,並且還刪除隊列中的患者。現在它刪除一個病人,但不會從屏幕上的列表中刪除它,直到我改變方向,即使我調用notifyDataSetChanged();.

作爲一名新手android程序員,我顯然沒有完全理解原始數組,數組列表,數組適配器之間的關係 - 即使在完成所有閱讀之後。我目前的代碼很笨拙,不夠優雅,因爲它是由許多示例中的零碎拼湊而成的,還有許多不必要的調試代碼。在調試過程中,我會看到隊列數組中的患者的多個副本,arraylist和arrayadapter。

我目前啓動一個編輯屏幕,如果用戶使用新的意圖點擊'編輯患者'按鈕。用戶陣列用於在陣列適配器中構建陣列列表,並且列表出現在屏幕上。使用具有編輯功能然後「保存」,「刪除」和「取消」按鈕的警報對話框,longpress會提供特定患者。

我需要重新開始,但需要更多有經驗的程序員提供一些關於如何更好地解決問題的指導。指向基本工作示例代碼是足夠的,或者可能是對象在listview中的生命週期的流程圖會更好。 (我試圖做出一個,但得到最虛心的失去。)我也花了幾個小時在developer.android.com,但失去了跟蹤我所有相關鏈接的原始追求。

我需要應用程序運行在Android 2.3以上,因爲Grad學校的研究人員並不都擁有最新的Android設備。希望這種方法沒有任何區別。

回答

1

不幸的是,大多數示例都使用字符串數組來創建示例。

我下面的例子是關於如何創建自己的適配器,然後在listview中使用患者的數組列表的一個小例子。

我已經做了一些類似的事情,只是列表中的另一個類。 如果創建患者的ArrayList:

ArrayList <Patient> patientList = new ArrayList<Patient>(); 
// Add patients to the list: 
patientList.add(new Patient()); 
patientList.add(new Patient()); 
// etc... 

您的自定義適配器然後加入到ListView。

PatientListAdapter adapter = new PatientListAdapter(getApplicationContext(), patientList); 
ListView patientListView.setAdapter(adapter); 

創建自定義列表適配器:

private class PatientListAdapter extends BaseAdapter { 

    private final Context context; 

    private final ArrayList<Patient> patientList; 

    public PatientListAdapter(Context context, ArrayList<Patient> itemsArrayList) { 

     this.context = context; 
     patientList = itemsArrayList; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    // code below 
    } 
} 

然後,你需要重寫自定義適配器的getView功能。然後getView裏面,你可以在裏面ArrayList的訪問病人:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    // This is a veeery minimal example. 

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    // Custom layout for a row if you want to: 
    convertView = inflater.inflate(R.layout.patient_row, parent, false); 

    // get the patient's name from the list of patients: 

    (TextView) name = (TextView) covertView.findViewById(R.id.patient_name); 
    String patientName = patientList.get(position).getName(); 
    name.setText = patientName; 

    return convertView; 
} 

最後,我鼓勵你去實現ViewHolder模式,以增加您的ListView的性能。