listview没有显示任何内容

时间:2015-01-26 11:27:35

标签: android xml listview

我无法弄清楚我的listview突然消失的问题。我搜索了解决方案并尝试了xml文件中的所有可能的更改,但我仍然得到空屏幕。 列表视图的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="wrap_content"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

列表视图项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

  <TextView
    android:id="@+id/dcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="drcode"
    android:textColor="#000000" />

<TextView
    android:id="@+id/dname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="18dp"
    android:text="docname"
    android:textColor="#000000" />

<TextView
    android:id="@+id/a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="43dp"
    android:text="adr"
    android:textColor="#000000" />

<TextView
    android:id="@+id/cd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="38dp"
    android:layout_toRightOf="@+id/adr"
    android:text="class_desc"
    android:textColor="#000000" />

 <TextView
    android:id="@+id/tvshift"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="25dp"
    android:text="shift"
    android:textColor="#000000" />

<TextView
    android:id="@+id/tcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="24dp"
    android:layout_toRightOf="@+id/docname"
    android:text="terrcode"
    android:textColor="#000000" />

listview适配器的代码

public class planningListViewAdapter extends BaseAdapter {

  Context context;
  public ArrayList<Planning> planArraylist;
  private static LayoutInflater inflater = null;
  public Activity attdSetupActivity;

  public planningListViewAdapter(Context context,ArrayList<Planning> planArraylist) {
          this.context = context;
          this.planArraylist = planArraylist;
          inflater = (LayoutInflater) context
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }


  @Override
  public int getCount() {
          return planArraylist.size();
  }

  @Override
  public Object getItem(int arg0) {
          return null;
  }

  @Override
  public long getItemId(int arg0) {
          return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
                  convertView = inflater.inflate(R.layout.single_lv_item, null);
          }

          Planning p = planArraylist.get(position);

         TextView tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
          TextView tvDrName = (TextView) convertView.findViewById(R.id.dname);
          TextView tvTerr = (TextView) convertView.findViewById(R.id.tcode);
          TextView tvAdr = (TextView) convertView.findViewById(R.id.a);
          TextView tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
        //TextView tvSpeciality= (TextView) convertView.findViewById(R.id.speciaility);
          TextView tvShift = (TextView) convertView.findViewById(R.id.tvshift); 



          tvdrCode.setText(p.getDrCode());
          tvDrName.setText(p.getDocName());
          tvTerr.setText(p.getTerrCode());
          tvAdr.setText(p.getAdr());
          tvClassDesc.setText(p.getClassDesc());
          //tvSpeciality.setText(p.getSpeciality());
         tvShift.setText(p.getShift());

          return convertView;
  }

} 活动代码

public class PlanningList_activity extends Activity{


 private Db_sqlite dbHelper;
 private planningListViewAdapter dataAdapter;
 private ListView listView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.planning_listview);

      /*   TextView columnHeader1 = (TextView) findViewById(R.id.column_header1);
         TextView columnHeader2 = (TextView) findViewById(R.id.column_header2);
         TextView columnHeader3 = (TextView) findViewById(R.id.column_header3);
         TextView columnHeader4 = (TextView) findViewById(R.id.column_header4);
         TextView columnHeader5 = (TextView) findViewById(R.id.column_header5);
         TextView columnHeader6 = (TextView) findViewById(R.id.column_header6);


         columnHeader1.setText("Plan No");
         columnHeader2.setText("Plan Date");
         columnHeader2.setText("Mon");
         columnHeader2.setText("Dr Code");
         columnHeader2.setText("Morn_Even");
         columnHeader2.setText("FF_Code"); */

         Log.e("on create", "yes");

         listView = (ListView) findViewById(R.id.listView1);
         dbHelper = new Db_sqlite(PlanningList_activity.this);
         new AsyncLoadplanList().execute();
 }

 public class AsyncLoadplanList extends
                 AsyncTask<Void, Void, ArrayList<Planning>> {

         ProgressDialog progressDialog;

         @Override
         protected void onPreExecute() {
                 progressDialog = new ProgressDialog(PlanningList_activity.this);
                 progressDialog.setMessage("Loading...");
                 progressDialog.setCancelable(false);
                 progressDialog.show();
                 Log.e("pre execute", "yes");

         }

         @Override
         public ArrayList<Planning> doInBackground(Void... params) {
                 try {
                         Log.e("In background", "yee");
                         return loadData();

                 } catch (Exception e) {
                         Log.e("get lost", e.getMessage());
                 }
                 return null;
         }

         @Override
         protected void onPostExecute(ArrayList<Planning> result) {
                 super.onPostExecute(result);

                 dbHelper.close();
                 Log.e("data saving", "yoo");
                 dataAdapter = new planningListViewAdapter(getApplicationContext(),result);
                 listView.setAdapter(dataAdapter);

                 if (progressDialog.isShowing()) {
                         progressDialog.dismiss();
                 }

                 Toast.makeText(getApplicationContext(), "data loaded",Toast.LENGTH_LONG).show(); 
         }

         private ArrayList<Planning> loadData() {

                 // Load data here from database
                 return dbHelper.getPlanningList();

         }
 }

数据库方法

public ArrayList<Planning> getPlanningList() {

    SQLiteDatabase db = getReadableDatabase();
    ArrayList<Planning> planningList = new ArrayList<Planning>();
    Cursor mCursor = db.query(DOC_PLANNING_TABLE_NAME, null,null,null,null,null,null);
    if (mCursor != null && mCursor.moveToFirst()) {
            do {
                    Planning planning = new Planning();
                    String docCode = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_dr_code));
                    String planno = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_PLAN_NO));
                   String terrCode = mCursor.getString(mCursor
                                    .getColumnIndex(DCP_COLUMN_terr_code));
                    String adr = mCursor.getString(mCursor
                                    .getColumnIndex(DCP_COLUMN_PLAN_DATE));
                    String classdesc= mCursor.getString(mCursor
                                    .getColumnIndex(DCP_COLUMN_MON));
                    String speciality= mCursor.getString(mCursor
                                  .getColumnIndex(DCP_COLUMN_REF_NO));

                    String shift= mCursor.getString(mCursor
                            .getColumnIndex(DCP_COLUMN_MORN_EVEN)); 

                    planning.setDrCode(docCode);
                    planning.setDocName(planno);
                    planning.setTerrCode(terrCode);
                    planning.setAdr(adr);
                    planning.setClassDesc(classdesc);
                    planning.setShift(shift);
                      planning.setSpeciality(speciality);
            } while (mCursor.moveToNext());
            mCursor.close();
    }
    return planningList;

}

我浪费了很多时间找到问题,但我不明白为什么它没有在屏幕上显示。请指导我。

2 个答案:

答案 0 :(得分:0)

在您的代码中更正如下:

@Override   
public Object getItem(int arg0) {
          return planArraylist.get(planArraylist.get(arg0-1));   }

  @Override   
  public long getItemId(int arg0) {
          return arg0;   }

答案 1 :(得分:0)

我认为你用不同的方式编写getView方法并使用holder类,如下所示:

    @Override
    public Object getItem(int arg0) {
      return planArraylist.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
      return arg0;
    }

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

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.single_lv_item, parent, false);

            holder = new ViewHolder();

            holder.tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
            holder.tvDrName = (TextView) convertView.findViewById(R.id.dname);
            holder.tvTerr = (TextView) convertView.findViewById(R.id.tcode);
            holder.tvAdr = (TextView) convertView.findViewById(R.id.a);
            holder.tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
            holder.tvShift = (TextView) convertView.findViewById(R.id.tvshift); 

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

          Planning p = planArraylist.get(position);

          holder.tvdrCode.setText(p.getDrCode());
          holder.tvDrName.setText(p.getDocName());
          holder.tvTerr.setText(p.getTerrCode());
          holder.tvAdr.setText(p.getAdr());
          holder.tvClassDesc.setText(p.getClassDesc());
          holder.tvShift.setText(p.getShift());

        return convertView;
    }

    static class ViewHolder {
          TextView tvdrCode;
          TextView tvDrName;
          TextView tvTerr;
          TextView tvAdr;
          TextView tvClassDesc;
          TextView tvShift;
    }

此外,列表视图xml文件使用线性布局,但您的属性只能在相对布局中找到,例如android:layout_alignParentTop="true"。你能解决这个问题吗?