在ListView项目2线 [英] 2 lines in a listView item
问题描述
我想在每一个列表视图两行item.this是我的code,但它不工作...
重新编辑code
公共类NEA扩展ListActivity {
私人列表<消息>消息;
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main2);
loadFeed(ParserType.DOM);
}
私人无效loadFeed(ParserType型){
尝试{
Log.i(新闻报,ParserType =+ type.name());
FeedParser解析器= FeedParserFactory.getParser(类型);
长时间启动= System.currentTimeMillis的();
消息= parser.parse();
持续时间长= System.currentTimeMillis的() - 启动;
Log.i(新闻报,分析器持续时间=+持续时间);
XML字符串=中WriteXML();
Log.i(新闻,XML);
名单<字符串>标题=新的ArrayList<字符串>(messages.size());
对于(消息消息:消息){
串O = titles.get(messages.size());
如果(O!= NULL){
TextView的TT =(TextView中)findViewById(R.id.TextView01);
TextView的BT =(TextView中)findViewById(R.id.TextView02);
如果(TT!= NULL){
titles.add(+ msg.getDate()); }
如果(BT!= NULL){
titles.add(日期+ msg.getTitle());
}返回;
//titles.add(msg.getDate()+\ N+ msg.getTitle());
}
// titles.add(msg.getTitle()++ msg.getDate());
}
ArrayAdapter<字符串>适配器=
新的ArrayAdapter<字符串>(这一点,R.layout.row,职称);
this.setListAdapter(适配器);
// textView.setText(Html.fromHtml(titles.get(位置)));
}赶上(的Throwable T){
Log.e(新闻,t.getMessage(),T);
}
}
第二种方式 - 不是工作压力太大
名单,其中,字符串>标题=新的ArrayList<字符串>(messages.size());
对于(消息消息:消息){
串O = titles.get(messages.size());
如果(O!= NULL){
TextView的TT =(TextView中)findViewById(R.id.TextView01);
TextView的BT =(TextView中)findViewById(R.id.TextView02);
如果(TT!= NULL){
titles.add(+ msg.getDate()); }
如果(BT!= NULL){
titles.add(日期+ msg.getTitle());
}返回;
//titles.add(msg.getDate()+\ N+ msg.getTitle());
}
// titles.add(msg.getTitle()++ msg.getDate());
}
ArrayAdapter<字符串>适配器=
新的ArrayAdapter<字符串>(这一点,R.layout.row,职称);
this.setListAdapter(适配器);
// textView.setText(Html.fromHtml(titles.get(位置)));
}赶上(的Throwable T){
Log.e(新闻,t.getMessage(),T);
}
为了创建一个的ListView
用在每个项目上两行文字,我用的是 SimpleAdapter
以下列方式:
创建一个列表
,将保留您的数据。每个映射项将具有的的ListView
产品每一个密钥(第一行)和价值(第二行)。
名单,其中,地图<字符串,字符串>>数据=新的ArrayList<地图<字符串,字符串>>();
有关我们希望把在的ListView
,创建一个的HashMap
并将其加入到申报的名单每对之前。
地图<字符串,字符串>基准=新的HashMap<字符串,字符串>(2);
(第一行文字,第一线)datum.put;
datum.put(二线,文本第二行);
data.add(数据);
然后声明 SimpleAdapter
。注意有使用行头,并在构造函数中二线的字符串。这将告诉适配器如何使用地图
之前声明的字符串。还要注意使用的布局 android.R.layout.simple_list_item_2
和文本ID android.R.id.text1
和 android.R.id.text2
。
SimpleAdapter适配器=新SimpleAdapter(这一点,数据,
android.R.layout.simple_list_item_2,
新的String [] {行头,二线},
新的INT [] {android.R.id.text1,android.R.id.text2});
i would like to have two lines in every listview item.this is my code but its not working...
RE-EDITED CODE
public class nea extends ListActivity{
private List<Message> messages;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main2);
loadFeed(ParserType.DOM);
}
private void loadFeed(ParserType type){
try{
Log.i("News", "ParserType="+type.name());
FeedParser parser = FeedParserFactory.getParser(type);
long start = System.currentTimeMillis();
messages = parser.parse();
long duration = System.currentTimeMillis() - start;
Log.i("News", "Parser duration=" + duration);
String xml = writeXml();
Log.i("News", xml);
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
String o = titles.get(messages.size());
if (o != null) {
TextView tt = (TextView) findViewById(R.id.TextView01);
TextView bt = (TextView) findViewById(R.id.TextView02);
if (tt != null) {
titles.add(""+msg.getDate()); }
if(bt != null){
titles.add("date: "+ msg.getTitle());
}return;
//titles.add(msg.getDate()+"\n"+msg.getTitle());
}
// titles.add(msg.getTitle()+" "+msg.getDate());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
// textView.setText(Html.fromHtml(titles.get(position)));
} catch (Throwable t){
Log.e("News",t.getMessage(),t);
}
}
SECOND WAY--not working too
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
String o = titles.get(messages.size());
if (o != null) {
TextView tt = (TextView) findViewById(R.id.TextView01);
TextView bt = (TextView) findViewById(R.id.TextView02);
if (tt != null) {
titles.add(""+msg.getDate()); }
if(bt != null){
titles.add("date: "+ msg.getTitle());
}return;
//titles.add(msg.getDate()+"\n"+msg.getTitle());
}
// titles.add(msg.getTitle()+" "+msg.getDate());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
// textView.setText(Html.fromHtml(titles.get(position)));
} catch (Throwable t){
Log.e("News",t.getMessage(),t);
}
In order to create a ListView
with two lines of text on each item, I use a SimpleAdapter
in the following way:
Create a List
that will hold your data. Each Map entry will have a Key (First line) and a Value (Second line) of each one of the ListView
Items.
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
For each pair we want to put in the ListView
, create a HashMap
and add it into the List declared before.
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("First Line", "First line of text");
datum.put("Second Line","Second line of text");
data.add(datum);
Then declare a SimpleAdapter
. Note there the use of "First Line" and "Second Line" strings inside the constructor. This will tell the adapter how to use the strings of the Map
declared before. Note too the use of the layout android.R.layout.simple_list_item_2
and the text id android.R.id.text1
and android.R.id.text2
.
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"First Line", "Second Line" },
new int[] {android.R.id.text1, android.R.id.text2 });
这篇关于在ListView项目2线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!