0
我開發一個自定義列表視圖Android應用程序(用單個圖像和兩個文本視圖)的onclick監聽器自定義列表視圖
類mainactivity有....
public class MainActivity extends ActionBarActivity {
String[] memetitles;
String[] memedesc;
ListView list;
int[] images = {R.drawable.angry_icon,R.drawable.happy_icon,R.drawable.kid_icon,R.drawable.scare_icon,R.drawable.warn_icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
memetitles = res.getStringArray(R.array.titles);
memedesc = res.getStringArray(R.array.notneed);
list = (ListView) findViewById(R.id.listView1);
Itarray adapter = new Itarray(this,memetitles,memedesc,images);
list.setAdapter(adapter);
setTitle("Tamil Memer");
}
類對於兩個佈局組合成單個列表視圖具有
class Itarray extends ArrayAdapter<String>{
Context context;
int[] image;
String[] tit1;
String[] tit2;
Itarray(Context c , String[] titles ,String[] notneed, int imgs[])
{
super(c , R.layout.single_row,titles);
this.context = c;
this.image = imgs;
this.tit1 = titles;
this.tit2 = notneed;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if(row == null)
{
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inf.inflate(R.layout.single_row , parent , false);
}
ImageView myImage = (ImageView) row.findViewById(R.id.imageView1);
TextView tit = (TextView) row.findViewById(R.id.textView1);
TextView des = (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(image[position]);
tit.setText(tit1[position]);
des.setText(tit2[position]);
return row;
}
}
釷Ë從兩種佈局文件: (具有圖像和textviews)
<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:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.itassistors.tmg.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:text="@string/simple"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/textView1"
android:layout_alignStart="@+id/textView1"
android:text="@string/simple"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView1"
android:src="@drawable/happy_icon" />
<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:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.itassistors.tmg.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
我想知道如何(在一個使用ListView)以及將onClickListener()放置在此自定義列表視圖的位置 在此先感謝
先生您好,我試過這種方法,但讓我吃驚的是顯示了使用row.setOnClickListener(新OnClickListener() – 2014-12-02 07:35:41
什麼樣的錯誤的錯誤? – Rami 2014-12-02 08:13:56
當我在適配器類中使用它時,它會拋出類似下面的錯誤...類型View中的方法setOnClickListener(View.OnClickListener)不適用於參數(新的DialogInterface.OnClickListener(){}) – 2014-12-02 10:15:01