0

我是android新手,有点困惑。我有一个带有图像和文本的 listView。在哪里,如果我单击图像,它应该启动一个活动,如果我单击文本另一个活动。

鳕鱼:在

onCrete(){
listView = getListView();}

myBaseAdapterItemActivity = new MyBaseAdapterItemActivity(
                    ItemActivity.this, placeNameList);
            setListAdapter(myBaseAdapterItemActivity);

            myBaseAdapterItemActivity.notifyDataSetChanged();

            listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(new OnItemClickListener() {


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

// One Activity I can start without any problem

// In xml File I set for image clicable to true.

// What I want to do is like this

if(view.getId() == R.id.imageId)
{
Intent intent = new Intent(this, ImageActivity.class);
startActivity(intent);
}
else if(view.getID == R.id.textId)
{
Intent intent = new Intent(this, TextActivity.class);
startActivity(intent);
}
}}

每当我单击图像时,它都不会出现在 textView 中。任何想法

4

4 回答 4

0

它有两个解决方案: 1)代替为列表编写 onItemClickListener ,您可以在 getview 方法中的自定义适配器中的 textview 和 imageview 中执行 findviewbyid ,然后在它们上设置 onclick 侦听器。

2)您可以使用 getChildAt 方法....并检查哪个孩子是您的图像视图,哪个是您的文本视图。这是一个解决方法,所以不能保证。

于 2013-05-08T08:44:53.870 回答
0

试试下面的代码。你的适配器应该像下面的代码。然后您的文本和图像单击将创建新活动。

嗨,代码应该如下所示希望这对您有所帮助。

package com.example.listwithclick;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


    ListView listView1;
    Activity activity;

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

        listView1=(ListView)findViewById(R.id.listView1);
        listView1.setAdapter(new MyAddapter(MainActivity.this));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
     class MyAddapter extends BaseAdapter {
            Context rContext;
            private LayoutInflater rInflater;

            public MyAddapter(Context c) {

                rInflater = LayoutInflater.from(c);

                rContext = c;

            }      

            public MyAddapter(Activity imagebinding) {
                // TODO Auto-generated constructor stub

                activity = imagebinding;        

                rContext = imagebinding;
                rInflater = LayoutInflater.from(imagebinding);
                rContext = imagebinding;
                rInflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub  


                return 10;
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                // TODO Auto-generated method stub
                convertView = rInflater.inflate(R.layout.child, null);
                final MyDat mydat = new MyDat();                

                mydat.textview = (TextView) convertView.findViewById(R.id.textView1);
                mydat.textview.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Toast.makeText(rContext, "text", 1000).show();
                        Intent image= new Intent(rContext,TextActivity.class);
                        startActivity(image);
                    }
                });

                mydat.imageView1=(ImageView)convertView.findViewById(R.id.imageView1);
                mydat.imageView1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(rContext, "image ", 1000).show();

                        Intent image= new Intent(rContext,ImageActivity.class);
                        startActivity(image);
                    }
                });

                return convertView;
            }

            class MyDat {


                TextView  textview;
                ImageView imageView1;


            }

        }

}
于 2013-05-08T08:43:16.760 回答
-1

在列表项 xml 中,设置 android:onClick="onFirstLinkClick" 并类似地设置图像视图,并在您的活动中使用以下方法

public void onFirstLinkClick(View V) {

}

于 2013-05-08T08:37:55.687 回答
-1

在你的MyBaseAdapterItemActivitysetOnClickListener((OnCLickListener)mContext)到 ImageVIew 和 TextView。

在您的活动中,扩展OnClickListener。写你startActivity(Intent)OnClick(View v)取决于v.getId()

于 2013-05-08T08:34:02.937 回答