我如何使用setOnItemClickListener

时间:2017-06-28 09:58:36

标签: android firebase firebase-realtime-database

这是我的代码。

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    mRef = FirebaseDatabase.getInstance().getReference();
    list = (ListView)findViewById(R.id.listview);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            android.R.id.text1);
    list.setAdapter(adapter);

    DatabaseReference reference_contacts = FirebaseDatabase.getInstance().getReference("商品");
    reference_contacts.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            adapter.clear();
            for (DataSnapshot ds : dataSnapshot.getChildren() ){
                adapter.add(ds.child("titles").getValue().toString());
            }
        }
        @Override
        public void onCancelled(DatabaseError error) {

            Log.w(TAG, "Failed to read value.", error.toException());


        }
    });

这是我的代码,我希望文本可以点击并从一个页面更改为另一个页面。 我该怎么办?

3 个答案:

答案 0 :(得分:1)

您可以将onItemClickListner用于ListView

请尝试以下代码

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(getApplicationContext(), "Tap Me.. " + arraylist.get(position).getFirstName() + " " + arraylist.get(position).getLastname(), Toast.LENGTH_SHORT).show();

            Intent nextActivity = new Intent(getApplicationContext(),SingleItem.class);


           startActivity(nextActivity);


        }
    });

答案 1 :(得分:0)

****这就是你如何使用onitemclick ****

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "click on " + arraylist.get(position).getName() + " " + arraylist.get(position).getLastname(), Toast.LENGTH_SHORT).show();

                Intent i = new Intent(getApplicationContext(),SingleItem.class);


               startActivity(i);


            }
        });

答案 2 :(得分:0)

You can Simply use

list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     {
     Object listItem = list.getItemAtPosition(position);
     } 
});