0

I have a custom listview with image title and description. I want every row show custom activity for example see this pic -> my pictures stored in drawable folder in res LINK TO MY PROJECT PLEASE Here

enter image description here

Here is the link to my project ANDROID STUDIO.

I'm trying this almost for 2 weeks everyday searching but didn't find anything

    activity_my.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"
    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=".MyActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:src="@drawable/allah"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView3"
    android:layout_alignTop="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textView"
    android:layout_below="@+id/textView3"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_alignRight="@+id/textView3"
    android:layout_alignEnd="@+id/textView3" />

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">Is Jesus (pbuh) God?</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="titles">
    <item>What is Islam?</item>
    <item>True God?</item>
    <item>How Islam shown on media?</item>
    <item>Is Jesus (pbuh) God or Son of God?</item>
    <item>How many books has Allah Almighty sent down?</item>
    <item>Who is Muhammad (saw)?</item>
    <item>Are Muslims terroists?</item>
    <item>Love in Islam?</item>
    <item>Rights of women in Islam?</item>
    <item>Ask yourself these questions?</item>
</string-array>
<string-array name="description">
    <item>What is Islam?</item>
    <item>True God?</item>
    <item>How Islam shown on media?</item>
    <item>Is Jesus (pbuh) God or Son of God?</item>
    <item>How many books has Allah Almighty sent down?</item>
    <item>Who is Muhammad (saw)?</item>
    <item>Are Muslims terroists?</item>
    <item>Love in Islam?</item>
    <item>Rights of women in Islam?</item>
    <item>Ask yourself these questions?</item>
</string-array>

MyActivity.java

    package com.islamic.truth.isjesuspbuhgod;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MyActivity extends ActionBarActivity {

    ListView list;
    String[] memeTitles;
    String[] memeDescription;
    int[] images={R.drawable.peace, R.drawable.allah, R.drawable.islam, R.drawable.kuran, R.drawable.muhammad, R.drawable.companions, R.drawable.brainwash, R.drawable.terrorist64, R.drawable.heart64, R.drawable.menandwomen, R.drawable.guestion};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        Resources res=getResources();
        memeTitles=res.getStringArray(R.array.titles);
        memeDescription=res.getStringArray(R.array.description);

        list= (ListView) findViewById(R.id.listView);
        IslamAdapter adapter=new IslamAdapter(this, memeTitles, images, memeDescription);
        list.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

class IslamAdapter extends ArrayAdapter<String>
{
    Context context;
    int[] images;
    String[] titleArray;
    String[] descriptionArray;
    IslamAdapter(Context c,String[] titles, int imgs[], String[] desc)
    {
        super(c,R.layout.single_row,R.id.textView,titles);
        this.context=c;
        this.images=imgs;
        this.titleArray=titles;
        this.descriptionArray=desc;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        View row=convertView;
        if(row==null)
        {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_row, parent, false);
        }
        ImageView myImage= (ImageView) row.findViewById(R.id.imageView);
        TextView myTitle= (TextView) row.findViewById(R.id.textView);
        TextView myDescription= (TextView) row.findViewById(R.id.textView3);

        myImage.setImageResource(images[position]);
        myTitle.setText(titleArray[position]);
        myDescription.setText(descriptionArray[position]);

        return row;
    }
}

myActivity.java

package com.islamic.truth.isjesuspbuhgod;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
3
  • can you say what is your actual problem? Commented Aug 23, 2014 at 10:14
  • Have you tried ListView.setOnItemClickListener()? Commented Aug 23, 2014 at 10:15
  • hi im really new to android thats why i dont understand :( Commented Aug 23, 2014 at 11:25

3 Answers 3

2

I want to update the answer of MHP for the Alienatus

list.setOnItemClickListener(new onItemClickListener(){

   @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent intent=new Intent(); 
           intent.startActivity(YourCurrentActivty.contextofYourActivity,DesireActivity.class); 
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

you should set onItemClickListener for your list:

list.setOnItemClickListener(new onItemClickListener(){

   @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            //arg2 return positin of clicked item.  
            //you can do everything with that like  
            // String text = memeTitles.get(arg2); or  
            //you can put switch case depend on witch row selected go to it's activity
        }
});

4 Comments

check top i posted link to my project can you do this for me plz? :(
normally I should say no,but because I liked your String-Array text,ok.
mhp thank you very much but i dont know your email can you give me your email so i can email you?
you could find my email in my profile.but this is my email: [email protected]
0

Do this :

<?xml version="1.0" encoding="utf-8"?>
**<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >**
<ImageView
    android:src="@drawable/allah"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView3"
    android:layout_alignTop="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/textView"
    android:layout_below="@+id/textView3"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_alignRight="@+id/textView3"
    android:layout_alignEnd="@+id/textView3" />
</RelativeLayout>

Your getview Method :

@Override
    public View getView (int position, View convertView, ViewGroup parent) {
        View row=convertView;
        if(row==null)
        {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_row, parent, false);
        }
        ImageView myImage= (ImageView) row.findViewById(R.id.imageView);
        TextView myTitle= (TextView) row.findViewById(R.id.textView);
        TextView myDescription= (TextView) row.findViewById(R.id.textView3);
        **RelativeLayout rel_main = (RelativeLayout)row.findViewById(R.id.rel_main);**


        **rel_main.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
            {
                    startActivity(new Intent(YourActivity.this,AnotherActivity.class));
            }
         });**
        myImage.setImageResource(images[position]);
        myTitle.setText(titleArray[position]);
        myDescription.setText(descriptionArray[position]);

        return row;
    }

2 Comments

in which line should i paste this code? and i want each row has own activity i mean when i click on first row show me page2 when i click on seocnd row show me page 3
Yes, you can do by putting the condition in getview method as you get position in that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.