Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have done the following code in android studio.
now i want to view a each element of the listview in a separate view when it is clicked

MAINACTIVITY.JAVA
Java
package com.example.ramneekkashyap.app2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
    Button _loginBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _loginBtn = (Button) findViewById(R.id.btn_login);

        _loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //This is a comment which does no good to your code. Feel free to remove it after you copy paste.
                //When the button is clicked, the control will come to this method.
                //To demonstrate this, let us try changing the label of the Button from 'Login' to 'I am clicked'

                Intent intent = new Intent(MainActivity.this, ORGANISATIONListActivity.class);
                startActivity(intent);
            }

        });//check if the current Activity needs to be skipped {
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}




ACTIVITY_MAIN.XML
Java
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:removed="#ffffffff">

    <textview android:text="@string/hello_ORGANISATION" android:layout_width="match_parent">
        android:layout_height="wrap_content"
        android:removed="#ff7bffff"
        android:textColor="#ff000000"
        android:textSize="26dp"
        android:id="@+id/textView" />

    <linearlayout>
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textView"
        android:id="@+id/linearLayout">

        <textview>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="User Name"
            android:id="@+id/textView2"
            android:textColor="#ff000000" />

        <edittext>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/id_of_the_username_field"
            android:layout_weight="1" />
    </edittext></textview></linearlayout>

    <linearlayout>
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout"
        android:id="@+id/linearLayout2">

        <textview>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Password: "
            android:id="@+id/textView3"
            android:textColor="#ff000000" />

        <edittext>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_weight="1" />
    </edittext></textview></linearlayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:id="@+id/btn_login"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:textSize="25dp" />

</textview></relativelayout>


ORGANISATIONLISTACTIVITY.JAVA

Java
package com.example.ramneekkashyap.app2;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ORGANISATIONListActivity extends ListActivity {

    private ArrayAdapter ORGANISATIONItemArrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ORGANISATION_list);
        ORGANISATIONItemArrayAdapter = new ORGANISATIONAdapter(this, new String[10]);
        setListAdapter(ORGANISATIONItemArrayAdapter);
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        TextView t = (TextView) v.findViewById(R.id.ORGANISATIONTitle);
        t.setText("ORGANISATION Clicked");
    }


}


ORGANISATIONADAPTER.JAVA
Java
package com.example.ramneekkashyap.app2;

import android.view.ViewGroup;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;





public class ORGANISATIONAdapter extends ArrayAdapter{
    private LayoutInflater inflater;

    public ORGANISATIONAdapter(Activity activity, String[] items){
        super(activity, R.layout.row_ORGANISATION, items);
        inflater = activity.getWindow().getLayoutInflater();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return inflater.inflate(R.layout.row_ORGANISATION, parent, false);
    }

}


ACTIVITY_ORGANISATION_ADAPTER.XML
Java
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ramneekkashyap.app2.ORGANISATIONAdapter">

    <textview android:text="@string/hello_world" android:layout_width="wrap_content">
        android:layout_height="wrap_content" />

</textview></relativelayout>


ROW_ORGANISATION.XML

Java
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/user_profile"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp" />


    <linearlayout>
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="left" >


        <textview>
            android:id="@+id/ORGANISATIONTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_removed"
            android:text="Header Text"
        android:textSize="19sp"
        android:textColor="#222222"
        android:textStyle="bold" />


        <textview>
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ORGANISATION body text here"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:lines="3"
        android:textColor="#666666"
        android:textSize="14sp" />


        <textview>
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="20 Nov 2013"
        android:layout_marginTop="5dp"
        android:textColor="#999999"
        android:textSize="12sp" />


    </textview></textview></textview></linearlayout>


</linearlayout>




ACTIVITY_ORGANISATION_LIST.XML
Java
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ramneekkashyap.app2.ORGANISATIONListActivity$PlaceholderFragment" >

    <listview>
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </listview>

</relativelayout>
Posted
Updated 17-Mar-15 0:43am
v2
Comments
Sandeep Londhe 17-Mar-15 15:02pm    
what is the problem in the code? you should post only problem not the whole code
Member 11402033 17-Mar-15 21:16pm    
I HAD USED THIS CODE TO CREATE A LISTVIEW, NOW I WANT TO CREATE ANOTHER VIEW FOR EACH ELEMENT OF THE LIST VIEW, WHICH DETAILS IT. JUST LIKE A NEWS APP, WHEN U CLICK ON ANY NEWS, VIEW CHANGES TO THE DETAIL NEWS, LIKE THAT I NEED

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900