具有多个TextView的CustomListview不显示活动中的任何内容

时间:2016-06-10 13:33:36

标签: android listview android-arrayadapter custom-lists

我希望制作自定义列表视图,其中包含多个textview

没有错误,但是当我运行此代码时,活动中将没有任何内容

这是我的活动列表

1。 hospital_list_view_layout.java (自定义适配器)

public class hospital_list_view_layout extends ArrayAdapter<String>
{
String[] hospital_name, address, phone, fax, state, city, pincode;
private final Activity context;

        public hospital_list_view_layout(Activity context,
                          String[] hospital_name,String[] address,String[] phone,String[] fax,String[] state,String[] city,String[] pincode ) {
            super(context, R.layout.hospital_list_view_layout);
            this.context=context;
            this.hospital_name=hospital_name;
            this.address=address;
            this.phone=phone;
            this.fax=fax;
            this.state=state;
            this.city=city;
            this.pincode=pincode;



}


public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.hospital_list_view_layout, null,true);

    TextView hospitalname = (TextView) rowView.findViewById(R.id.custom_hospital_name);
    TextView hospitaladd = (TextView) rowView.findViewById(R.id.custom_hospital_add);
    TextView hospitalno = (TextView) rowView.findViewById(R.id.custom_hospital_no);
    TextView hospitalfax = (TextView) rowView.findViewById(R.id.custom_hospital_fax);
    TextView hospitalstate = (TextView) rowView.findViewById(R.id.custom_hospital_state);
    TextView hospitalcity = (TextView) rowView.findViewById(R.id.custom_hospital_city);

    hospitalname.setText(hospital_name[position]);
    hospitaladd.setText(address[position]);
    hospitalno.setText(phone[position]);
    hospitalname.setText(fax[position]);
    hospitalname.setText(city[position]);
    hospitalname.setText(state[position]);
    hospitalname.setText(pincode[position]);
    return rowView;

};


  }

2. hospital_list_view.java (运行的活动)

 public class hospital_list_view extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hospital_list_view);
    String[] hospital_name, address, phone, fax, state, city, pincode;

    hospital_name = new String[]{"sterling","walkheart","suyona","sadbhavana"};
    address = new String[]{"gandhigram","kalawas","maruti","hudko"};
    phone = new String[]{"123","222","333","444"};
    fax = new String[]{"0101,0202,0303,0404"};
    state = new String[]{"gujarat","gujarat","gujarat","gujarat"};
    city = new String[]{"sterling","walkheart","suyona","sadbhavana"};
    pincode = new String[]{"sterling","walkheart","suyona","sadbhavana"};
   // Toast.makeText(this,getIntent().getExtras().getString("city")+getIntent().getExtras().getString("state"),Toast.LENGTH_LONG).show();
    ListView l1 = (ListView) findViewById(R.id.listview_hospital);

    hospital_list_view_layout h = new hospital_list_view_layout(this,hospital_name,address,phone,fax,state,city,pincode);
    l1.setAdapter(h);



}
}

以下是XML文件列表

  1. hospital_list_view.xml (运行活动xml)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
     <ListView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/listview_hospital">
    
     </ListView>
    
     </LinearLayout>
    
  2. 4. hospital_list_view_layout (自定义列表视图xml)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="Name :"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_name" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="Address :"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_add" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="Phone no :"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_no" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="Fax no"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_fax" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="State"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_state" />
    
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="City :"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_city" />
    
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:text="Pincode :"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27dp"
            android:layout_marginLeft="20dp"
            android:text="hospital Name"
            android:id="@+id/custom_hospital_pincode" />
    
    </LinearLayout>
    
    </LinearLayout>
    

1 个答案:

答案 0 :(得分:0)

所有代码都很好,只有一个错误 将null替换为parent,最后一个参数为true ..

View rowView=inflater.inflate(R.layout.hospital_list_view_layout, null,true);

View rowView=inflater.inflate(R.layout.hospital_list_view_layout, parent,false);

这是我的代码,我希望它会对你有所帮助:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row_spinner_with_checkbox, parent, false);
        holder.txtAbility = (TextView) convertView.findViewById(R.id.rowSpinnerTxtItem);
        holder.txtSubjectList = (TextView) convertView.findViewById(R.id.rowSpinnerTxtSubItem);
        holder.checkboxAbility = (CustomCheckBox) convertView.findViewById(R.id.rowSpinnerCheckItem);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }



    holder.checkboxAbility.setTag(position);
    AbilityModel dataAtPosition = dataAbility.get(position);
    holder.txtAbility.setText(dataAtPosition.getName());


    // show related subject list
    holder.txtSubjectList.setVisibility(View.VISIBLE);
    StringBuilder strSubject = null;

    for (AbilitySubjectModel subEntity : dataAtPosition.getAbiSubs()) {
        if(strSubject==null)
        {
            strSubject= new StringBuilder();
            strSubject.append(subEntity.getSubjectName());
        }else {
            strSubject.append(", ").append(subEntity.getSubjectName());
        }
    }

    holder.txtSubjectList.setText(strSubject);

    if (dataAtPosition.isChecked()) {
        holder.checkboxAbility.setChecked(true, false);
    } else {
        holder.checkboxAbility.setChecked(false, false);
    }

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewHolder viewHolder = (ViewHolder) v.getTag();
            int pos = (int) viewHolder.checkboxAbility.getTag();
            viewHolder.checkboxAbility.setChecked(!dataAbility.get(pos).isChecked());
        }
    });

    holder.checkboxAbility.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int pos = (int) buttonView.getTag();
            dataAbility.get(pos).setChecked(isChecked);

        }
    });
    return convertView;
}