0

In my application I am displaying list view with image and text in that i added one button called read more if i click the read more button i want to move to next activity.

can any one please help.

OurTeam class

public class OurTeam extends Activity {
    Button click;

    // Array of strings storing country names
     String[] countries = new String[] {
                "Arun Arora Chairman, Edvance Group",
                "Anshul Arora CEO, Edvance Group ",
                "Ranjan Goyal CEO, Edvance Pre-Schools "

        };

    // Array of integers points to images stored in /res/drawable-ldpi/
     int[] flags = new int[]{
                R.drawable.arun_arora_chairman,
                R.drawable.anshul_arora_ceo,
                R.drawable.ranjan_goyal_ceo




        };

    // Array of strings to store currencies


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ourteam1);      

       click = (Button)findViewById(R.id.click);

        //click.setBackgroundColor(Color.TRANSPARENT);
        /*click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //click.setVisibility(View.INVISIBLE);
                //rl.setBackgroundResource(R.drawable.voted);               
                //mp.start();       
                Intent in = new Intent(getApplicationContext(), AboutUs.class);
                startActivity(in);
                //text.setText("Thanks for Voting S K Vel Election Date is on 24th April 2014. Please come and vote for your S K Vel. Our Party Symbol is Drum.");
                //text.setVisibility(View.VISIBLE);
            }
        }); */

        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<3;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", countries[i]);

            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt"};

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.ourteam, from, to);


        // Getting a reference to listview of main.xml layout file
        final ListView listView = ( ListView ) findViewById(R.id.listview);

        // Setting the adapter to the listView
        listView.setAdapter(adapter);  

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                switch (position) {
                case R.id.click:
                    Intent in = new Intent(getApplicationContext(), AboutUs.class);
                    startActivity(in);
                      break;

                }


            }


        });


    }



}

ourteam.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/layout_bg"    
     >

    <ImageView 
            android:id="@+id/flag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/hello"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"        

            />

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"            
            >    

            <TextView 
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="16sp" 
                android:textStyle="bold"            
            />   
             <Button
                android:id="@+id/click"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"

                android:text="Read More" />       



        </LinearLayout>
</LinearLayout>

ourteam1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:divider="@android:color/transparent"
        android:dividerHeight="10.0sp"

    />
</LinearLayout>
user1
  • 17
  • 8

4 Answers4

0

i think your using a custom adapter for ListView, then use the Button OnClick in Adapter getView()..

 holder.yourbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                Intent in = new Intent(context, AboutUs.class);
                context.startActivity(in);
            }
         }
Rajesh Mikkilineni
  • 854
  • 10
  • 22
0

You have to use the Custom Adapter.

  1. Create a class and extends with BaseAdapter
  2. In getView Methods you must add the Click event of that button and specific action on that button
  3. Set the Listview this adapter in main Activity.
Sushil
  • 147
  • 1
  • 9
0

Your problem is in the onItemClick method. You can read this to understand the parameters from this method.

You need to check the position differently:

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1,
          int position, long arg3) {

         switch (position) {
         case 0: // For "Arun Arora Chairman, Edvance Group"
             Intent in = new Intent(getApplicationContext(), AboutUs.class);
             startActivity(in);
             break;
         case 1: // For "Anshul Arora CEO, Edvance Group "
             Intent in = new Intent(getApplicationContext(), AboutUs.class);
             startActivity(in);
             break;
         ...
         }
}

If you want to launch the same Activity, you can remove the switch case.

This method will trigger the onItemClick even if the click is outside the button, if you want to restrict it to the button you need to create a CustomAdapter.

calimbak
  • 856
  • 13
  • 21
  • what is using adding button – user1 Jul 30 '14 at 07:56
  • hi giant00 if i click the readmore button i want to move to next activity this is my question – user1 Jul 30 '14 at 08:03
  • You need to follow the link in my answer and do a `CustomAdapter`. You will also find how to do it [here](http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view). – calimbak Jul 30 '14 at 08:13
0

add the following code below this line click = (Button)findViewById(R.id.click); in your code .

click.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent in = new Intent(OurTeam.this, AboutUs.class);
            startActivity(in);

        }
    });
nobalG
  • 4,544
  • 3
  • 34
  • 72