RecyclerView.ViewHolder未正确绑定

时间:2017-08-14 04:44:41

标签: java android android-recyclerview kotlin

我正在使用java和kotlin构建一个Android应用程序。我对kotlin很新,我的适配器出现问题。由于某种原因,似乎适配器ViewHolder没有正确绑定。我不知道该怎么办,因为我没有收到任何错误消息,我在调试时遇到了麻烦。这是我的适配器的代码:

data class NetworkTest(val addr: String, val port: String, val results: String, val protocol: String)

public class NetTestRecyclerAdapter(val netList: List<NetworkTest>, val context: Context): RecyclerView.Adapter<NetTestRecyclerAdapter.NetTestRecyclerHolder>(){

override fun onBindViewHolder(holder: NetTestRecyclerHolder, position: Int) {
    holder.bindItems(netList[position])
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NetTestRecyclerHolder {
    val v: View = LayoutInflater.from(context).inflate(R.layout.tested_list_item, parent, false)
    return NetTestRecyclerHolder(v)
}

override fun getItemCount(): Int {
    return netList.size
}

class NetTestRecyclerHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    fun bindItems(networkTest: NetworkTest){
        val addrPortView = itemView.find<TextView>(R.id.addrPortView)
        val resultsProtocolView = itemView.find<TextView>(R.id.resultProtocolView)
        addrPortView.text = "Address: ${networkTest.addr} Port: ${networkTest.port}"
        resultsProtocolView.text = "Results: ${networkTest.results} Protocol: ${networkTest.protocol}"
    }
}
}

这是我主要活动的代码:

public class NetTestActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_net_test);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mRecyclerView = (RecyclerView)findViewById(R.id.networkList);
    List toolTestList = new ArrayList<>();
    toolTestList.add(new NetworkTest("addr", "port", "results", "protocol"));
    NetTestRecyclerAdapter adapter = new NetTestRecyclerAdapter(toolTestList, getApplicationContext()); //context may need to be changed

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    mRecyclerView.setAdapter(adapter);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.net_test, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

请帮助我找到我在这里做错了什么。提前感谢您的帮助。

继承我的浏览器的布局文件以及

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/list_item"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

    <TextView
        android:id="@+id/addrPortView"
        android:text="testing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/resultProtocolView"
        android:text="again"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我的recyclerview布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/networkList"
android:layout_height="match_parent"
android:layout_width="match_parent">



</android.support.v7.widget.RecyclerView>

我应该包括这样的内容:

<include
    layout="@layout/activity_net_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

这里是adb dumpsys top

adb shell dumpsys activity top
TASK com.example.zacharymcdaniel.nettest id=4928
ACTIVITY com.example.zacharymcdaniel.nettest/.NetTestActivity 2543227 pid=14548
Local Activity 5de3ad9 State:
  mResumed=true mStopped=false mFinished=false
  mChangingConfigurations=false
  mCurrentConfig={1.0 311mcc870mnc en_US ldltr sw360dp w360dp h569dp 320dpi nrml port finger -keyb/v/h -nav/h s.73 fontTypeIndex0 forceEmbolden0}
  mLoadersStarted=true
  FragmentManager misc state:
    mHost=android.app.Activity$HostCallbacks@86516d4
    mContainer=android.app.Activity$HostCallbacks@86516d4
    mCurState=5 mStateSaved=false mDestroyed=false
ViewRoot:
  mAdded=true mRemoved=false
  mConsumeBatchedInputScheduled=false
  mConsumeBatchedInputImmediatelyScheduled=false
  mPendingInputEventCount=0
  mProcessInputEventsScheduled=false
  mTraversalScheduled=false
  android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
  android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
  android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
Choreographer:
  mFrameScheduled=false
  mLastFrameTime=43338300 (43548 ms ago)
View Hierarchy:
  com.android.internal.policy.PhoneWindow$DecorView{bf7807d V.E...... R....... 0,0-720,1280}
    android.widget.LinearLayout{f35fb72 V.E...... ........ 0,0-720,1187}
      android.view.ViewStub{8c4fbc3 G.E...... ......I. 0,0-0,0 #10203aa android:id/action_mode_bar_stub}
      android.widget.FrameLayout{c66cc40 V.E...... ........ 0,0-720,1187}
        android.support.v7.widget.FitWindowsLinearLayout{380a779 V.E...... ........ 0,0-720,1187 #7f0d0067 app:id/action_bar_root}
          android.support.v7.widget.ViewStubCompat{1645cbe G.E...... ......I. 0,0-0,0 #7f0d0068 app:id/action_mode_bar_stub}
          android.support.v7.widget.ContentFrameLayout{4f1911f V.E...... ........ 0,0-720,1187 #1020002 android:id/content}
            android.support.v4.widget.DrawerLayout{20c6c6c VFED..... ........ 0,0-720,1187 #7f0d007b app:id/drawer_layout}
              android.support.design.widget.CoordinatorLayout{fdb4235 V.E...... ........ 0,48-720,1187}
                android.support.design.widget.AppBarLayout{90688e5 V.E...... ........ 0,0-720,112}
                  android.support.v7.widget.Toolbar{88586ca V.E...... ........ 0,0-720,112 #7f0d007d app:id/toolbar}
                    android.support.v7.widget.AppCompatTextView{a28203b V.ED..... ........ 144,29-283,83}
                    android.support.v7.widget.AppCompatImageButton{589e358 VFED..C.. ........ 0,0-112,112}
                    android.support.v7.widget.ActionMenuView{d0accb1 V.E...... ........ 640,0-720,112}
                      android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{9dd4596 VFED..CL. ........ 0,8-80,104}
                android.support.v7.widget.RecyclerView{b9c3fba VFED..... .F...... 0,0-720,1139 #7f0d007f app:id/networkList}
                  android.widget.LinearLayout{66e8517 V.E...... ........ 0,0-720,76 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{5cc1647 V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{65dd04 V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                  android.widget.LinearLayout{9ce82ed V.E...... ........ 0,76-720,152 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{e5ef9d V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{9202522 V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                  android.widget.LinearLayout{84c5bb3 V.E...... ........ 0,152-720,228 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{fa9cfe3 V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{be5c570 V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                  android.widget.LinearLayout{79d60e9 V.E...... ........ 0,228-720,304 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{b36e899 V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{ebf716e V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                  android.widget.LinearLayout{527000f V.E...... ........ 0,304-720,380 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{81f873f V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{e39c89c V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                  android.widget.LinearLayout{e6222a5 V.E...... ........ 0,380-720,456 #7f0d0050 app:id/list_item}
                    android.support.v7.widget.AppCompatTextView{4047555 V.ED..... ........ 0,0-720,38 #7f0d00a5 app:id/addrPortView}
                    android.support.v7.widget.AppCompatTextView{6f5367a V.ED..... ........ 0,38-720,76 #7f0d00a6 app:id/resultProtocolView}
                android.support.design.widget.FloatingActionButton{cdfe6b VFED..C.. ........ 576,995-688,1107 #7f0d007e app:id/fab}
              android.support.design.widget.NavigationView{95d8e2b I.ED..... ......ID -560,0-0,1187 #7f0d007c app:id/nav_view}
                android.support.design.internal.NavigationMenuView{e68d288 VFED.V... ......ID 0,0-560,1187 #7f0d0087 app:id/design_navigation_view}
                  android.widget.LinearLayout{ff74421 V.E...... ......I. 0,0-560,336 #7f0d0086 app:id/navigation_header_container}
                    android.widget.LinearLayout{5904046 V.E...... ......ID 0,0-560,320}
                      android.support.v7.widget.AppCompatImageView{e24e207 V.ED..... ......ID 32,52-128,180 #7f0d008c app:id/imageView}
                      android.support.v7.widget.AppCompatTextView{afc8f34 V.ED..... ......ID 32,180-528,250}
                      android.support.v7.widget.AppCompatTextView{763015d V.ED..... ......ID 32,250-294,288 #7f0d008d app:id/textView}
                  android.support.design.internal.NavigationMenuItemView{a801ad2 VFED..C.. ......I. 0,336-560,432}
                    android.support.v7.widget.AppCompatCheckedTextView{12397a3 V.ED..... ......ID 32,0-528,96 #7f0d0088 app:id/design_menu_item_text}
                    android.view.ViewStub{f6d6aa0 G.E...... ......I. 0,0-0,0 #7f0d0089 app:id/design_menu_item_action_area_stub}
                  android.support.design.internal.NavigationMenuItemView{fd35659 VFED..C.. ......I. 0,432-560,528}
                    android.support.v7.widget.AppCompatCheckedTextView{321121e V.ED..... ......ID 32,0-528,96 #7f0d0088 app:id/design_menu_item_text}
                    android.view.ViewStub{9ce0aff G.E...... ......I. 0,0-0,0 #7f0d0089 app:id/design_menu_item_action_area_stub}
                  android.support.design.internal.NavigationMenuItemView{44e90cc VFED..C.. ......I. 0,528-560,624}
                    android.support.v7.widget.AppCompatCheckedTextView{f59ff15 V.ED..... ......ID 32,0-528,96 #7f0d0088 app:id/design_menu_item_text}
                    android.view.ViewStub{448322a G.E...... ......I. 0,0-0,0 #7f0d0089 app:id/design_menu_item_action_area_stub}
                  android.support.design.internal.NavigationMenuItemView{882581b VFED..C.. ......I. 0,624-560,720}
                    android.support.v7.widget.AppCompatCheckedTextView{639edb8 V.ED..... ......ID 32,0-528,96 #7f0d0088 app:id/design_menu_item_text}
                    android.view.ViewStub{1687791 G.E...... ......I. 0,0-0,0 #7f0d0089 app:id/design_menu_item_action_area_stub}
                  android.support.design.internal.NavigationMenuItemView{90f46f6 VFED..C.. ......I. 0,720-560,816}
                    android.support.v7.widget.AppCompatCheckedTextView{b645af7 V.ED..... ......ID 32,0-528,96 #7f0d0088 app:id/design_menu_item_text}
                    android.view.ViewStub{f7c2d64 G.E...... ......I. 0,0-0,0 #7f0d0089 app:id/design_menu_item_action_area_stub}
    android.view.View{d0bfbcd V.ED..... ........ 0,1187-720,1280 #1020030 android:id/navigationBarBackground}
    android.view.View{260dc82 V.ED..... ........ 0,0-720,48 #102002f android:id/statusBarBackground}
Looper (main, tid 1) {4f9af93}
  (Total messages: 0, polling=false, quitting=false)
Local FragmentActivity 5de3ad9 State:
  mCreated=truemResumed=true mStopped=false mReallyStopped=false
  mLoadersStarted=true
FragmentManager misc state:
  mHost=android.support.v4.app.FragmentActivity$HostCallbacks@80bbd0
  mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@80bbd0
  mCurState=5 mStateSaved=false mDestroyed=false

1 个答案:

答案 0 :(得分:0)

看起来你的适配器几乎没问题,但是你可以写你的视图持有人,这可能会有所帮助

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val addrPortView = view.findViewById(R.id.addrPortView) as TextView
        fun bind(networkTest: NetworkTest){
            addrPortView .text = "Address: ${networkTest.addr} Port: 
                                  ${networkTest.port}"
        }
    }