我有类似的屏幕,所以你可以使用下面的一些代码。
public class ListViewCustomAdapter extends BaseAdapter {
/**
* Array list to hold assets which need to be displayed
*/
ArrayList<Asset> itemList;
/**
* Context: Interface to global information about an application
* environment.
*/
public Activity context;
/**
* Layout inflater to inflate view of each row of the list from the layout
* xml
*/
public LayoutInflater inflater;
/**
* @param context
* : Context: Interface to global information about an
* application environment.
* @param itemList
* : arraylist to hold asset which need to be displayed in list
*/
public ListViewCustomAdapter(Activity context, ArrayList<Asset> itemList) {
super();
this.context = context;
this.itemList = itemList;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// @Override
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
public int getCount() {
if (itemList != null) {
return itemList.size();
}else return 0;
}
// @Override
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
public Object getItem(int position) {
if(itemList != null){
return itemList.get(position);
}else{
return 0;
}
}
// @Override
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int position) {
return 0;
}
/**
* @author asadafale class to create view of each row in the list
*/
public static class ViewHolder {
/**
* displays name of asset
*/
TextView txtVw_name;
/**
* displays serial no of asset
*/
TextView txtVw_sno;
/**
* displays type of asset
*/
TextView txtVw_type;
}
// @Override
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_row, null);
holder.txtVw_name = (TextView) convertView
.findViewById(R.id.tv_name);
holder.txtVw_sno = (TextView) convertView.findViewById(R.id.tv_sno);
holder.txtVw_type = (TextView) convertView
.findViewById(R.id.tv_type);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// set selected item
LinearLayout activeItem = (LinearLayout) convertView;
if (position == SearchAssetActivity.selectedItem) {
activeItem.setBackgroundColor(Color.GRAY);
// for focus on it
int top = (activeItem == null) ? 0 : activeItem.getTop();
((ListView) parent).setSelectionFromTop(position, top);
} else {
activeItem.setBackgroundColor(Color.BLACK);
}
Asset bean = (Asset) itemList.get(position);
holder.txtVw_name.setText(bean.getAssetName());
holder.txtVw_sno.setText(bean.getSno());
holder.txtVw_type.setText(bean.getAssetType());
return convertView;
}
}
![在此处输入图像描述](https://i.stack.imgur.com/oDdD1.png)
我的屏幕的一部分看起来像你想要的一样。
这是我在 xml 中的布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey05"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.25"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_sno"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Serial No."
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_type"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:layout_marginLeft="20dp"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
</LinearLayout>
我知道我只想显示 3 列。您可以在 layout.xml 中定义许多 textview。在您的情况下,我相信您会即时知道列数,因此您将需要某种TextView
s 数组来动态执行此操作,而这无法通过 xml 完成。您必须TextView
在适配器类中定义一个 s 数组,并且可以根据来自数据库的数据动态初始化。希望你能得到我想要传达的东西。让我知道您是否需要更多帮助来设计它,一旦您开始。:)