0

我有一个带有图像和文本作为其项目的列表。我已经为 onlistitemclick 方法编写了代码,但是当我单击列表项时没有任何反应............在这个文件中,我在 drawable 中有图像资源- hdpi 文件夹和 xml 文件中的项目文本继承了代码:

//main.java
package abhi.tv;<br/>
import android.app.ListActivity;<br/>
import android.content.Context;<br/>
import android.os.Bundle;<br/>
import android.view.LayoutInflater;<br/>
import android.view.View;<br/>
import android.view.View.OnClickListener;<br/>
import android.view.ViewGroup;<br/>
import android.widget.AdapterView;<br/>
import android.widget.AdapterView.OnItemClickListener;<br/>
import android.widget.ArrayAdapter;<br/>
import android.widget.ImageView;<br/>
import android.widget.ListView;<br/>
import android.widget.TextView;<br/>
import android.widget.Toast;<br/>
public class IndiatvActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       MyAdapter ad=new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.channel_name,getResources().getStringArray(R.array.channels));

       setListAdapter(ad);




    }







        private class MyAdapter extends ArrayAdapter<String> implements OnItemClickListener{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
        }


        public View getView(int position,View convertView,ViewGroup parent){
            LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.list_item,parent,false);
            String chann[]=getResources().getStringArray(R.array.channels);
            ImageView iv=(ImageView) row.findViewById(R.id.logo);
            TextView tv=(TextView) row.findViewById(R.id.channel_name);
            tv.setText(chann[position]);
            if(chann[position].equalsIgnoreCase("star plus"))
                iv.setImageResource(R.drawable.star_plus);
            else if(chann[position].equalsIgnoreCase("sony tv"))
                iv.setImageResource(R.drawable.sony_tv);
            else if(chann[position].equalsIgnoreCase("sahara one"))
                iv.setImageResource(R.drawable.sahara_one);
            else if(chann[position].equalsIgnoreCase("zee tv"))
                iv.setImageResource(R.drawable.zee_tv);
            else if(chann[position].equalsIgnoreCase("colors"))
                iv.setImageResource(R.drawable.colors_tv);
            else if(chann[position].equalsIgnoreCase("mtv"))
                iv.setImageResource(R.drawable.mtv);
            else if(chann[position].equalsIgnoreCase("9x india"))
                iv.setImageResource(R.drawable.ninex);
            else if(chann[position].equalsIgnoreCase("star one"))
                iv.setImageResource(R.drawable.star_one);
            else if(chann[position].equalsIgnoreCase("sab tv"))
                iv.setImageResource(R.drawable.sab_tv);
            else if(chann[position].equalsIgnoreCase("ndtv imagine"))
                iv.setImageResource(R.drawable.ndtv_imagine);
            return row;
        }
        @Override
        public void onItemClick(AdapterView<?> l, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            //super.onItemClick(l, v, position, id);
            String chann[]=getResources().getStringArray(R.array.channels);
            Toast.makeText(IndiatvActivity.this,"hiiii xt", Toast.LENGTH_LONG);
            Toast.makeText(IndiatvActivity.this,"this message is of"+chann[position],Toast.LENGTH_SHORT).show();
            android.util.Log.d("abhi"," msg:item was clicked");

        }


        }

    }**





<br/>heres the xml code:<br/>
//list_item.xml<br/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="true"
    android:orientation="horizontal" android1:addStatesFromChildren="false">




    <ImageView
        android1:id="@+id/logo"
        android1:layout_width="100dp"
        android1:layout_height="100dp"
        android1:layout_alignParentLeft="true"
        android1:layout_alignParentTop="true"
        android1:adjustViewBounds="true"
        android1:clickable="true"
        android1:maxHeight="500dp"
        android1:maxWidth="500dp"
        android1:minHeight="500dp"
        android1:minWidth="500dp"
        android1:src="@drawable/colors_tv" />


    <TextView
        android1:id="@+id/channel_name"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android1:layout_alignBottom="@+id/logo"
        android1:layout_marginLeft="15dp"
        android1:layout_toRightOf="@+id/logo"
        android1:text="Large Text"
        android1:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>




<br/>
<br/>
<br/>
//main.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="vertical" >

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

</LinearLayout>
4

2 回答 2

2

这不会起作用,因为您使用了一个简单的列表适配器,要在列表视图中一起添加图像和文本,您必须定义自己的自定义适配器并使用它。

检查此链接以编写您自己的自定义适配器

于 2012-04-22T10:54:59.613 回答
0
setOnItemClickListener() 

在不在适配器中的主要活动中,它将返回您特定的行

然后在您点击项目后......

于 2012-04-22T10:49:07.310 回答