1

テーブルを含む SQLite データベースがあります。ListActivityとListViewを使ってテーブルの内容を表示したい。問題は、これしか表示されないことです

database.VesproModel@41814440

VesproModel.java クラスは、テーブルのフィールドとそのゲッターおよびセッターを保持するだけです。デバイスからデータベースをプルしてコンピューターで表示することで、検証したテーブルにデータを正常に挿入できました。

これは私がデータをフェッチする方法です:

public List<VesproModel> getAllComments() {
    List<VesproModel> comments = new ArrayList<VesproModel>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_TPCS_VESSEL, allColumns, null, null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      VesproModel comment = cursorToComment(cursor);
      comments.add(comment);
      cursor.moveToNext();
    }
    cursor.close();
    return comments;
  }

private VesproModel cursorToComment(Cursor cursor) {
    VesproModel comment = new VesproModel();
    Log.d("cursorToComment", "Before if cursorToComment");
    if( cursor != null && cursor.moveToFirst() ){
        Log.d("cursorToComment", "inside cursorToComment");
        comment.setId(cursor.getLong(0));
        comment.setImo_number(cursor.getString(1));
        ...
        ...
        ...}
        return comment;
        }

そして、次のように ListActivity でこの関数を呼び出しています。

public class CurrentList extends ListActivity {
private VesproDataSource datasource;

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

    datasource = new VesproDataSource(this);
    datasource.open();

    List<VesproModel> values = datasource.getAllComments();

    ArrayAdapter<VesproModel> adapter = new ArrayAdapter<VesproModel>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
     } 
   }

そして、これは上記の ListActivity に対応するレイアウトです:

<?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" >

 <ListView 
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
</ListView>
</LinearLayout>

テーブルのデータを画面に表示するにはどうすればよいですか。テーブルにはかなりの数の列があるため、使用するレイアウトのタイプを提案してください。ありがとう。

編集1:

提案どおり、ArrayAdapter クラスを拡張して Vespromodel 変数の表示を処理するカスタム クラスを作成しました。

public class ItemAdapter extends ArrayAdapter<VesproModel> {
private List<VesproModel> objects;
public ItemAdapter(Context context, int textViewResourceId, List<VesproModel> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
}
    public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }
    if (i != null) {
            // I inserted some textviews to test the data out. For the final display , I would need many more textviews or a TableLayout
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
        TextView mt = (TextView) v.findViewById(R.id.middletext);
        TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        TextView btd = (TextView) v.findViewById(R.id.desctext);
        if (tt != null){
            tt.setText("Imo_number: ");
        }
        if (ttd != null){
            ttd.setText(i.getImo_number());
        }
        if (mt != null){
            mt.setText("Vessel Name : ");
        }
        if (mtd != null){
            mtd.setText(i.getVessel_name());
        }
        if (bt != null){
            bt.setText("Vessel_type : ");
        }
        if (btd != null){
            btd.setText(i.getVessel_type());
        }
    }
    return v;
}

}

私はこれを ListActivity から次のように呼び出しています:

            List<VesproModel> values = dsrc.getAllComments();  // function is defined in the above section.
    m_adapter = new ItemAdapter(this, R.layout.list_item, values);
    setListAdapter(m_adapter);

現在、アクティビティは getAllComments() メソッド内で無限ループに陥っています。

4

3 に答える 3

2

ListViewから値を表示する方法がわからないため、クラスVesproModelの名前が表示されます。VesproModel必要なのは、クラスのオブジェクトからデータをフェッチする方法を知っている独自のアダプターを実装することです。

または、VesproModelクラスにたとえば表示したいフィールドがある場合 - それを呼び出しましょう- の代わりにtext使用できます。ArrayAdapter<String>ArrayAdapter<VesproModel>

List<VesproModel> values = datasource.getAllComments();
ArrayList<String> valuesToDisplay = new ArrayList<String>();
for(VesproModel vm : values) {
    valuesToDisplay.add(vm.text);
}

ArrayAdapter<String> adapter = new ArrayAdapter<VesproModel>(this,
        android.R.layout.simple_list_item_1, valuesToDisplay);
setListAdapter(adapter);

このようにして、文字列のリストを取得し、ArrayAdapterそれらの文字列を表示する方法を正確に知っています。

さらに詳しい情報が必要な場合はお知らせください。より明確な説明をいたします。

于 2013-06-25T10:49:54.873 に答える
1

で使用ResourceIdしてArrayAdapterいる は文字列のみを受け入れるため、値オブジェクトはArraylist文字列型のみである必要があります。

ここで、 を拡張するアダプタを作成し、独自の行を作成して のメソッドにArrayAdapter<VesproModel>コメントを表示する必要があります。getViewArrayAdapter

アダプターを作成したら、次のように言って呼び出すことができます。

 MyAdapter<VesproModel> adapter = new MyAdapter<VesproModel>(this,
            R.layout.your_layout, values);

どこvalues = new ArrayList<VesproModel>;

于 2013-06-25T11:09:58.653 に答える
0

モキトスの答えは正しいです。toString()別のアプローチは、カスタム クラスのメソッドをオーバーライドすることVesproModelです。次に、取得する代わりに、入力database.VesproModel@41814440したものを出力しますtoString()。たとえば、Mosquitos のアプローチに従って、次のようなことができます。

@Override
public String toString(){
    return text;
}
于 2013-06-25T11:04:12.097 に答える