Kev
Kev

Reputation: 173

Android: Display list items to new view

How do you display the items in list view? I mean, when you clicked on an item of the list view it will display on a new view, just plain textview. Can anyone help me on this? I want to display it in two textviews.

Here's the code I got:

@Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Cursor c = mMessagesCursor;
            c.moveToPosition(position);
            Intent i = new Intent(this, KAHTextApp.class);
            i.putExtra(MessagesDBAdapter.KEY_ROWID, id);
            i.putExtra(MessagesDBAdapter.KEY_RECIPIENT, c.getString(
                    c.getColumnIndexOrThrow(MessagesDBAdapter.KEY_RECIPIENT)));
            i.putExtra(MessagesDBAdapter.KEY_MESSAGE, c.getString(
                    c.getColumnIndexOrThrow(MessagesDBAdapter.KEY_MESSAGE)));
            startActivityForResult(i, ACTIVITY_EDIT);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            Bundle extras = intent.getExtras();
            switch(requestCode) {
                case ACTIVITY_CREATE:
                    String recipient = extras.getString(MessagesDBAdapter.KEY_RECIPIENT);
                    String message = extras.getString(MessagesDBAdapter.KEY_MESSAGE);
                    mDbHelper.createNote(recipient, message);
                    fillData();
                    break;
                case ACTIVITY_EDIT:
                    Long rowId = extras.getLong(MessagesDBAdapter.KEY_ROWID);
                    if (rowId != null) {
                        String editTextRecipient = extras.getString(MessagesDBAdapter.KEY_RECIPIENT);
                        String editTextNewMessage = extras.getString(MessagesDBAdapter.KEY_MESSAGE);
                        mDbHelper.updateNote(rowId, editTextRecipient, editTextNewMessage);
                    }
                    fillData();
                    break;
            }

So basically, when I clicked on the list view a new activity will come to front showing just the two textviews namely, the recipient and the message.

Upvotes: 0

Views: 527

Answers (3)

eMich
eMich

Reputation: 1736

You mean starting a new activity when you click?

In this case, just add an OnItemClickListener on your ListView https://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener) that will have the code to open the new activity.

If using a ListActivity, just override the onListItemClicked method and have your code in there.

Upvotes: 0

Dante
Dante

Reputation: 1094

What you do with an onclick event is all up to you, so I don't understand why you will get only one textview.

If I click on a listitem in a listview, then I set an OnItemClickListener with an OnItemClick event.

For a simple example check the developers http://developer.android.com/resources/tutorials/views/hello-listview.html

After your edit I see you start a new Activity (KAHTextApp). In this activity you can define what you want to be shown.

Upvotes: 0

April Smith
April Smith

Reputation: 1830

Listactivity.class

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent intent = new Intent(view.getContext(), yourclasname.class);    
intent.putExtra(String name, String[] value); 
.
.
.

startActivity(intent);

       }
          });

Fulldetail.class

Bundle extras = getIntent().getExtras();
        if (extras != null) {

        String stringTitle = extras.getString(String string);

if (title != null) {
            title.setText(stringTitle); 

}

Upvotes: 1

Related Questions