【问题标题】:How do i display my data when overriding getview覆盖getview时如何显示我的数据
【发布时间】:2011-09-25 15:00:35
【问题描述】:

我一直在学习 getview 。但我无法让它在我的列表视图中显示数据。任何人都可以帮助代码如下

//public class OrderProductSearch extends Activity {
public class OrderProductSearch extends Activity {
 ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> item = new HashMap<String,String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try{
        setContentView(R.layout.orderproducts);
    }
    catch (Exception e) {
        //
        String shaw="";
        shaw = e.getMessage();
    }

    //Create view of the list where content will be stored
    final ListView listContent = (ListView)findViewById(R.id.orderproductlistview); 

    //Set for fast scrolling 
    listContent.setFastScrollEnabled(true);

    //Create instance of the database
    final DbAdapter db = new DbAdapter(this); 

    //Open the Database and read from it
    db.openToRead();

    //Routine to call all product sub groups from the database
    final Cursor cursor = db.getAllSubGroupProduct();
   //Manages the cursor
    startManagingCursor(cursor);
    int i=0;
    cursor.moveToFirst();
    while (cursor.getPosition() < cursor.getCount()) {
         item.put("ProdName",cursor.getString(2));
         item.put("ProdSize", cursor.getString(3));
         item.put("ProdPack",cursor.getString(4));
         item.put("OrdQty","0");

         //list.add(item);
         list.add(i, item);
         item = new HashMap<String,String>();   
         cursor.moveToNext();
        i = i + 1;

    }

    String[] from = new String[] {"ProdName", "ProdSize", "ProdPack", "OrdQty"};

    int[] to = new int[] { R.id.productlinerow, R.id.productlinerow2, R.id.productlinerow3, R.id.productlinerow4};
    //SimpleAdapter notes = new SimpleAdapter(OrderProductSearch.this,list,R.layout.productlinerow,from,to);        
    NewAdapter notes = new NewAdapter(OrderProductSearch.this,list,R.layout.productlinerow,from,to);


    listContent.setAdapter(notes);


    //Close the database
    db.close();     


        }

class NewAdapter extends SimpleAdapter{
    int resource;
    Context cxt;
    private Context context;
    List<? extends Map<String, ?>> DataList;

    public NewAdapter(Context context, List<? extends Map<String, ?>> data,
            int _resource, String[] from, int[] to) {
        super(context, data, _resource, from, to);
        resource = _resource;
        this.context = context;
        DataList = data;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {            
        View v = convertView;

           if (v == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.productlinerow, null);
           }

    //       TextView bTitle = (TextView) v.findViewById(R.id.productlinerow);
  //         bTitle.setText(DataList[position,1][1].toString());

        return v;
            }   

}






 }

【问题讨论】:

  • 您必须改为扩展 CursorAdapter。
  • 为什么?我所有的数据都在一个简单的列表中,在我尝试 getview 之前可以正常工作。通过将它放在一个数组中,我可以额外的字段不在数据库中。

标签: android listview view simpleadapter


【解决方案1】:

如果您使用的是 SimpleAdapter,则无需扩展类并自定义 getView(),因为 SimpleAdapter 通过资源、from 和 to 参数处理数据到布局的映射。看看这个教程的想法:

http://vbsteven.com/archives/24

如果您想进行更多涉及的自定义,请使用 BaseAdapter,它实际上是 SimpleAdapter 的扩展。这是一个带有示例的教程:

http://android.amberfog.com/?p=296

【讨论】:

  • 您好,感谢您回复我。问题是我必须使用它,就好像你在列表视图上放置一个编辑文本框(供用户添加数量)然后你不能在你必须使用 getview overide 的行上使用正常的点击监听器。
  • 所有我想做的事情就是显示一个产品列表和一个描述和一个数量框,让他们在列表视图上添加一个数量不能相信这样做太尴尬了。
  • 不应该那么尴尬 - 使用适当的布局创建布局行,其中包括文本空间以及 EditText。然后,扩展一个 BaseAdapter 类,您可以在其中覆盖 getView()。在那里,您将膨胀您的 EditText 并使用 addTextChangeListner() 方法来分配您的侦听器。
  • 好的,谢谢 baseadapter 会在 getview 中自动显示数据还是我需要对其进行编码?
  • 这让我回到了我最初的问题,我将如何编码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-08
  • 2012-12-07
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多