1

我从这里http://www.vogella.com/articles/AndroidListView/article.html遵循了 Vogella 关于 ListViews 的上下文操作模式的教程。

无论如何,我想做的有点不同。本教程中使用的数据是初始化的字符串数组。但我的数据是从数据库中检索到的对象列表。我怎样才能做到这一点?在此先感谢您的帮助

这是名为 MyPerformanceArrayAdapter.java 的自定义 ArrayAdapter 的编码

    package de.vogella.android.listactivity;
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

     public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
     private final Activity context;
     private final String[] names;

      static class ViewHolder {
      public TextView text;
      public ImageView image;
      }

      public MyPerformanceArrayAdapter(Activity context, String[] names) {
       super(context, R.layout.rowlayout, names);
       this.context = context;
      this.names = names;
      }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
       View rowView = convertView;
        if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.rowlayout, null);
         ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
         viewHolder.image = (ImageView) rowView
      .findViewById(R.id.ImageView01);
       rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
if (s.startsWith("Windows7") || s.startsWith("iPhone")
    || s.startsWith("Solaris")) {
  holder.image.setImageResource(R.drawable.no);
} else {
  holder.image.setImageResource(R.drawable.ok);
}

return rowView;
     }
    }   

这是初始化字符串数据的活动的代码

    public class MyListActivityActionbar extends ListActivity {

    protected Object mActionMode;
    public int selectedItem = -1;

    public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);
      String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
    "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };

     MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
     setListAdapter(adapter);

      getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

     @Override
       public boolean onItemLongClick(AdapterView<?> parent, View view,
      int position, long id) {

    if (mActionMode != null) {
      return false;
    }
    selectedItem = position;

    // start the CAB using the ActionMode.Callback defined above
    mActionMode = MyListActivityActionbar.this
        .startActionMode(mActionModeCallback);
    view.setSelected(true);
    return true;
  }
});
      }

      private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

// called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  // Inflate a menu resource providing context menu items
  MenuInflater inflater = mode.getMenuInflater();
  // assumes that you have "contexual.xml" menu resources
  inflater.inflate(R.menu.rowselection, menu);
  return true;
}

// the following method is called each time 
// the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  return false; // Return false if nothing is done
}

// called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  switch (item.getItemId()) {
  case R.id.menuitem1_show:
    show();
    // the Action was executed, close the CAB
    mode.finish();
    return true;
  default:
    return false;
  }
}

// called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
  mActionMode = null;
  selectedItem = -1;
}
    };

     private void show() {
      Toast.makeText(MyListActivityActionbar.this,
        String.valueOf(selectedItem), Toast.LENGTH_LONG).show();
      }

     } 
4

3 回答 3

1

您从数据库中获取的对象列表,将其放入ArrayList<E>

适配器中的下一个传递ArrayList<E>并相应地更改适配器以传递ArrayList<E>而不是String[] array

于 2013-10-31T11:15:10.933 回答
1

你的适配器类...

public class MyPerformanceArrayAdapter extends ArrayAdapter<String>

应该...

public class MyPerformanceArrayAdapter extends ArrayAdapter<T> 

其中 T 是列表对象,而不是字符串。

这可能会有所帮助:ListView with objects tutorial

于 2013-11-01T02:10:02.583 回答
0

您可以轻松地列出从数据库中检索到的对象,然后创建自定义 ArrayAdapter。例如,在 listview 中显示的对象列表(我的 second-android-project 的一部分,所以它与数据更改有点纠结(方案列表是多余的))

class SchemeAdapter extends ArrayAdapter<Storable> {

    private final transient List<Storable> schemes;
    private final transient int layout;

    public SchemeAdapter(final Context context, final List<Storable> objects) {
        super(context, R.layout.list_row_choose_schedule,
                R.id.list_row_name_of_schedule, objects);

        layout = R.layout.list_row_choose_schedule;
        this.schemes = objects;
    }

    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {
        final View row = (convertView == null) ? getLayoutInflater()
                .inflate(layout, parent, false) : convertView;

        final TextView name = (TextView) row
                .findViewById(R.id.list_row_name_of_schedule);
        //can be replaced with (Scheme) getItem(position) or sth like that
        final Scheme Scheme = (Scheme) schemes.get(position);
        name.setText(Scheme.getName());

        return row;
    }

}

}

在 Activity 的 onCreate 中创建(完整的活动代码http://pastebin.com/3vj12USr):

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_scheme);

    final RunSource datasource = RunSource.getInstance(this);
    datasource.open();

    final Scheme scheme = new Scheme();
    schemes = datasource.getStorableList(scheme);
    schemes.add(0, scheme);

    setListAdapter(new SchemeAdapter(this, schemes));
}

从数据库中检索项目列表,如下所示:

public List<Storable> getStorableList(final Storable storable) {
            final List<Storable> list = new ArrayList<Storable>();
            final Cursor cursor = database.query(storable.getTableName(),
                            storable.getColNames(), null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                    final Storable run = storable.get(cursor);
                    list.add(run);
                    cursor.moveToNext();
            }
            cursor.close();
            return list;
    }

运行源代码在这里:http ://pastebin.com/xpFdcBvK

于 2013-11-01T09:37:58.200 回答