添加新项目时未调用onCreateViewHolder

时间:2015-11-11 20:04:39

标签: android android-recyclerview wear-os

我正在开发Android Wear上的第一个应用程序。

手机通过compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig -lwps -lcrypto -laxtls 发送数据。手表会在Wearable.DataApi.putDataItem()

中检索数据

WearableListenerService启动此WearableActivity并向其注册Service,以便动态获取每个新数据。

然后,我想在Receiver中添加每个新数据。

一切都很完美。 MainActivity获取所有必需的数据并将其添加到适配器。

事实是我没有显示任何项目。

这是我的MainActivity的代码,因为问题可能来自适配器。

WearableListView.Adapter

如果我将登录public class MainActivity extends WearableActivity { private WearableListView mListView; private DataReceiver mDataReceiver; private MyAdapter myAdapter; @Override protected void onStart() { mDataReceiver = new DataReceiver(); mDataReceiver.setListener(new DataReceiver.DataReceivedListener() { @Override public void onDataReceived(JSONObject jsonObject) { myAdapter.addItem(jsonObject); myAdapter.notifyDataSetChanged(); } }); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ListenerService.GET_PHONE_DATA_ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(mDataReceiver, intentFilter); Intent intent = new Intent(MainActivity.this, ListenerService.class); startService(intent); super.onStart(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (WearableListView) findViewById(R.id.wearable_list); MyAdapter = new MyAdapter(getApplicationContext()); mListView.setAdapter(myAdapter); } catch (JSONException e) { e.printStackTrace(); } } public class MyAdapter extends WearableListView.Adapter { private List<JSONObject> mList; private final Context mContext; private final LayoutInflater mInflater; public MyAdapter (Context context) { mList= ArrayList<>(); mContext = context; mInflater = LayoutInflater.from(context); } public void addItem(JSONObject item) { mList.add(item); notifyDataSetChanged(); } public class ItemViewHolder extends WearableListView.ViewHolder { private TextView textView1; private TextView textView2; public ItemViewHolder(View itemView) { super(itemView); textView1= (TextView) itemView.findViewById(R.id.tv1); textView2= (TextView) itemView.findViewById(R.id.tv2); } } @Override public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ItemViewHolder(mInflater.inflate(R.layout.custom_view, null)); } @Override public void onBindViewHolder(WearableListView.ViewHolder holder, int position) { ItemViewHolder itemHolder = (ItemViewHolder) holder; TextView textView1 = itemHolder.textView1; TextView textView2 = itemHolder.textView2; JSONObject item; try { item = mList.get(position); textView1.setText(item.getString("title")); textView2.setText(item.getString("name")); } catch (JSONException e) { Log.e("MyAdapter", e.getMessage()); } holder.itemView.setTag(position); } @Override public int getItemCount() { Log.d("Test", "SIZE ==" + mList.size()); return mList.size(); } } @Override protected void onStop() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mDataReceiver); super.onStop(); } public static class DataReceiver extends BroadcastReceiver { public interface DataReceivedListener { void onDataReceived(JSONObject jsonObject); } private DataReceivedListener mDataReceivedListener; public void setListener(DataReceivedListener dataReceivedListener) { mDataReceivedListener = dataReceivedListener; } @Override public void onReceive(Context context, Intent intent) { if (intent != null && intent.getStringExtra("data") != null) { if (mDataReceivedListener != null) { try { mDataReceivedListener.onDataReceived(new JSONObject(intent.getStringExtra("data"))); } catch (JSONException e) { Log.e("DataReceiver", "Could not read JSON data"); } } } } } 以便将JSONObject置于当前位置,结果似乎是合法的。即使onBindViewHolder也是连贯的。

问题是onCreateViewHolder只被调用两次,ListView上没有显示任何内容。

我做错了什么?我尝试了很多东西,补丁等,但它只会让一切变得更糟。

编辑:这是布局

getItemCount

enter image description here 这是HierarchyViewer结果。变量名称(textviews)不同,但它们的代码相同。

TextViews正确填充了相关数据。

1 个答案:

答案 0 :(得分:2)

如果您仔细检查了您的适配器(数据层)是否正常工作,则最有可能出现表示层问题,例如您可能在白色背景上有白色文本,在这种情况下Hierarchy Viewer是非常有帮助