您需要使用自定义适配器而不是 ArrayAdapter。这样您就可以随心所欲地自定义视图。
例如你的情况
row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<!-- Name -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Marshmallow"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<!-- Comments-->
<TextView
android:id="@+id/comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="5dp"
android:text="Android 6.0"
android:textColor="@android:color/black" />
<!-- Date and Time-->
<TextView
android:id="@+id/dateandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/comments"
android:layout_marginTop="5dp"
android:text="11th August"
android:textColor="@android:color/black" />
</RelativeLayout>
DataModel.java
public class DataModel {
String name;
String comment,
String date;
public DataModel(String name, String type, String comment, String date) {
this.name=name;
this.comment=comment;
this.date=date;
}
public String getName() {
return name;
}
public String getComment() {
return comment;
}
public String getDate() {
return date;
}
}
最后你的自定义适配器应该如下所示
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtName;
TextView txtComment;
TextView txtDate;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
// your on click code
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);
viewHolder.txtComment = (TextView) convertView.findViewById(R.id.comments);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateandtime);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getName());
viewHolder.txtComment.setText(dataModel.getComment());
viewHolder.txtDate.setText(dataModel.getDate());
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
你必须像这样设置适配器。
ArrayList<DataModel> dataModels; // then add your data into it
CustomAdapter adapter= new CustomAdapter(dataModels,getApplicationContext());
listView.setAdapter(adapter);
查看example 以获得更好的想法。