隨筆 - 0, 文章 - 264, 評論 - 170, 引用 - 0
          數(shù)據(jù)加載中……

          自定義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 小一敗涂地 閱讀(1595) 評論(0)  編輯  收藏 所屬分類: android+移動開發(fā)

          主站蜘蛛池模板: 桂阳县| 鄱阳县| 株洲市| 北流市| 昌吉市| 利川市| 莱阳市| 锡林郭勒盟| 五家渠市| 安泽县| 吕梁市| 桓台县| 浙江省| 弥渡县| 惠州市| 安图县| 鸡西市| 两当县| 上饶县| 乌拉特中旗| 衡阳市| 安康市| 德州市| 灵台县| 天祝| 望谟县| 正镶白旗| 永昌县| 利川市| 长宁县| 天祝| 博客| 二手房| 五台县| 鸡西市| 财经| 乌兰浩特市| 临沧市| 姚安县| 余江县| 高青县|