码迷,mamicode.com
首页 > 其他好文 > 详细

baseAdapter

时间:2016-03-26 14:08:37      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

MainActivity.java主要代码:

public class MainActivity extends ActionBarActivity {
public ListView lv;
public int[] imageIDS={R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,
R.drawable.h,R.drawable.j};
public String[] name={"a","b","c","d","e","f","g","h","j"};
public int num[]=new int[10];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++)
num[i]=i+1;
// Log.i("imageids",String.valueOf(imageIDS.length));
lv=(ListView) findViewById(R.id.list);


//myBaseAdapter adapter =new myBaseAdapter(MainActivity.this);
Myadapter adapter=new Myadapter(MainActivity.this, imageIDS, name, num);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("你最喜欢这个人物吗?")
.setIcon(imageIDS[arg2])
.setMessage("就是它!!")
.setPositiveButton("确定", null)
.setNegativeButton("重选", null)
.show();
}
});
}

//该类为基本用法,未经优化,上述代码未使用该类
class myBaseAdapter extends BaseAdapter{
private Context context;

public myBaseAdapter(Context context) {
this.context=context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return imageIDS.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater flater= LayoutInflater.from(context);
View view=flater.inflate(R.layout.base, null);
ImageView ivg=(ImageView) view.findViewById(R.id.iv);
ivg.setImageResource(imageIDS[position]);
TextView tv1=(TextView) view.findViewById(R.id.t1);
tv1.setText(String.valueOf(num[position]));
TextView tv2=(TextView) view.findViewById(R.id.t2);
tv2.setText(name[position]+" hi");
return view;
}

}
}


activity_main.xml主要代码:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动漫人物,你最喜欢哪个?" />
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>

base.xml主要代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Myadapter.java主要代码:
public class Myadapter extends BaseAdapter {

private Context context;
private String[] names;
int[] imagesIDS,num;
public Myadapter(Context context,int[] imagesIDS,String[]names,int[] num) {
this.context=context;
this.imagesIDS=imagesIDS;
this.names=names;
this.num=num;
}

static class ViewHold{
public ImageView iv;
public TextView t1;
public TextView t2;
};

@Override
public int getCount() {
// TODO Auto-generated method stub
return imagesIDS.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater flater= LayoutInflater.from(context);
ViewHold viewhold = null;
if(convertView==null){
convertView=flater.inflate(R.layout.base, null);
viewhold=new ViewHold();
viewhold.iv=(ImageView) convertView.findViewById(R.id.iv);
viewhold.t1=(TextView) convertView.findViewById(R.id.t1);
viewhold.t2=(TextView) convertView.findViewById(R.id.t2);
convertView.setTag(viewhold);
}
else{
viewhold=(ViewHold) convertView.getTag();
}
viewhold.iv.setImageResource(imagesIDS[position]);
viewhold.t1.setText(names[position]);
viewhold.t2.setText(String.valueOf(num[position]));
return convertView;
}

}

 

baseAdapter

标签:

原文地址:http://www.cnblogs.com/linduan/p/5322609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!