我有一些问题让我的listview显示在我正在编写的简单应用程序中.由于某种原因getView()
,我的适配器类的方法没有被调用.但是,当getCount()
在我的适配器类中调用when时,它不返回0但实际上返回了正确的值.我真的很困惑为什么这不起作用.
这是我的活动:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class CardListActivity extends Activity {
protected ListView lv;
protected int nCards = 12;
protected String[] cardList = new String[nCards];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardlist);
this.loadCardStrings();
lv = (ListView) findViewById(R.id.CardList_ListView);
EditorListAdapter adapter = new EditorListAdapter(this, this.cardList);
lv.setAdapter(adapter);
}
protected void loadCardStrings(){
cardList = getResources().getStringArray(R.array.card_list);
Util.logD("Created the string arrays with:" + Integer.toString(cardList.length) + " items.");
}
public void onStart(){
super.onStart();
}
public void onResume(){
super.onResume();
}
public …
Run Code Online (Sandbox Code Playgroud) 目标:构建一个Android应用程序,发现范围内BT设备的名称和地址,并将其值提交给Web服务.以前没有将BT设备绑定到主机设备,我只是想在我走动时查看所有内容.
我做了什么:
ACTION_FOUNDs
即将到来.事情起作用(使用增量控制台记录测试)直到startDiscovery()
.
挫折:
如果你能够使用这种方法,我将非常感谢你的智慧.
更新 - 这是一个简化的代码简化版本,让我感到悲伤; 这种简化概括了我的错误.此代码运行,它不会抛出任何cat.log
错误或其他错误,它根本不提供任何输出.
package aqu.bttest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class BT2Activity extends Activity {
private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//register …
Run Code Online (Sandbox Code Playgroud) android bluetooth android-intent android-context android-adapter
我正在使用ViewPageIndicator中的ViewPager,我需要能够在其他人的中间动态插入一个片段.
我试图用FragmentPagerAdapter和FragmentStatePagerAdapter(都来自v4支持代码)管理模型,第一个似乎不管理中间插入页面的任何方式.第二个只有当我做一个天真的getItemPosition实现返回总是POSITION_NONE但这会导致每次刷卡时完全重新创建页面.
我用FragmentStatePagerAdapter(FSP)观察到的问题是这样的:
在代码中查看一下后,似乎提供的fragmentStatePagerAdapter不支持在中间插入.这是正确的还是我错过了什么?
更新: 适配器中的插入是以逻辑方式进行的,当某个编码为真时,页面会增加1.片段创建是使用getItem()中的构造函数以这种方式完成的:
void setCondition(boolean condition) {
this.condition=condition;
notifyDataSetChanged();
}
public int getCount(){
return condition?3:2;
}
public Fragment getItem(int position) {
if(position==0)
return new A();
else if(position==1)
return condition?new C():new B();
else if(position==2 && condition)
return new B();
else throw new RuntimeException("Not expected");
}
public int getItemPosition(Object object) {
if(object instanceof A) return 0;
else if(object instanceof …
Run Code Online (Sandbox Code Playgroud) android android-adapter android-viewpager viewpagerindicator
当重写ArrayAdapter我知道使用这样的模式是正确的:
if(view != null){
...create new view setting fields from data
}else
return view; //reuse view
Run Code Online (Sandbox Code Playgroud)
使用CursorAdapters的这种模式也是正确的吗?我的问题是我有一个根据光标字段可以是红色或蓝色的文本颜色,所以我不希望任何错误像一个需要蓝色字段的单元格上的红色.我的bindView代码是这样的:
if(c.getString(2).equals("red"))
textView.setTextColor(<red here>);
else
textView.setTextColor(<blue here>);
Run Code Online (Sandbox Code Playgroud)
如果我重复使用视图,我可以确定红色是红色,而蓝色是蓝色吗?
是否可以将TableLayout与ArrayAdapter绑定?
我正在使用RecyclerView.我使用循环器视图来显示模型类的详细信息.
//My model class
MyModel {
String name;
Double latitude;
Double longitude;
Boolean isOnline;
...
}
Run Code Online (Sandbox Code Playgroud)
由于某些值可能不存在,我使用RecyclerView和自定义视图类型(一个代表我的模型的每个值).
//Inside my custom adapter
public void setModel(T model) {
//Reset values
itemCount = 0;
deviceOfflineViewPosition = -1;
mapViewPosition = -1;
//If device is offline, add device offline item
if (device.isOnline() == null || !device.isOnline()) {
deviceOfflineViewPosition = itemCount;
itemCount++;
}
//Add additional items if necessary
...
//Always add the map as the last item
mapViewPosition = itemCount;
itemCount++;
notifyDataSetChanged();
}
@Override
public int …
Run Code Online (Sandbox Code Playgroud) android google-maps adapter android-adapter android-recyclerview
我有一个基本的自定义视图,如下所示:
public class CustomView extends RelativeLayout {
private User user;
private ImageView profilePicture;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.custom_layout, this);
profilePicture = (ImageView) findViewById(R.id.profilePicture);
// ACCESS USER MODEL HERE
// e.g. user.getUsername()
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我想在View中访问用户数据(即:)user.getUsername()
.
我还需要能够在RecyclerView
适配器中使用自定义视图.
这是我的适配器目前的样子:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<User> userData;
public MyAdapter(Context context, List<User> userData) {
this.context = context;
this.userData …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法将项目添加到ListView而不会导致重新加载整个列表.
我有一个BaseAdapter派生的ListView适配器,当底层模型添加一个新元素时,它调用notifyDataSetChanged(),它触发ListView重新加载.
列表中的元素具有图像,这些图像根据元素的内容动态加载.问题是,当在重新加载期间调用getView()时,传递以供重用的convertView参数之前来自不同的位置,因此也必须重新加载图像,这会导致相当难看的闪烁.
如果我只在最后添加一个项目(这将是添加新项目的唯一方法),那么有没有办法不重新加载整个列表?或者至少以某种方式将单元重用于相同的位置,如果可能的话,以避免昂贵的图像重载?
我首先要说的是,我已经详细阅读了所有关于自定义可检查列表项和选择器的问题.他们中的许多人都有类似的问题,但没有一个答案可以解决我的问题.
在我的应用程序的某一点,我提出了一个自定义列表活动.创建时,它从调用它的intent中检索一组静态数据,并将该数据传递给它的自定义数组适配器.每个列表项都是一个简单的RelativeLayout,它实现了Checkable接口.默认情况下,如果单击其中一个项目,则会显示一个新活动,其中显示有关所选联系人的详细信息.但是,如果长按单击列表中的项目,则会启动ActionMode.此时单击列表中的项目不会显示详细信息活动,它只是将项目设置为选中.然后,如果用户选择动作模式项之一,则它对所检查的项执行动作.
需要了解的一点是,在两个选择"模式"中,单击列表项会将其设置为选中状态.
我上面描述的所有内容都非常有效.我唯一的问题是,即使使用默认选择器,列表项的背景在设置为选中时也不会突出显示.
我想要做的是,有两个选择器:每个选择模式一个.在第一种情况下,检查项目时背景不会改变,而在第二种情况下,背景不会改变.我尝试过实现自定义选择器,但即使在那些state_checked中也会被忽略!选择器的其他部分工作正常,但不是state_checked.
我对CheckableListItem的实现结合了很多不同的例子,所以如果我做错了,或者有更好的方法让我知道!
注意:有趣的一点是,如果我将results_list_item.xml中列表项的背景设置为我的选择器,而不是ListView的listSelector属性,则在选中项时会更改背景.但是,这样做会导致我的选择器中的长按转换不起作用.
ResultsActivity.java:
public class ResultsActivity extends ListActivity implements OnItemLongClickListener {
private ListView listView; // Reference to the list belonging to this activity
private ActionMode mActionMode; // Reference to the action mode that can be started
private boolean selectionMode; // Detail mode or check mode
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
// When the home icon is pressed, go back
ActionBar actionBar = getActionBar(); …
Run Code Online (Sandbox Code Playgroud) 我实际上在我的应用程序中使用了一个带有两列的GridLayoutManager,我希望根据所使用的视图类型有一列.
这是我片段的方法"onCreateView()"中的代码:
// Recycler view for users
usersList = (RecyclerView) mView.findViewById(R.id.usersList);
// Layout manager (grid of users
layoutManager = new GridLayoutManager(mContext, 2);
usersList.setLayoutManager(layoutManager);
// ArrayList of users
users = new ArrayList<User>();
// Set the adapter for the list of users
mHomeListAdapter = new HomeListAdapter(getActivity(), users);
usersList.setAdapter(mHomeListAdapter);
Run Code Online (Sandbox Code Playgroud)
然后,在我的适配器中,我有这样的代码:
@Override
public int getItemViewType(int position) {
if (position == 5) {
return TYPE_PREMIUM;
} else {
return TYPE_ITEM;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_PREMIUM) { …
Run Code Online (Sandbox Code Playgroud) android ×10
android-adapter ×10
adapter ×1
android-view ×1
bluetooth ×1
google-maps ×1
gridview ×1
listview ×1