随笔 - 0, 文章 - 264, 评论 - 170, 引用 - 0
数据加载中……

自定义ListView

lite_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
 
  <TextView
    android:text="@+id/TextView01"
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
  <ImageButton
    android:id="@+id/ImageButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>


MySource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class MySource extends Activity 
{
  private ArrayList<String> dt=new ArrayList<String>();
  
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

for(int i=0;i<10;i++)
{
  dt.add("A"+i);
}

MyAdapter adapter=new MyAdapter(MySource.this, dt);

ListView lv=(ListView)findViewById(R.id.ListView01);
lv.setAdapter(adapter);
}
 
}
 
class MyAdapter extends BaseAdapter
{
  private Context context;
  private LayoutInflater m_inflater;
  private ArrayList<String> dt;
  
  public MyAdapter(Context context, ArrayList<String> dt)
  {
    m_inflater = LayoutInflater.from(context);
    this.context=context;
    this.dt=dt;
  }
  
  @Override
  public int getCount()
  {
    return dt.size();
  }
 
  @Override
  public Object getItem(int arg0)
  {
    return dt.get(arg0);
  }
 
  @Override
  public long getItemId(int position)
  {
    return position;
  }
 
  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    if(convertView==null)
    {
      convertView=m_inflater.inflate(R.layout.lite_item, null);
    }
    
    final TextView tv=(TextView)convertView.findViewById(R.id.TextView01);
    ImageButton ib=(ImageButton)convertView.findViewById(R.id.ImageButton01);
    
    tv.setText(dt.get(position));
    ib.setBackgroundDrawable(convertView.getResources().getDrawable(android.R.drawable.btn_dialog));
    
    ib.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        tv.setText("Click!!!");
      }
    });
    
    return convertView;
  }
}

posted on 2011-01-03 17:53 小一败涂地 阅读(1579) 评论(0)  编辑  收藏 所属分类: android+移动开发


只有注册用户登录后才能发表评论。


网站导航: