我想在每个列表视图项中有两行.这是我的代码但是,它不起作用.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
第二种方式 - 不工作
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);
}
Run Code Online (Sandbox Code Playgroud)
Mig*_*Dus 20
为了ListView在每个项目上创建两行文本,我使用SimpleAdapter以下方式:
创建一个List将保存您的数据.每个Map条目都有一个Key(第一行)和每个ListViewItems 的Value(第二行).
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Run Code Online (Sandbox Code Playgroud)
对于我们想要放入的每一对ListView,创建一个HashMap并将其添加到之前声明的List中.
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);
Run Code Online (Sandbox Code Playgroud)
注意:如果要添加多行,则需要创建与datum上面类似的另一个对象.
然后宣布一个SimpleAdapter.请注意,在构造函数中使用"第一行"和"第二行"字符串.这将告诉适配器如何使用Map之前声明的字符串.还要注意使用的布局android.R.layout.simple_list_item_2和文本ID android.R.id.text1和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 });
Run Code Online (Sandbox Code Playgroud)
然后,您需要设置adapter为您的适配器ListView
ListView listView = (ListView) findViewById(R.id.theIdOfYourListView);
listView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
线路:
TextView tt = (TextView) findViewById(R.id.TextView01);
TextView bt = (TextView) findViewById(R.id.TextView02);
Run Code Online (Sandbox Code Playgroud)
没有意义,因为此时没有列表项视图msg,并且您不是在列表项视图中按 ID 查找文本视图,而是在主视图中查找。
当您的应用程序首次启动时,您有一个空的ListView. 然后,您将消息解析为onCreate(顺便说一句,这不是一个好主意,因为不应在 UI 线程中执行耗时的过程)的一部分,然后继续尝试通过 ID 查找不存在的文本视图,R.id.TextView01并R.id.TextView02在主要视图。
请记住,您将需要两个布局 XML 文件:一个用于列表活动的布局,另一个用于每个列表项的布局。另请记住,查询另一个视图中的findViewById视图。在本例中,它尝试查询主视图,但没有文本视图和; 文本视图,是通过膨胀列表项布局获得的列表项视图的元素。TextView01TextView02TextView01TextView02
我强烈建议您首先从 API 演示开始,这样您就可以看到一个完整的、与您想要完成的任务类似的工作示例。例如,http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
| 归档时间: |
|
| 查看次数: |
29371 次 |
| 最近记录: |