2016-07-29 108 views
0

我有一個listview兩行項目:用戶ID和用戶名。當單擊其中一行時,如何從listview中僅檢索用戶標識?Android - ListView佈局兩行列表項

public class MainActivity extends AppCompatActivity { 

private DatabaseReference mUsers; 
String getValue = null; 
ListView lvUsers = (ListView) findViewById(R.id.lvUsers); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    mUsers = FirebaseDatabase.getInstance.getReference().child(Constants.FIREBASE_USERS); 

    FirebaseListAdapter<Users> adapter = new FirebaseListAdapter<Users>(
      this, Users.class, android.R.layout.two_line_list_item, mUsers 
    ) { 
      @Override 
      protected void populateView(View v, Users user, int pos) { 
      ((TextView)v.findViewById(android.R.id.text1)).setText(user.getUserID()); 
      ((TextView)v.findViewById(android.R.id.text2)).setText(user.getUsername()); 
      } 
    }; 

    lvUsers.setAdapter(adapter); 
    setupListViewMenu(); 
} 

private void setupListViewMenu() { 
    lvUsers.setOnItemLongClickListener(
      new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> adapter, 
             View item, int pos, long id) { 

      // How to retrieve only User ID from here? 
      getValue = ((TextView)view).getText().toString(); 

      return true; 
     } 

    }); 
} 
} 

listview數據來自於Firebase數據庫。使用for loop唯一方法檢索?我必須通過二維數組循環?謝謝

回答

1

你能試下面的代碼嗎?

getValue = ((TextView)item.findViewById(android.R.id.text2)).getText().toString(); 

我相信android.R.id.text2itemView在clickListener收到)孩子的

之一,但需要對其進行測試...希望工程