Android 如何在列表视图中显示预发布数据库中的多个列?

Android 如何在列表视图中显示预发布数据库中的多个列?,android,listview,android-sqlite,Android,Listview,Android Sqlite,这是我使用预填充数据库的主要活动。我试图在列表视图中显示行。我的数据库中有五列,我想显示其中除id之外的所有列 public class MainActivity extends Activity implements OnClickListener{ private IngHelper dbIngHelper = null; private Cursor curname=null; private SimpleCursorAdapter adapter = null; @Override

这是我使用预填充数据库的主要活动。我试图在列表视图中显示行。我的数据库中有五列,我想显示其中除id之外的所有列

public class MainActivity extends Activity implements OnClickListener{

private IngHelper dbIngHelper = null;
private Cursor curname=null;
private SimpleCursorAdapter adapter = null;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView myList; 
    myList = (ListView)findViewById(R.id.listView1);

    dbIngHelper = new IngHelper(this);
    dbIngHelper.createDB();
    dbIngHelper.openDB();


    fillData();
}


public void fillData(){

    //ListView myList; 
    ListView myList; 
    myList = (ListView)findViewById(R.id.listView1);
    curname=dbIngHelper.getCursor();
    startManagingCursor(curname);
    String[] columns = new String[] {
            IngHelper.COLUMN_ID,
            IngHelper.COLUMN_NAME,
            IngHelper.COLUMN_NATURE,
            IngHelper.COLUMN_SECTION,
            IngHelper.COLUMN_PENALTY
      };

    int[] to = new int[] { 
            R.id.id,
            R.id.name,
            R.id.nature,
            R.id.penalty,
            R.id.section
          };

    adapter = new SimpleCursorAdapter(
            this, R.layout.activity_main, 
            curname, 
            columns, 
            to);



    myList.setAdapter(adapter);



myList.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> listView, View view,int position, long   id) {
   //Get the cursor, positioned to the corresponding row in the result set
   // Cursor cursor = (Cursor) listView.getItemAtPosition(position);


   }
});
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class IngAdapter extends CursorAdapter{
    @SuppressWarnings("deprecation")
    IngAdapter(Cursor c){
        super(MainActivity.this,c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub

        IngHolder holder=(IngHolder)row.getTag();
        holder.populateFrom(c,dbIngHelper);


    }

    @Override
    public View newView(Context cxtx, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inf = getLayoutInflater();
        View row=inf.inflate(R.layout.row, parent, false);
        IngHolder holder = new IngHolder(row);
        row.setTag(holder);
        return(row);
    }

}

static class IngHolder{
    private TextView name=null;
   //   private TextView nature=null;
    //private TextView section=null;
    //private TextView penalty=null;

    IngHolder(View row){

        name=(TextView)row.findViewById(R.id.name);

    }

    void populateFrom(Cursor c,IngHelper r){

        name.setText(r.getName(c));


    }


}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}
}
有两个xml文件。这是activity_main.xml。。。它有一个列表视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >



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

</ListView>

</RelativeLayout>

另一个布局是row.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="match_parent"
    android:orientation="vertical" >

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

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

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

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

<TextView
    android:id="@+id/penalty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"

    android:text="TextView" />

</LinearLayout>


我得到一个空白列表作为输出,每行有一个列表条目,但数据无法查看。请给出一些解决问题的建议

如果要显示所有列值,必须创建自定义适配器。请参阅和。

检查链接以了解您的问题。:)处理此问题的最佳视图必须是ListView吗?Android没有专用的视图来显示表格?
<?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="match_parent"
    android:orientation="vertical" >

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

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

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

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

<TextView
    android:id="@+id/penalty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"

    android:text="TextView" />

</LinearLayout>