Android Clickable Listview(每行都有另一个活动)

时间:2014-08-23 10:08:28

标签: android listview android-listview onclicklistener clickable

我有一个自定义列表视图,其中包含图像标题和说明。 我希望每一行显示自定义活动,例如看到这张照片 - > 我的照片存储在res的drawable文件夹中 链接到我的项目请Here

enter image description here

以下是我的项目ANDROID STUDIO的链接。

我每天都在尝试这种情况差不多2周但却找不到任何东西

    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 个答案:

答案 0 :(得分:2)

我想为Alienatus更新MHP的答案

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); 
    }
});

答案 1 :(得分:1)

你应该为你的列表设置onItemClickListener:

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
        }
});

答案 2 :(得分:0)

这样做:

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

您的getview方法:

@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;
    }