覆盖 getview 时如何显示我的数据

发布于 2024-12-06 12:47:50 字数 3228 浏览 0 评论 0原文

我一直在学习 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;
            }   

}






 }

Ive been learning about the getview . But i cant get it to display the data in my listview. Can anyone help code is below

//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;
            }   

}






 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-12-13 12:47:50

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

http://vbsteven.com/archives/24

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

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

If you're using a SimpleAdapter, you don't need to extend the class and customize the getView() as the SimpleAdapter handles the mapping of data to your layout via the resource, from and to parameters. Take a look at this tutorial for the idea:

http://vbsteven.com/archives/24

If you want to do more involved customization, use a BaseAdapter, which the SimpleAdapter actually extends. Here's a tutorial with examples of that:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文