-1

Ok, I am facing a problem while inflating a layout for listitem click. I am very new to android so please consider me in learning Phase. I have successfully implemented the customize listview after 1 day of effort. As I told, I am newbie.

But now I want to make a listitem clickable, like when I click on list item it inflate a layout which have a imageview and a textview.

On each click these two imageview and textview will take the value from array according to position click.

I can do this by using the intent like this :

 @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    if (position == 0) {
        Intent int0 = new Intent(getApplicationContext(), Activity.class);
        startActivity(int0);
    }

But let supposed the situation where I have 1000 items in array,and i need to make each item
clickable. So in this case their will need of 1000 new class's and layout for intent that i don't wants.

I don't know, that how could I explain it. But i will try:

Please see picture for more clearance :

I have implemented the customize listview.

enter image description here

Now what I want to do :

When a user click on item then get a view like this: means top screen will show the image and bottom screen will show the text, that are coming from array's.

enter image description here

Here is my complete code for reference : MainActivity.java

  package com.diljeet.customlistview;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
    ListView list;
    String[] web = { "Diljeet", "sweet", "kaur", "preet", "manjeet", "dillun",
            "rupal" };
    Integer[] imageId = { R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4, R.drawable.image5,
            R.drawable.image6, R.drawable.image7

    };

    String textonclick[] = { "a", "b", "c", "d", "e", "f", "i", "j", "k" };

    Integer[] imageonclick = { R.drawable.r, R.drawable.ra, R.drawable.rb,
            R.drawable.rc, R.drawable.rd, R.drawable.re, R.drawable.rf

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomList adapter = new CustomList(MainActivity.this, web, imageId);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

             @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    intent.putExtra("key",web[position]);
                    intent.putExtra("key1",imageonclick[position]);
                    intent.putExtra("key2",textonclick[position]);
                    startActivity(intent); 

                }
            });

    }

}

CustomList.java

package com.diljeet.customlistview;


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

public class CustomList extends ArrayAdapter<String>{

  Activity context;
  String[] web;
  Integer[] imageId;
  CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}

SecondActivity.java

package com.diljeet.customlistview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class SecondActivity extends Activity {

    public void oncreate(Bundle savedInstanseState) {
        super.onCreate(savedInstanseState);
        setContentView(R.layout.getintant);

        TextView txtnew = (TextView) findViewById(R.id.get_txt);
        ImageView imgnew = (ImageView) findViewById(R.id.get_img);

        Intent i = getIntent();
        // Receiving the Data
        String name = i.getStringExtra("key");
        String email = i.getStringExtra("key2");
        txtnew.setText(name);

        Bitmap image = imgnew.getDrawingCache();
        Bundle extras = getIntent().getExtras();
        Bitmap bmp = (Bitmap) extras.getParcelable("key1");

        image.setImageBitmap(bmp);

    }

}

activity_main.xml

  <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" >

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

</RelativeLayout>

list_single.xml

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_height="wrap_content" />

</RelativeLayout>

getintant.xml

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

    <ImageView
        android:id="@+id/get_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/get_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:textSize="20sp" />

</RelativeLayout>

One thing i want to say that onclick we are getting the value from array's, textonclick,imageonclick from MainActivity.

Many many thanks in advance, and please pardon me for complex explanation.

Diljeet
  • 21
  • 9
  • you can use intent to pass values between activities – Raghunandan Jan 06 '14 at 05:32
  • @ Raghunandan, I have few query, what is use of SecondActivity. Is this for getting the intent?. I have to create this? and is this single intent will work for all list item click. – Diljeet Jan 06 '14 at 06:03
  • No answer is working till now............. – Diljeet Jan 07 '14 at 06:27
  • post your updated code for further help' – Raghunandan Jan 07 '14 at 06:28
  • @Raghunandan, Please find the updated code. – Diljeet Jan 07 '14 at 06:58
  • What is not working now? – Raghunandan Jan 07 '14 at 07:31
  • on click it shows a blank screen. – Diljeet Jan 07 '14 at 07:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44678/discussion-between-raghunandan-and-diljeet) – Raghunandan Jan 07 '14 at 07:44
  • i see nothing wrong it works fine for me. I used your code posted here. – Raghunandan Jan 07 '14 at 08:28
  • @Raghunandan, have u seen the image also on click.....i am not getting image on click...but only getting text. – Diljeet Jan 07 '14 at 09:23
  • pls see my edited post for image – Raghunandan Jan 07 '14 at 09:25
  • So how should i correct this, Means what correction you should suggest. – Diljeet Jan 07 '14 at 09:25
  • You have written, R.drawable.ic_launcher....but this for specific image we need the corresponding image(images from imageonclick array) on click...not the particular one. – Diljeet Jan 07 '14 at 09:32
  • Do you ever read the docs. Have you tried before commenting. You din't read the docs. you din't try but you comment. this is not good – Raghunandan Jan 07 '14 at 09:33
  • may i know that who and why voted me down, instead of giving an answer. Have some one noted down that this question got only 2 answer after 59 visits, means it was bit complex to solve. – Diljeet Jan 08 '14 at 05:03
  • this is not complex to solve. the docs already had an example and i directed you there. If you have any issue regarding how the site is run you take up the issue here http://meta.stackoverflow.com/ instead of commenting. The moderators will answer you. Secondly read this http://meta.stackexchange.com/questions/27534/see-who-is-upvoting-downvoting-my-question-answer – Raghunandan Jan 08 '14 at 05:16
  • Yaa i got the help from meta.stackoverflow.com, their was clearly mentioned that leave a comment to know who & why you got the vote down. Ok leave it, Now really thanks to help me and also vote me down. – Diljeet Jan 08 '14 at 05:23
  • Also there they have mentioned something about research effort to be shown. If you are pointed to a working example you should be able to implement it. And you were expecting a code. May be the reason for downvote. There is no reason to leave a comment. I do leave a comment if i downvote. I also indicate what is wrong. If you still have a issue take it up with moderators and they will take action appropriately – Raghunandan Jan 08 '14 at 05:25

2 Answers2

1

Use the below. You can pass values between activities using intent.

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            intent.putExtra("key",web[position]);
            intent.putExtra("key1",imageonclick[position]);
            intent.putExtra("key2",imageId[position]);
            startActivity(intent); 

        }
    });

Edit:

For image

int id = i.getIntExtra("key2",R.drawable.ic_launcher);
imgnew.setImageResource(id);

http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int)

public int getIntExtra (String name, int defaultValue)

Added in API level 1
Retrieve extended data from the intent.

Parameters
name    The name of the desired item.
defaultValue    the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Many thnks for your valuable answer, but how i will get the intent for all click. – Diljeet Jan 06 '14 at 06:09
  • @Diljeet just use the above you will `imageonclick[position]`. position gives the index of the item in listview. Listview index start from 0. in the second activity retrieve it and display the same – Raghunandan Jan 06 '14 at 06:21
  • @Diljeet SecondActivity is another activity class where you can retrieve the items and display. There is example inthe docs for you to refer – Raghunandan Jan 06 '14 at 06:30
  • @Diljeet you should look at the developer site rather than asking me http://developer.android.com/training/basics/firstapp/starting-activity.html. there is an example of how to pass values using intents – Raghunandan Jan 06 '14 at 06:54
  • I followed the same. But getting the blank Screen on click. – Diljeet Jan 06 '14 at 07:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44589/discussion-between-diljeet-and-raghunandan) – Diljeet Jan 06 '14 at 07:52
  • @Diljeet read the docs before commenting http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,int). – Raghunandan Jan 07 '14 at 09:34
0
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {

   Intent int0 = new Intent(getApplicationContext(), Activity.class);
   int0.putExtra("text",textonclick[position]);
   startActivity(int0);
}

use this to pass your list item values from one activity to another .

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • Thanks for answer, I dont want to get the single text. But i need to get the text and image both from arrar's,As i showed in reference images. – Diljeet Jan 06 '14 at 06:19