我有一个带有图像的列表和一个文本作为其item.i已经为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>
答案 0 :(得分:2)
这不会起作用,因为您使用了一个简单的列表适配器,在列表视图中一起添加图像和文本,您必须定义自己的自定义适配器并使用它。
选中此link以编写您自己的自定义适配器
答案 1 :(得分:0)
setOnItemClickListener()
在主要活动中不在适配器中 它将返回特定的行
然后你会点击项目.....