menu_on_top
menu_on_top

Reputation: 2613

Open a Dialog on ListView item click

I want to open a Dialog every time I click on a ListView item.

This code is not working and I really cant find error. Please help!

 private void loadFeed(){
            try{
                BaseFeedParser parser = new BaseFeedParser();
                messages = parser.parse();
                List<String> descriptions = new ArrayList<String>();
                List<String> titles = new ArrayList<String>(messages.size());
                for (Message msg : messages){
                     descriptions.add(msg.getDescription());
                    titles.add(msg.getTitle() + "\n" +msg.getDate());
                }
                ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String>(this, R.layout.row,titles);
                this.setListAdapter(adapter);
            } catch (Throwable t){
                Log.e("AndroidNews",t.getMessage(),t);
            }
        }


        @Override
        protected void onListItemClick(ListView descriptions, 
                    View v, int position, long id) {
            super.onListItemClick(descriptions, v, position, id);
            String description = descriptions.get(position);
            Dialog dialog = new Dialog(this);
                dialog.setContentView(R.layout.single);
                dialog.setTitle("Blog");
                dialog.setCancelable(true);
                TextView text = (TextView) dialog.findViewById(R.id.TextView1);
                text.setText(description);
                dialog.show();

        }

with this code the app is running ,the dialog show the description but the description is also shown in the list item.

messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
    titles.add(msg.getTitle() + "\n" +msg.getDate() + "\n\n" + msg.getDescription());
 }
ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String selection = l.getItemAtPosition(position).toString();
        Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.row2)
            dialog.setCancelable(true);
            TextView text = (TextView) dialog.findViewById(R.id.SinglePost);
            text.setText(selection);
            dialog.show();

    }

Upvotes: 2

Views: 7742

Answers (2)

bigmerol
bigmerol

Reputation: 13

i'm doing something like that ... and it helps me to much. I't's not mine so please give the credit whom it deserves.

http://stackoverflow.com/questions/6467140/how-to-open-dialog-from-listview

Upvotes: 0

Octavian Helm
Octavian Helm

Reputation: 39604

This is not going to work because the method get(int i) does not exist for the type ListView nor for the ArrayAdapter.

EDIT:

You seem to confound the ListView class and the List interface. Those are two totally different things!

A class implementing the List interface like the ArrayList in your case holds objects and a ListView class is and Android widget which displays Views in a list representation.

I really recommend you working through the Hello Views tutorial section so you get a basic understanding of the Android views before diving into more complex stuff.

Upvotes: 3

Related Questions