我使用ArrayAdapter创建了自定义Listview。以下是我的代码
adapter = new ArrayAdapter<Item>(this,
android.R.layout.activity_list_item, android.R.id.text1,
fileList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// creates view
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);
// put the image on the text view
textView.setCompoundDrawablesWithIntrinsicBounds(
fileList[position].icon, 0, 0, 0);
// add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
textView.setCompoundDrawablePadding(dp5);
Button b1 = (Button)findViewById(android.R.id.button1);
return view;
}
};
在上面的代码按钮中没有显示。如何显示带有textview和图像的按钮。
由于
答案 0 :(得分:0)
这里的问题是你使用textview&#34;&#34; android.R.id.text1&#34;&#34; android中的设置并在Arraylist适配器中设置,因此您只能获取Textview,因为您在适配器的getview()方法中返回视图
for Button您想要创建Customview。
创建xmlfile cstom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
创建自定义适配器类
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class MyCustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<String> list;
public MyCustomAdapter(Context context, ArrayList<String> list) {
inflater=LayoutInflater.from(context);
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// creates view
View view = inflater.inflate(R.layout.cstom, null);
Button b1 = (Button) view.findViewById(android.R.id.button1);
TextView textView = (TextView) view.findViewById(android.R.id.textView1);
return view;
}
}
从您的活动中设置
MyCustomAdapter adapter=new MyCustomAdapter(context,list);
listview.setAdapter(adapter);
答案 1 :(得分:0)
将this
更改为get activity()
或getBaseContext
adapter = new ArrayAdapter<Item>(getActivity(),
android.R.layout.activity_list_item, android.R.id.text1,
fileList)
答案 2 :(得分:0)
您的代码已经是自定义适配器,尽管为此创建单独的类更好。无论如何,对于做得好的代码,缺少holder
。看看它是如何完成的:
adapter = new ArrayAdapter<Item>(this,
android.R.layout.activity_list_item, android.R.id.text1,
fileList) {
private ViewHolder mHolder;
private static class ViewHolder {
TextView textView;
Button b1;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
mHolder = new ViewHolder();
LayoutInflater _layoutInflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = _layoutInflater.inflate(R.layout.custom, null);
mHolder.textView = (TextView) view.findViewById(R.id.text1);
mHolder.b1 = (Button) view.findViewById(R.id.button1);
view.setTag(mHolder);
}
else{
mHolder = (ViewHolder) view.getTag();
}
// put the image on the text view
mHolder.textView.setCompoundDrawablesWithIntrinsicBounds(
fileList[position].icon, 0, 0, 0);
// add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
mHolder.textView.setCompoundDrawablePadding(dp5);
mHolder.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
};