请帮助我。
我想开发“国家历史”Android应用程序。
但我有一个问题。我不能将Country Header放在Listview列表项中。我想把Header;如“非洲:北非”,“撒哈拉以南非洲”,“亚洲”,“北亚”,“西亚和中亚”,“南亚和东南亚”,“欧洲”,“北方和中部”美国“,”北美“,”大洋洲“,”南美洲“
此处我的源代码:
https://www.dropbox.com/s/k66xonom9up1yif/Country%20Search%20ListView.zip?dl=0
这是我的示例代码:
的 Search_Country.java
package com.nasir.search;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Search_Country extends ListActivity {
private EditText SearchText;
private ListView ListText;
private String[] Number_List = {
// Africa : North Africa
"Algeria", "Egypt", "Libya", "Morocco", "Tunisia", "Western Sahara",
// Sub-Saharan Africa
"Angola", "Benin", "Botswana", "Burkina Faso", "Burundi", "Cameroon", "Cape Verde", "Chad", "Comoros", "Congo",
"Côte d'Ivoire", "Djibouti", "Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia", "Ghana", "Guinea", "Kenya",
"Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius", "Mayotte", "Mozambique", "Namibia",
"Niger", "Nigeria", "Rwanda", "Senegal", "Seychelles", "Sierra Leone", "Somalia", "South Africa", "South Sudan",
"Sudan", "Swaziland", "Uganda", "Zambia", "Zimbabwe",
// Asia :
// East Asia
"China", "Hong Kong", "Japan", "Korea", "Macao", "Mongolia", "Taiwan",
// North Asia
"Belarus", "Moldova", "Russian Federation", "Ukraine",
// West & Central Asia
"Afghanistan", "Armenia", "Azerbaijan", "Bahrain", "Cyprus", "Georgia", "Iran", "Iraq", "Israel", "Jordan", "Kazakhstan",
"Kuwait", "Kyrgyzstan", "Lebanon", "Oman", "Pakistan", "Qatar", "Saudi Arabia", "Tajikistan", "Turkey", "Turkmenistan",
"United Arab Emirates", "Uzbekistan", "Yemen",
// South & Southeast Asia
"Bangladesh", "Bhutan", "Brunei", "Cambodia", "India", "Indonesia", "Malaysia", "Maldives", "Myanmar", "Nepal",
"Philippines", "Singapore", "Sri Lanka", "Thailand", "Timor-Leste", "Viet Nam",
// Europe
"Åland Islands", "Albania", "Andorra", "Austria", "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia",
"Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "France", "Germany", "Gibraltar", "Greece",
"Greenland", "Guernsey", "Hungary", "Iceland", "", "", "", "", "Ireland", "Jersey", "Latvia", "Liechtenstein",
"Lithuania", "Luxembourg", "Macedonia", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal",
"Romania", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Svalbard and Jan Mayen", "Sweden", "Switzerland",
"United Kingdom",
// North & Central America
"Caribbean Islands", "Anguilla", "Antigua and Barbuda", "Aruba", "Bahamas", "Barbados", "Bermuda", "Cayman Islands",
"Bonaire", "Cuba", "Dominica", "Curaçao", "Guadeloupe", "Haiti", "Jamaica", "Martinique", "Montserrat", "Puerto Rico",
"Saint Bathélemy", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin (French Part)", "Saint Vincent and the Grenadines",
"Sint Maarten (Dutch Part)", "Trinidad and Tobago", "Turks and Caicos Islands", "Virgin Islands, British", "Virgin Islands, U.S.",
// Mesoamerica (or Central America)
"Belize", "Costa Rica", "El Salvador", "Guatemala", "Honduras", "Mexico", "Nicaragua", "Panama",
// North America
"Canada", "Saint Pierre and Miquelon", "United States",
// Oceania
"American Samoa", "Australia", "Christmas Island", "Cocos (Keeling) Islands", "Cook Islands", "Fiji", "Guam", "Kiribati",
"Marshall Islands", "Micronesia", "New Caledonia", "New Zealand", "Niue", "Norfolk Island", "Northern Mariana Islands",
"Palau", "Papua New Guinea", "Pitcairn", "Samoa", "Solomon Islands", "Tokelau", "Tonga", "Tuvalu", "United States Minor Outlying Islands",
"Vanuatu", "Wallis and Futuna",
// South America
"Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador", "Falkland Islands (Malvinas)", "French Guiana", "Guyana",
"Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela"};
private ArrayList<String> array_sort;
int textlength = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_country);
SearchText = (EditText) findViewById(R.id.listview_search);
ListText = (ListView) findViewById(android.R.id.list);
array_sort = new ArrayList<String>(Arrays.asList(Number_List));
setListAdapter(new bsAdapter(this));
SearchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = SearchText.getText().length();
array_sort.clear();
for (int i = 0; i < Number_List.length; i++)
{
if (textlength <= Number_List[i].length())
{
if(Number_List[i].toUpperCase().contains(SearchText.getText().toString().toUpperCase().trim()))
{
array_sort.add(Number_List[i]);
}
}
}
AppendList(array_sort);
}
});
ListText.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if( array_sort.get(position).equals("Afghanistan")){
Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_afganistan));
startActivity(intent);
}
else if ( array_sort.get(position).equals("Albania")){
Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_albenia));
startActivity(intent);
}
else if ( array_sort.get(position).equals("Algeria")){
Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_algeria));
startActivity(intent);
}
else if ( array_sort.get(position).equals("Brazil")){
Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_brazil));
startActivity(intent);
}
}
});
}
public void AppendList(ArrayList<String> str)
{
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
this.cntx = context;
}
public int getCount()
{
return array_sort.size();
}
public Object getItem(int position)
{
return array_sort.get(position);
}
public long getItemId(int position)
{
return array_sort.size();
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = cntx.getLayoutInflater();
row = inflater.inflate(R.layout.search_country_listview, null);
TextView tv = (TextView) row.findViewById(R.id.listview_seacrh_text);
tv.setText(array_sort.get(position));
return row;
}
}
}