我有一个项目ListView的2个视图。因此,一个用作ListView的HeaderView,另一个用作另一个。这是第二个标记从DetailsAdapter获取数据,它们分别是初始化字段。
public class DetailsAdapter extends ArrayAdapter<TicketObjects> {
private int resource;
private LayoutInflater inflater;
private Context context;
public DetailsAdapter ( Context ctx, int resourceId, List<TicketObjects> objects) {
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = (LinearLayout) inflater.inflate( resource, null );
TicketObjects ticketObjects = getItem( position );
TextView depTransferCity = (TextView) convertView.findViewById(R.id.tvTransferCity);
TextView detDepartTime = (TextView) convertView.findViewById(R.id.detDepartTime);
TextView detDepartDate = (TextView) convertView.findViewById(R.id.detDepartDate);
TextView detArriveTime = (TextView) convertView.findViewById(R.id.detArriveTime);
TextView detArriveDate = (TextView) convertView.findViewById(R.id.detArriveDate);
TextView depDepartCity = (TextView) convertView.findViewById(R.id.depDepartCity);
TextView detDepartAirport = (TextView) convertView.findViewById(R.id.detDepartAirport);
TextView detArriveCity = (TextView) convertView.findViewById(R.id.detArriveCity);
TextView detArriveAirport = (TextView) convertView.findViewById(R.id.detArriveAirport);
TextView detFlight = (TextView) convertView.findViewById(R.id.detFlight);
ImageView airlineLogo = (ImageView) convertView.findViewById(R.id.detAirportLogo);
depTransferCity.setText(ticketObjects.getTransferCity());
detDepartTime.setText(ticketObjects.getDepartTime());
detDepartDate.setText(ticketObjects.getDepartDate());
detArriveTime.setText(ticketObjects.getArriveTime());
detArriveDate.setText(ticketObjects.getArriveDate());
depDepartCity.setText(ticketObjects.getDepartCity());
detDepartAirport.setText(ticketObjects.getDepartAirport());
detArriveCity.setText(ticketObjects.getArriveCity());
detArriveAirport.setText(ticketObjects.getArriveAirport());
detFlight.setText(ticketObjects.getFlight());
airlineLogo.setImageResource(ticketObjects.getAirlineLogo());
return convertView;
}
}
MainActivity:
public class MainActivity extends Activity {
private ListView lvDetails;
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket_details);
ctx=this;
List<TicketObjects> ticketObjectses = new ArrayList<>();
ticketObjectses.add(new TicketObjects("Бишкек", "Манас", "Ош", "Аэропорт Оша", "FRU", "13:45", "16.09.2015", "OSS", "13:45", "16.09.2015", "Almaty", "1ч 15мин", "15000", R.drawable.logo_flyduba, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Ош", "Аэропорт Оша", "Новосибирск", "Толмачево", "OSS", "15:43", "16.09.2015", "OVB", "17:45", "16.09.2015", "Astana", "1ч 25мин", "16000", R.drawable.logo_kazak, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Алматы", "Алматы", "Москва", "Домодедово","ALA","11:54","16.09.2015","DME","12:44","16.09.2015","Novosibirsk", "2ч 15мин","13000", R.drawable.logo_pegasus_logo, "Рейс: 543"));
lvDetails = ( ListView ) findViewById( R.id.lvDetails);
ViewGroup header = (ViewGroup) getLayoutInflater().inflate(R.layout.ticket_details_header_item,lvDetails,false);
lvDetails.addHeaderView(header);
lvDetails.setAdapter(new DetailsAdapter(ctx, R.layout.ticket_details_item, ticketObjectses));
}
}
ticket_details_item_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/leftPadding"
android:layout_below="@+id/line"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout"
android:background="@drawable/layouts_border">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/iconsSize"
android:layout_gravity="center_horizontal"
android:padding="@dimen/leftPadding"
android:background="#e8e8e8"
android:layout_marginBottom="@dimen/rightPadding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="@+id/detTitleArriveCity"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Нью-Йорк"
android:id="@+id/detTitleArriveCity"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView" />
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/icon_depart"
android:padding="@dimen/rightPadding"
android:layout_marginLeft="@dimen/rightPadding"
android:layout_marginRight="@dimen/rightPadding" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="35ч 00м"
android:id="@+id/detTotalDuration"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainSmallSize"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/leftPadding"
android:gravity="center_vertical"
android:paddingLeft="@dimen/text_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="3"
android:gravity="center">
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView2"
android:src="@drawable/from"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView3"
android:src="@drawable/punktir"
android:layout_below="@+id/imageView2"
android:layout_marginTop="-5dp" />
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView4"
android:src="@drawable/to"
android:layout_below="@+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09:50"
android:id="@+id/detDepartTime"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.06.2016"
android:id="@+id/detDepartDate"
android:layout_below="@+id/detDepartTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/rightPadding"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11:40"
android:id="@+id/detArriveTime"
android:layout_below="@+id/detDepartDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13.06.2016"
android:id="@+id/detArriveDate"
android:layout_below="@+id/detArriveTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="2"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="@+id/depDepartCity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Манас"
android:id="@+id/detDepartAirport"
android:layout_below="@+id/depDepartCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/rightPadding"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Москва"
android:id="@+id/detArriveCity"
android:layout_below="@+id/detDepartAirport"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Шереметьево"
android:id="@+id/detArriveAirport"
android:layout_below="@+id/detArriveCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/stndHeight"
android:layout_gravity="center_horizontal"
android:paddingTop="@dimen/leftPadding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Рейс: AN565"
android:id="@+id/detFlight"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/detAirportLogo"
android:src="@drawable/logo_flyduba"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
ticket_details_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/leftPadding"
android:background="#e8e8e8"
android:gravity="center_vertical">
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/locIcon"
android:src="@drawable/location"
android:layout_weight="1"
android:padding="@dimen/leftPadding" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Пересадка:"
android:id="@+id/textviewTransfer"
android:layout_weight="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTransferCity"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize"
android:layout_weight="3" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/leftPadding"
android:gravity="center_vertical"
android:paddingLeft="@dimen/text_margin"
android:id="@+id/centerlayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="3"
android:gravity="center">
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView2"
android:src="@drawable/from"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView3"
android:src="@drawable/punktir"
android:layout_below="@+id/imageView2"
android:layout_marginTop="-5dp" />
<ImageView
android:layout_width="@dimen/smallIconSize"
android:layout_height="@dimen/smallIconSize"
android:id="@+id/imageView4"
android:src="@drawable/to"
android:layout_below="@+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="-5dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09:50"
android:id="@+id/detDepartTime"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.06.2016"
android:id="@+id/detDepartDate"
android:layout_below="@+id/detDepartTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/rightPadding"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11:40"
android:id="@+id/detArriveTime"
android:layout_below="@+id/detDepartDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13.06.2016"
android:id="@+id/detArriveDate"
android:layout_below="@+id/detArriveTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/rightPadding"
android:layout_weight="2"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бишкек"
android:id="@+id/depDepartCity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Манас"
android:id="@+id/detDepartAirport"
android:layout_below="@+id/depDepartCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/rightPadding"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Москва"
android:id="@+id/detArriveCity"
android:layout_below="@+id/detDepartAirport"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Шереметьево"
android:id="@+id/detArriveAirport"
android:layout_below="@+id/detArriveCity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="@color/mainGreyColor"
android:textSize="@dimen/mainMiddleSize" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/stndHeight"
android:layout_gravity="center_horizontal"
android:paddingTop="@dimen/leftPadding">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Рейс: AN565"
android:id="@+id/detFlight"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:textColor="@color/otherTextColor"
android:textSize="@dimen/mainLargeSize" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/detAirportLogo"
android:src="@drawable/logo_flyduba"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
现在,ticket_details_header_item.xml没有显示,也就是说,没有数据。第二个视图正如您在适配器中看到的那样,初始化并且它有数据。
DetailObjects
package kz.ticketdetail;
import java.text.DecimalFormat;
public class TicketObjects {
private String departCity;
private String departAirport;
private String arriveCity;
private String arriveAirport;
private String departCode;
private String departTime;
private String departDate;
private String arriveCode;
private String arriveTime;
private String arriveDate;
private String transferCity;
private String flyDuration;
private String ticketPrice;
private int airlineLogo;
private String flight;
public TicketObjects(String departCity, String departAirport, String arriveCity, String arriveAirport,
String departCode, String departTime, String departDate,
String arriveCode, String arriveTime, String arriveDate,
String transferCity, String flyDuration, String ticketPrice, int airlineLogo, String flight) {
this.departCity = departCity;
this.departAirport = departAirport;
this.arriveCity = arriveCity;
this.arriveAirport = arriveAirport;
this.departCode = departCode;
this.departTime = departTime;
this.departDate = departDate;
this.arriveCode = arriveCode;
this.arriveTime = arriveTime;
this.arriveDate = arriveDate;
this.transferCity = transferCity;
this.flyDuration = flyDuration.trim().replaceFirst("^[0]{1}", "").replace(":", " h ") + " m";
this.ticketPrice = ticketPrice;
this.airlineLogo = airlineLogo;
this.flight = flight;
}
public static String getFormattingPrice(final String ticketPrice) throws IllegalArgumentException {
DecimalFormat formatter = new DecimalFormat("#,###,###");
return formatter.format(Float.parseFloat(ticketPrice)).replace(",", " ");
}
public String getDepartCity(){
return departCity;
}
public void setDepartCity(String departCity){
this.departCity = departCity;
}
public String getDepartAirport(){
return departAirport;
}
public void setDepartAirport(String departAirport){
this.departAirport = departAirport;
}
public String getArriveCity(){
return arriveCity;
}
public void setArriveCity(String arriveCity){
this.arriveCity = arriveCity;
}
public String getArriveAirport(){
return arriveAirport;
}
public void setArriveAirport(String arriveAirport){
this.arriveAirport = arriveAirport;
}
public String getFlight(){
return flight;
}
public void setFlight(String flight){
this.flight = flight;
}
public String getDepartCode(){
return departCode;
}
public void setDepartCode(String departCode){
this.departCode = departCode;
}
public String getDepartTime(){
return departTime;
}
public void setDepartTime(String departTime){
this.departTime = departTime;
}
public String getDepartDate(){
return departDate;
}
public void setDepartDate(String departDate){
this.departDate = departDate;
}
public String getArriveCode(){
return arriveCode;
}
public void setArriveCode(String arriveCode){
this.arriveCode = arriveCode;
}
public String getArriveTime(){
return arriveTime;
}
public void setArriveTime(String arriveTime){
this.arriveTime = arriveTime;
}
public String getArriveDate(){
return arriveDate;
}
public void setArriveDate(String arriveDate){
this.arriveDate = arriveDate;
}
public String getTransferCity(){
return transferCity;
}
public void setTransferCity(String transferCity){
this.transferCity = transferCity;
}
public String getFlyDuration(){
return flyDuration;
}
public void setFlyDuration(String flyDuration){
this.flyDuration = flyDuration;
}
public String getTicketPrice(){
return ticketPrice;
}
public void setTicketPrice(String ticketPrice){
this.ticketPrice = ticketPrice;
}
public int getAirlineLogo(){
return airlineLogo;
}
public void setAirlineLogo(int airlineLogo){
this.airlineLogo = airlineLogo;
}
}
问题:标题视图如何显示MainActivity数组中的数据?应该在哪里进行初始化ticket_details_header_item?
答案 0 :(得分:0)
你需要从标题中获取视图并设置这样的数据。
public class MainActivity extends Activity {
private ListView lvDetails;
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket_details);
ctx=this;
List<TicketObjects> ticketObjectses = new ArrayList<>();
ticketObjectses.add(new TicketObjects("Бишкек", "Манас", "Ош", "Аэропорт Оша", "FRU", "13:45", "16.09.2015", "OSS", "13:45", "16.09.2015", "Almaty", "1ч 15мин", "15000", R.drawable.logo_flyduba, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Ош", "Аэропорт Оша", "Новосибирск", "Толмачево", "OSS", "15:43", "16.09.2015", "OVB", "17:45", "16.09.2015", "Astana", "1ч 25мин", "16000", R.drawable.logo_kazak, "Рейс: 543"));
ticketObjectses.add(new TicketObjects("Алматы", "Алматы", "Москва", "Домодедово","ALA","11:54","16.09.2015","DME","12:44","16.09.2015","Novosibirsk", "2ч 15мин","13000", R.drawable.logo_pegasus_logo, "Рейс: 543"));
lvDetails = ( ListView ) findViewById( R.id.lvDetails);
View header = getLayoutInflater().inflate(R.layout.ticket_details_header_item,lvDetails,false);
TextView text1 = (TextView) header.findViewById(R.id.detTitleArriveCity);
TextView text2 = (TextView) header.findViewById(R.id.detTotalDuration);
// do the same far all your text views or what ever you want to get from layout.
TicketObjects mTicketObjects = ticketObjectses.get(0);
text1.setText(mTicketObjects.getDepartCity());
text2.setText(mTicketObjects.getFlyDuration());
// do the same for other options.
lvDetails.addHeaderView(header);
lvDetails.setAdapter(new DetailsAdapter(ctx, R.layout.ticket_details_item, ticketObjectses));
}
}
为所有项目执行此操作。在活动内部不在适配器中。希望能回答你的问题。