Android ListView-如何显示
我想在我的列表视图中显示项目,如下图所示Android ListView-如何显示,android,listview,android-listview,android-activity,Android,Listview,Android Listview,Android Activity,我想在我的列表视图中显示项目,如下图所示 左边的正方形是一个图像 右边的矩形是文本框 问题: 我的XML应该是什么样子 如何在那里插入项目 当前代码: 布局: activity_news_feed.xml <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/newsfeed" android:layout_width="wrap_content"
- 左边的正方形是一个图像
- 右边的矩形是文本框
- 我的XML应该是什么样子
- 如何在那里插入项目
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newsfeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
list_single.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
活动:
package uk.ac.gla.serengeti.activities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import uk.ac.gla.serengeti.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class NewsFeed extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
// Show the Up button in the action bar.
setupActionBar();
final ListView listview = (ListView) findViewById(R.id.newsfeed);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
}
private void setupActionBar() {
ActionBar actionBar = this.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news_feed, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
//NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
包uk.ac.gla.serengeti.activities;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
进口英国ac.gla.serengeti.R;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.os.Bundle;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.ActionBarActivity;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类新闻源扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u news\u feed);
//在操作栏中显示“向上”按钮。
setupActionBar();
最终ListView ListView=(ListView)findViewById(R.id.newsfeed);
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”、“Ubuntu”、“Windows7”、“Max OS X”、“Linux”,
“OS/2”、“Ubuntu”、“Windows7”、“Max OS X”、“Linux”、“OS/2”,
“安卓”、“iPhone”、“WindowsMobile”};
最终ArrayList=新ArrayList();
对于(int i=0;i
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLEft="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
我正在挣扎布局:)当然,请粘贴您的XMLI更新的代码。我不确定何时何地应该在单个布局中扩大列表。您的XML没有根元素。所有XML文件都必须有一个根,例如“我的答案”中XML中的第一个LinearLayout。第二,你应该覆盖你的
stablerayadapter
的getView方法,这是你应该膨胀你的列表的地方。我不能聊天,因为我在工作,但我已经更正了我的getView()代码。我发现你想要的最好的解决方案可能对你也有帮助。
ImageView image = ( ImageView ) convertView.findViewById ( R.id.image );
TextView title = ( TextView ) convertView.findViewById ( R.id.title );
TextView description = ( TextView ) convertView.findViewById ( R.id.description );
Recipe recipe = this.getItem ( position );
image.setImageBitmap(recipte.getBitmap());
title.setText(recipe.getTitle());
description.setText(recipe.getDescription());
return convertView;
}