我目前正在撰写的应用程序处理地点.创建场所的主要方法是输入地址.提供便捷的方法对于UX来说非常重要.
在调查问题一段时间后,我得出的结论是,最好的方法是根据当前位置和用户输入自动完成的文本区域(如Google Map应用程序中).由于Google Map API不提供此功能,因此我必须自行实施.
为了获得地址建议我使用Geocoder.这些建议由自定义ArrayAdaptor通过自定义过滤器提供给AutoCompleteTextView.
ArrayAdapter(某些代码已被删除)
public class AddressAutoCompleteAdapter extends ArrayAdapter<Address> {
@Inject private LayoutInflater layoutInflater;
private ArrayList<Address> suggested;
private ArrayList<Address> lastSuggested;
private String lastInput = null;
private AddressLoader addrLoader;
public AddressAutoCompleteAdapter(Activity activity,
AddressLoaderFactory addrLoaderFact,
int maxSuggestionsCount) {
super(activity,R.layout.autocomplete_address_item);
suggested = new ArrayList<Address>();
lastSuggested = new ArrayList<Address>();
addrLoader = addrLoaderFact.create(10);
}
...
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
...
@SuppressWarnings("unchecked")
@Override
protected FilterResults …Run Code Online (Sandbox Code Playgroud) android autocomplete autocompletetextview android-arrayadapter
ArrayAdapter具有add(T object)在列表末尾添加对象的方法.有没有办法在列表的开头添加对象?
对不起我的英语不好.我学习kotlin,现在我需要编写ArrayAdapter,但是在尝试这样做时我有一些错误__CODE__kotlin说我__CODE__.在网站kotline我将java类转换为kotlin类,并尝试在此站点中做.为什么我有错误,我怎么能解决这个问题?
convertView = vi.inflate(resource, null)
Run Code Online (Sandbox Code Playgroud) 我想创建一个Activity包含列表的列表,其中行具有自定义布局.所以我创建了list_entry_layout.xml定义列表每行应该具有的布局的文件(在我的示例中,每行应该有标题和摘要):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/list_entry_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" >
</TextView>
<TextView
android:id="@+id/list_entry_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" >
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道如何将数据添加到ListActivity类中的每一行.使用以下代码片段,我可以添加每行的标题:
public class MyActivity extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity);
ListView listView = (ListView) findViewById(android.R.id.list);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> titleAdapter = new ArrayAdapter<String>(this, R.layout.list_entry_layout, R.id.list_entry_title, values);
// Assign …Run Code Online (Sandbox Code Playgroud) 我有一个Activity从Web服务检索数据.这个数据ListView通过一个ArrayAdapter 呈现在一个ArrayAdapter中,里面RelativeLayout有三个TextViews内部,没什么花哨的,它工作得很好.
现在我想要实现一个Activity应该在用户单击ListView中的项目时调用的详细信息,听起来很简单,但我不能在我的生活中使用onItemClickListener来处理我的ArrayAdapter.
这是我的主要内容Activity:
public class Schema extends Activity {
private ArrayList<Lesson> lessons = new ArrayList<Lesson>();
private static final String TAG = "Schema";
ListView lstLessons;
Integer lessonId;
// called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// can we use the custom titlebar?
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// set the view
setContentView(R.layout.main);
// set the title
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
// listview called lstLessons
lstLessons = (ListView)findViewById(R.id.lstLessons); …Run Code Online (Sandbox Code Playgroud) 我想知道,有什么指导方针,我们如何选择ArrayAdapter和SimpleAdapter.
对于每个列表项,它们将由几个TextViews和Views呈现,这些布局很好.通过使用ArrayAdapter或,我意识到我可以毫无问题地实现它们SimpleAdapter.
我们如何选择它们是否有任何指导方针?我的猜测是
ArrayAdapter使我们能够动态添加新项目,即使在此期间显示列表后也是如此.注意,ArrayAdapter有方法add.SimpleAdapter 用于大小写,一旦显示列表,就不能再添加新项目了.这是我们应该遵循的准则吗?
我ArrayAdapter在我的应用程序中有大量使用,因为大多数活动都持有ListView,我需要一些自定义的东西.
我看了一下android开发者文档中的测试类,但是无法找到一些示例或正确的测试类......
1)ArrayAdapterAndroid中是否有(单元)测试的最佳实践?
2)我可以选择错误的方法(使用适配器)并以这种方式杀死可测试性吗?
你能解释一下这种getView()方法吗ArrayAdapter?
我阅读了文档,它有三个参数:
position:项目在我们想要的视图的项目的数据集的位置. convertView:如果可能的话,重用旧视图.注意:在使用之前,应检查此视图是否为非null且类型是否合适.如果无法转换此视图以显示正确的数据,则此方法可以创建新视图.parent:此视图最终将附加到的父级我理解了position参数.就像他们说的那样,这意味着该项目的位置,已经请求了视图.
convertView来自哪里.我见过很多例子,他们检查是否convertView为null.如果is为null,则它们会对行布局的新实例进行膨胀,填充并返回它.我想我也已经知道了这一点,但还有一件事让我感到困惑.通过convertView参数传递的布局是什么.如果初始化时传入的resource参数是ArrayAdapter?是返回的最后一个布局的缓存副本getView()?
最后.什么是parent参数做.我没有看到太多利用它的例子.他们中的大多数只是重复/膨胀行布局并返回它.
(我问,因为我的点击动画在我的ListView.特别是这个旨在复制Spotify的下拉快速动作菜单.我的动画有点迟钝.在诊断了这个问题一段时间之后,我意识到这是因为我的getView()方法需要花费一些时间才能完成,因为我在每次迭代中都在为一个新的行布局充气.有人建议缓存行布局,ViewHolder其他一些例子指向重用convertView参数,即仅膨胀一个行布局如果convertView为null.)
java android android-arrayadapter android-listview android-adapter
我想为我使用ArrayAdapter创建的ListView创建一个OnItemClickListener
这是我用来创建它的代码:
List<Comment> values = datasource.some_search("Wednesday","11");
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
我如何实现onItemClickListener?
谢谢!
编辑:我在我的ArrayAdapter和ListView中使用一串对象.
编辑2:更多代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
//check if database is populated if NOT, populate with txtToDb();
if (!datasource.isPopulated()) {
// Database is not populated so copy it from assets here
try {
txtToDb();
Log.i("Database", "Was not Populated");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Database", "Was not populated: txtToDb(); failed");
}
} …Run Code Online (Sandbox Code Playgroud) android listview onclick android-arrayadapter onitemclicklistener
我有一个定制的适配器,具有标头和自定义行.有时候我的v.getTag()会在我存储ViewHolder的地方返回null.它不会一直发生,我无法弄清楚它何时以及为什么会诅咒.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
//Header
if(items.hasDescription() && 0 == position) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.app_list_header, null);
((TextView) v.findViewById(R.id.app_list_header_description_text)).setText(items.getDescription());
return v;
}
ViewHolder holder;
// Inflate app view.
if (v == null || v.getTag() == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(textViewResourceId, null); //TODO: parent instead of null?
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.title);
holder.company = (TextView) v.findViewById(R.id.company);
holder.priceOrStatus = (TextView) …Run Code Online (Sandbox Code Playgroud)