0

アイテムとして画像とテキストを含むリストがあります.onlistitemclickメソッドのコードを書きましたが、リストアイテムをクリックしても何も起こりません.........このファイルには、ドローアブルに画像リソースがあります- 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 に答える