简体   繁体   English

列出每个项目带有多个TextViews

[英]List with multiple TextViews for each item

For an android application, I needed to create a list that would allow me to enter different things into the same 'item'. 对于Android应用程序,我需要创建一个列表,该列表使我可以将不同的内容输入到同一“项目”中。 For example, each one of the objects in the list would need different information(each with a separate row). 例如,列表中的每个对象都需要不同的信息(每个对象都有单独的行)。 Specifically, my app would want the following for each list item: pain location, treatment, type of pain, and some other categories per instance. 具体来说,我的应用程序希望为每个列表项提供以下内容:疼痛位置,治疗,疼痛类型以及每个实例的其他一些类别。

This is what I have gathered so far, but it only displays one TextView per item on the list: 到目前为止,这是我收集的内容,但是列表上每个项目仅显示一个TextView:

PACKAGE, Imports, etc.



public class PainLoggerActivity extends Activity implements OnClickListener,   
   OnKeyListener {
/** Called when the activity is first created. */
    EditText txtItem;
    Button btnAdd;
    ListView listItems;
    ArrayList <String> painItems;
    ArrayAdapter<String> aa;


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

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

    btnAdd.setOnClickListener(this);
    txtItem.setOnKeyListener(this);
    painItems = new ArrayList<String>();
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
    painItems);
    listItems.setAdapter(aa);

    }
private void addItem(String item){
    if(item.length() > 0){
        this.painItems.add(item);
        this.aa.notifyDataSetChanged();
        this.txtItem.setText("");       
    }

}

@Override
public void onClick(View v) {

    if(v == this.btnAdd)
        this.addItem(this.txtItem.getText().toString());
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

    if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == 
              KeyEvent.KEYCODE_DPAD_CENTER){
        this.addItem(this.txtItem.getText().toString());
    }
    return false;

}

} }

* Some sample code would be appreciated, so I have an idea how to implement suggestions - no doubt, I am still a beginner * * 一些示例代码将不胜感激,所以我对如何实现建议有一个想法-毫无疑问,我仍然是初学者*

It's not possible to have multiple TextView items when using the layout "simple_list_item_1" for your Adapter's layout ... that's why they call it "simple - item - 1". 将布局“ simple_list_item_1”用作适配器的布局时,不可能有多个TextView项...这就是为什么他们将其称为“简单-项-1”。

If you want a total of 2 TextViews per row you can use the "simple_list_item_2" but I actually recommend you go with using a SimpleAdapter and creating your own row entry layout file. 如果您希望每行总共2个TextView,则可以使用“ simple_list_item_2”,但实际上我建议您使用SimpleAdapter并创建自己的行条目布局文件。 It's much more flexible and you can include as many text items, images, check boxes, or anything else you can think of, versus just a couple of text items on a single row. 它更加灵活,您可以包括尽可能多的文本项,图像,复选框或其他您能想到的内容,而不是在一行中仅包含几个文本项。

Food for thought ... 令人回味的食物...

Do a search for SimpleAdapter examples to see how to implement this. 搜索SimpleAdapter示例以了解如何实现。 Not a big deal, and you'll find it's a "gift that keeps on giving." 没什么大不了的,您会发现这是“不断付出的礼物”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM