【发布时间】:2014-02-22 06:21:37
【问题描述】:
您好,我已经创建了一个 Android 应用程序,其中我使用 CustomAdapter 在 ListView 中添加了一些按钮和图像。
我的文件结构是:
layout
[1] album ->List View
[2] album_list -> Button
那么我如何创建按钮对象?
我的代码是:
专辑.xml
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0" >
</ListView>
</GridLayout>
专辑列表.xml
<RelativeLayout
android:id="@+id/RelativeLayout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/btn_hindi"
android:layout_alignBottom="@+id/ImageView12"
android:layout_alignLeft="@+id/ImageView12"
android:background="@drawable/normal"
android:text="ENG"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/tv_albumname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_go"
android:layout_alignTop="@+id/iv_album_image"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="Album Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_hindi"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginTop="7dp"
android:layout_marginRight="17dp"
android:layout_alignTop="@+id/ImageView12"
android:layout_alignLeft="@+id/btn_eng"
android:background="@drawable/normal"
android:text="HINDI"
android:textColor="@android:color/white" />
</RelativeLayout>
相册.java
Album_List_Custom_Adapter adapter =
new Album_List_Custom_Adapter(getApplicationContext(), albumList);
lv.setAdapter(adapter);
Album_List_Custom_Adapter.java
private Context context;
ArrayList<HashMap<String, String>> listAlbum;
ViewHolder vholder;
private OnClickListener listener;
public Album_List_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList)
{
this.context = context;
this.listAlbum=albumList;
}
@Override
public int getCount()
{
return listAlbum.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi=convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.home_list_model, null);
vholder = new ViewHolder();
vholder.hindi=(Button)vi.findViewById(R.id.btn_hindi);
vholder.eng=(Button)vi.findViewById(R.id.btn_eng);
vholder.tv_album_name = (TextView) vi.findViewById(R.id.tv_albumname);
vi.setTag(vholder);
}
else
{
vholder = (ViewHolder) (vi.getTag());
}
vholder.hindi.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
vholder.hindi.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.hindi.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.eng.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.hindi.setBackgroundResource(R.drawable.normal);
}
});
vholder.tv_album_name.setText(listAlbum.get(position).get("album"));
return vi;
}
static class ViewHolder
{
TextView tv_album_name;
Button eng, hindi;
}
【问题讨论】:
-
在适配器中创建按钮对象,您可以在其中膨胀列表项布局。
-
我还想在 Button 上设置 Click Listener。怎么样?
-
添加按钮 b=(Button) view.findViewById(R.id.button);