好的,我正在研究一個應用程序,該應用程序的頂部有一個帶有列表視圖和編輯文本框的頁面。當你在edittext框中輸入東西時,它會過濾在listview中顯示的項目。我遇到的問題是出現在滑塊側面的快速滾動圖標。
當頁面第一次加載時無論我做什麼快速滾動滑塊圖標都不會出現在屏幕上。然後我點擊編輯文本框並輸入一個字符,然後抹掉它,現在我的快速滾動滑塊圖標就會出現。
首先加載沒有快速滾動圖標。
編輯文本框,然後擦除文本和快速滾動圖標出現。
Android:fastScrollEnabled起初不工作
我的機器人:fastScrollEnabled = 「真」 在我的列表視圖設置。另外我已經在代碼中通過執行lv1.setFastScrollEnabled(true)來手動設置它;
無論我改變,我仍然得到相同的行爲,除非我從代碼和xml中完全刪除它,然後它將停止在第二頁上工作。我已經嘗試清理我的項目,但仍然沒有好處。我傾向於它是Android中的錯誤,或者我錯過了一些非常簡單的東西。
這是我的代碼。
public class SearchByFood extends ParentClass
{
private ListView lv1;
private EditText ed;
int textlength = 0;
private ArrayList<String> arr_sort = new ArrayList<String>();
private ArrayList<String> foods = new ArrayList<String>();
private LayoutInflater mInflater;
private ArrayList<Food> foodList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_by_food);
setTextTitle("Search by Food");
lv1 = (ListView) findViewById(R.id.ListView01);
ed = (EditText) findViewById(R.id.EditText01);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DataLayerFunctions d = new DataLayerFunctions(getApplicationContext());
foodList = d.selectFoodsWithSubstitutes();
for (Food f : foodList)
{
// this is to build a ArrayList<String> to pass to the setAdapter
Log.d("SearchByFood", "FoodName: " + f.getFood_Name());
foods.add(f.getFood_Name());
}
ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.search_food_listview, foods);
lv1.setAdapter(firstAdapter);
lv1.setFastScrollEnabled(true);
ed.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 = ed.getText().length();
arr_sort.clear();
for (String f : foods)
{
if (textlength <= f.length())
{
if (f.toString().toLowerCase().contains((CharSequence) ed.getText().toString().toLowerCase()))
{
Log.d("STRING", "STRING: " + f.toString() + " contains " + ed.getText());
if (ed.getText().length() > 0)
{
String newString = boldMyString(f, ed.getText().toString());
arr_sort.add(newString);
}
else
{
arr_sort.add(f);
}
}
}
}
// if empty add a no foods found
if (arr_sort.isEmpty())
{
arr_sort.add("No Foods Found");
}
// Load array
// lv1.setAdapter(new
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.search_food_listview, arr_sort)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView)
{
row = mInflater.inflate(R.layout.search_food_listview, null);
}
else
{
row = convertView;
}
TextView tv = (TextView) row.findViewById(android.R.id.text1);
tv.setText(Html.fromHtml(getItem(position)));
// tv.setText(getItem(position));
return row;
}
};
lv1.setAdapter(adapter);
}
private String boldMyString(String foodName, String guess)
{
int gLength = guess.length();
ArrayList<Integer> results = new ArrayList<Integer>();
for (int i = foodName.toLowerCase().indexOf(guess.toLowerCase()); i >= 0; i = foodName.toLowerCase()
.indexOf(guess.toLowerCase(), i + 1))
{
System.out.println("TEST:" + i);
results.add(i);
}
// Count value is for words that have 2 or more values of guess
// in them.
int count = 0;
for (int i : results)
{
StringBuffer s1 = new StringBuffer(foodName);
s1.insert(i + count, "<b>");
count = count + 3;
s1.insert(i + count + gLength, "</b>");
count = count + 4;
foodName = s1.toString();
System.out.println("FOOD NAME:" + i + ":" + foodName);
}
return foodName;
}
});
// This is what actually does stuff when you click on a listview item.
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Strip out the bold tags
String clicked = (String) lv1.getItemAtPosition(position);
clicked = clicked.replaceAll("<b>", "");
System.out.println("Clicked" + clicked);
clicked = clicked.replaceAll("</b>", "");
// Find the Food ID match and pass the food id to the
// fooddisplay page
for (Food f : foodList)
{
if (null != clicked && clicked.equals(f.getFood_Name()))
{
Intent intent = new Intent(SearchByFood.this, SubstituteDisplay.class);
intent.putExtra("FoodID", f.getFood_ID());
startActivity(intent);
}
}
}
});
}
@Override
public void onBackPressed()
{
final Intent intent = new Intent(this, MasterTemplateActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
return;
}
}
同樣,任何幫助,爲什麼我的快速滾動的圖標起初不露面,將不勝感激。這是一件小事,但它真的讓我煩惱。
嘗試具有fastScrollEnabled =在XML如此。從代碼中刪除它。可能會起作用。我與fastscroll有類似的問題.. – Varun 2012-04-24 13:28:24