0

I am currently learning android and working on a few projects...in this one I have a list view and when I click it I pass the name of the row I click and a value from a database to my NEXT activity and that works 100%.

This is part of my main Activity on create code. where I populate the listview from my database here and other stuff.

lvUsers = (ListView)findViewById(id.lvUsers);
    List<String> listUsers = dbHeplper.getAllUsers();

    if(listUsers != null){

        adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1,listUsers);

       //  adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);


        lvUsers.setAdapter(adapter);

        lvUsers.setOnItemClickListener(new OnItemClickListener(){


            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String product= ((TextView)view).getText().toString();

              //  String product= String.valueOf((TextView)findViewById(id.movie_title));
                // Toast.makeText(getBaseContext(),product,Toast.LENGTH_LONG).show();

                Intent x = new Intent(getApplicationContext(), SingleListItem.class);
                // sending data to new activity
                 x.putExtra("product", product);
                startActivity(x);

            }
        });

This code works fine, but I have attempted to style my row myself if you look at this line. (which is commented in the main view)

adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.test, id.movie_title,listUsers);.

Adding this threw the error of relativelayout cannot be cast to android widget textview on the line

String product= ((TextView)view).getText().toString();

So I took that to mean it does not know which textview I am referring to anymore. so I edited that line to

String product= String.valueOf((TextView)findViewById(id.movie_title));

because movie_title is the ID of the textview in the test layout.

The then disappeared but now when I click the row instead of getting that data I expected like row name and data from database. the textview in my second activity is displaying

"android.widget.TextView{176f155.v.Ed (more random numbers) app:id/movie_title}

(Some of the code from second activity

 Intent i=getIntent();
    String name = i.getStringExtra("product");
     txtName.setText(name);

Like I said all these errors are occurring from me trying to implement my owned custom rows. So If anyone could point out where I went wrong while doing this I would be grateful .

Liberace
  • 81
  • 1
  • 11

1 Answers1

0

Have you tried creating a custom adapter and making it extend ArrayAdapter ? You can do this easily and then inside your getView() of your custom adapter you can inflate your textview with your appropriate text . Check this : https://stackoverflow.com/a/8166802/1497188

Also String product= String.valueOf((TextView)findViewById(id.movie_title)); , this line is getting the ID of the view and not the text inside the view. The appropriate view to do it is that from inside your onItemClick() you would convert/cast your view into a textview and then do the getText().toString() on that view. Just Search online how to create custom adapters and onItemClick Listener for Adapter

EDIT:

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String data=(String)adapterView.getItemAtPosition(arg2); 

This will atleast help you get the string from inside your onItemClick()

Community
  • 1
  • 1
Android2390
  • 1,150
  • 12
  • 21
  • Thanks for the reply, but don't I already have an onITemClick Listener? – Liberace Apr 07 '15 at 00:25
  • Yes you do but if you want your custom view inside listview to work correctly and have more control then creating a custom adapter is needed. Also i added a edit check that – Android2390 Apr 07 '15 at 00:58