如何用简单的适配器和列表视图创建自己的自定义行布局 [英] How to create own custom row layout using simple adapter and listview

查看:125
本文介绍了如何用简单的适配器和列表视图创建自己的自定义行布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个自定义列表视图,可以让我有每行2串/ textviews帮助。我一直在研究了很多,但我似乎无法理解如何做到这一点。任何样品code和帮助将是AP preciated。我知道如何使用simple_list_item_1管理,而不是我自己的布局。 谢谢。 我的(静止无功能)code

 包com.painLogger;
 //全进口

 公共类PainLoggerActivity扩展活动实现OnClickListener,
 OnKeyListener {

     / **第一次创建活动时调用。 * /
     的EditText txtItem;
     的EditText txtItem2;
     按钮btnAdd;
     ListView控件时listItems;
     ArrayAdapter<字符串> AA;
     名单< HashMap的<字符串,字符串>> painItems =新的ArrayList< HashMap的<字符串,字符串>> ();
     INT []键;
     的String []的;
     SimpleAdapter适配器;


     @覆盖
     公共无效的onCreate(包savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.main);

         txtItem =(EditText上)findViewById(R.id.txtItem);
         txtItem2 =(EditText上)findViewById(R.id.txtItem2);

         btnAdd =(按钮)findViewById(R.id.btnAdd);
         listItems中的=(的ListView)findViewById(R.id.listItems);

         btnAdd.setOnClickListener(本);

         从=新的String [] {
             row_1,row_2
         };
         为=新INT [] {
             R.id.row1,R.id.row2
         };

         SimpleAdapter适配器=新SimpleAdapter(这一点,painItems,R.layout.mylistlayout,
             从到);
         listItems.setAdapter(适配器);
     }

     私人无效的addItem(){
         HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串> ();

         map.put(row_1,txtItem.getText()的toString());
         map.put(row_2,txtItem2.getText()的toString());
         painItems.add(图)
         adapter.notifyDataSetChanged();
     }

     @覆盖
     公共无效的onClick(视图v){
         如果(V == this.btnAdd){
             的addItem();
         }
     }

     @覆盖
     公共布尔onKey(视图V,INT关键code,KeyEvent的事件){

         如果(event.getAction()== KeyEvent.ACTION_DOWN和放大器;&放大器;关键code ==
             KeyEvent.KEY code_DPAD_CENTER){THIS.ADDITEM();

         }
         返回false;
     }
 }
 

解决方案

参照这个问题,使用code。
编辑:添加HashMap中定义

 的String []从=新的String [] {row_1,row_2};
INT []到=新INT [] {R.id.row1,R.id.row2};
名单< HashMap的<字符串,字符串>> fillMaps =新的ArrayList< HashMap的<字符串,字符串>>();

对于(INT J = 0; J< sourceObj.length(); J ++){
        HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
        map.put(row_1,sourceObj.data1);
        map.put(row_2,sourceObj.data2);
        fillMaps.add(图)
}

SimpleAdapter适配器=新SimpleAdapter(背景下,fillMaps,R.layout.yourlayoutname,从,到);
mListView.setAdapter(适配器);
 

  • 请您的列表视图,可以是一个的LinearLayout 一对夫妇的 TextViews
  • 参考在此行中使用 R.layout.yourlayoutname 此列表布局 SimpleAdapter适配器=新SimpleAdapter(背景下,fillMaps,R.layout.result从,到);
  • 通过你的数据

这个方法的好处是,它避免了你创建任何新的对象,它不涉及太多code。

I need help in creating a custom listview that allows me to have 2 strings/textviews per row. I have been researching a lot, but I cannot seem to understand how to do this. Any sample code and help would be appreciated. I know how to use simple_list_item_1, but not my own layout. Thank YOU. My (Still Non-Functioning) Code

 package com.painLogger;
 //ALL IMPORTS

 public class PainLoggerActivity extends Activity implements OnClickListener,
 OnKeyListener {

     /** Called when the activity is first created. */
     EditText txtItem;
     EditText txtItem2;
     Button btnAdd;
     ListView listItems;
     ArrayAdapter < String > aa;
     List < HashMap < String, String >> painItems = new ArrayList < HashMap < String, String >> ();
     int[] to;
     String[] from;
     SimpleAdapter adapter;


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

         txtItem = (EditText) findViewById(R.id.txtItem);
         txtItem2 = (EditText) findViewById(R.id.txtItem2);

         btnAdd = (Button) findViewById(R.id.btnAdd);
         listItems = (ListView) findViewById(R.id.listItems);

         btnAdd.setOnClickListener(this);

         from = new String[] {
             "row_1", "row_2"
         };
         to = new int[] {
             R.id.row1, R.id.row2
         };

         SimpleAdapter adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
             from, to);
         listItems.setAdapter(adapter);
     }

     private void addItem() {
         HashMap < String, String > map = new HashMap < String, String > ();

         map.put("row_1", txtItem.getText().toString());
         map.put("row_2", txtItem2.getText().toString());
         painItems.add(map);
         adapter.notifyDataSetChanged();
     }

     @Override
     public void onClick(View v) {
         if (v == this.btnAdd) {
             addItem();
         }
     }

     @Override
     public boolean onKey(View v, int keyCode, KeyEvent event) {

         if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode ==
             KeyEvent.KEYCODE_DPAD_CENTER) {             this.addItem();

         }
         return false;
     }
 }

解决方案

With reference to this question, use this code.
EDIT: Added hashmap definition

String[] from = new String[] {"row_1", "row_2"};
int[] to = new int[] { R.id.row1, R.id.row2};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

for (int j = 0; j < sourceObj.length(); j++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("row_1", sourceObj.data1);
        map.put("row_2", sourceObj.data2);
        fillMaps.add(map);
}

SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.yourlayoutname, from, to);
mListView.setAdapter(adapter);

  • Make your list view which could be a LinearLayout with a couple of TextViews
  • Reference this list layout using R.layout.yourlayoutname in this line SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
  • Pass in your data

The good thing about this approach is that it avoids you having to create any new objects, and it doesn't involve much code.

这篇关于如何用简单的适配器和列表视图创建自己的自定义行布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆