2013-06-03 71 views
0

我正在製作一個應用程序,用戶可以在其中搜索患者選項。當用戶搜索患者時,結果將進入列表視圖。但問題是,結果不夠明亮,用戶無法正確看到,因爲您可以在應用程序的下面的屏幕截圖中看到它(在搜索患者按鈕下方,結果顯示爲1234567890,abc,xyz。請參閱徹底。)ListView Listitem不夠亮

Search patient screen

我不知道如何使它正確光明的。同樣的問題也出現在真實的Android設備上。 請有人幫助我。

以下是我的代碼: -

SearchPatient.java

public class SearchPatient extends Activity implements PacketListener, OnItemClickListener, TextWatcher 
{ 
private EditText mobileNumber; 
private EditText firstName; 
private EditText lastName; 
private ListView patients; 
private ArrayAdapter<String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search_patient); 
    mobileNumber = (EditText) findViewById(R.id.mobile_number); 
    mobileNumber.addTextChangedListener(this); 
    firstName = (EditText) findViewById(R.id.first_name); 
    firstName.addTextChangedListener(this); 
    lastName = (EditText) findViewById(R.id.last_name); 
    lastName.addTextChangedListener(this); 
    patients = (ListView) findViewById(R.id.patients); 
    patients.setOnItemClickListener(this); 
    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1); 
    patients.setAdapter(adapter); 

    PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
    Login.connection.addPacketListener(this, filter); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
    Login.connection.addPacketListener(this, filter); 
} 

@Override 
protected void onStop() 
{ 
    Login.connection.removePacketListener(this); 
    super.onStop(); 
} 

@Override 
public void onTextChanged(CharSequence charSequence, int i1, int i2, int i3) 
{ 
    return; 
} 

@Override 
public void afterTextChanged(Editable editable) 
{ 
    adapter.clear(); 

    String mobileNumber = this.mobileNumber.getText().toString(); 
    String firstName = this.firstName.getText().toString(); 
    String lastName = this.lastName.getText().toString(); 

    if(mobileNumber.equals("")) 
     mobileNumber = null; 
    if(firstName.equals("")) 
     firstName = null; 
    if(lastName.equals("")) 
     lastName = null; 

    searchPatient(mobileNumber, firstName, lastName); 
} 

@Override 
public void beforeTextChanged(CharSequence charSequence, int i1, int i2, int i3) 
{ 
    return; 
} 

public void searchPatient(View view) 
{ 
    adapter.clear(); 

    String mobileNumber = this.mobileNumber.getText().toString(); 
    String firstName = this.firstName.getText().toString(); 
    String lastName = this.lastName.getText().toString(); 

    if(mobileNumber.equals("")) 
     mobileNumber = null; 
    if(firstName.equals("")) 
     firstName = null; 
    if(lastName.equals("")) 
     lastName = null; 

    searchPatient(mobileNumber, firstName, lastName); 
} 

private void searchPatient(String mobileNumber, String firstName, String lastName) 
{ 
    Login.connection.sendPacket(getMessage(buildCommand(mobileNumber, firstName, lastName))); 
} 

private String buildCommand(String mobileNumber, String firstName, String lastName) 
{ 
    StringBuilder commandBuilder = new StringBuilder(); 

    commandBuilder.append("##?##"); 
    commandBuilder.append("##app:patients##"); 
    commandBuilder.append("##" + mobileNumber + ", " + firstName + ", " + lastName + "##"); 

    return commandBuilder.toString(); 
} 

private Message getMessage(String body) 
{ 
    Message message = new Message("[email protected]", Message.Type.chat); 
    message.setBody(body); 

    return message; 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
{ 
    String[] record = adapterView.getItemAtPosition(i).toString().split("\\,"); 
    if(record.length == 1) 
     return; 

    Intent patientDetails = new Intent(getApplicationContext(), PatientDetails.class); 
    patientDetails.putExtra(PatientDetails.ACTION, PatientDetails.READ_PATIENT); 
    patientDetails.putExtra(PatientDetails.RECORD, record); 

    startActivity(patientDetails); 
} 

@SuppressLint("NewApi") 
@Override 
public void processPacket(Packet packet) 
{ 
    Message message = (Message) packet; 
    String reply = message.getBody().trim(); 

    if(reply.contains(".")) 
    { 
     String[] records = reply.split("\\."); 

     if(Build.VERSION.SDK_INT >= 11) 
      adapter.addAll(records); 
     else 
      for(int i = 0; i < records.length; i++) 
       adapter.add(records[i]); 
    } 
    else 
     adapter.add(reply); 

    adapter.notifyDataSetChanged(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 

}

search_patient.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:orientation="vertical" 
tools:context=".SearchPatient" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/mobile_number"/> 
<EditText 
    android:id="@+id/mobile_number" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:inputType="number"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/and_or"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/first_name"/> 
<EditText 
    android:id="@+id/first_name" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:inputType="text"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/last_name"/> 
<EditText 
    android:id="@+id/last_name" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:inputType="text"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/search_patient" 
    android:onClick="searchPatient"/> 
<ListView 
    android:id="@+id/patients" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scrollbars="vertical"/> 

</LinearLayout> 

回答

2

嘗試不同Context傳遞給你的適配器:

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 

更多關於這一點:Android Holo Light styling changes depending on chosen context

+0

謝謝,這是工作。你能否向我解釋問題實際上是什麼。 –

+0

編輯我的答案。 –