-1

Here is my code.

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());


        }
    });

This is my code, I want the text to be clickable and change from one page to another page. What should I do?

Jean Eric
  • 73
  • 2
  • 11
郭奇靂
  • 19
  • 4
  • where is the text? – John Joe Jun 28 '17 at 10:00
  • 2
    Possibly a duplicate of: https://stackoverflow.com/questions/16226125/how-do-i-properly-use-an-setonitemclicklistener – abhi Jun 28 '17 at 10:24
  • 1
    Possible duplicate of [How do I properly use an setOnItemClickListener?](https://stackoverflow.com/questions/16226125/how-do-i-properly-use-an-setonitemclicklistener) – rmcsharry Jun 28 '17 at 10:55

3 Answers3

1

You can use onItemClickListner for ListView

Please try below code

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);


        }
    });
pawan kasar
  • 170
  • 1
  • 6
0

**** This is how you can use 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);


            }
        });
Arul Harsh
  • 133
  • 9
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);
     } 
});
yash786
  • 1,151
  • 8
  • 18