gpt4 book ai didi

android - 将 ImageView 添加到 ListView

转载 作者:行者123 更新时间:2023-11-29 02:09:02 26 4
gpt4 key购买 nike

我有一个扩展 Activity 并在主布局中扩充另一个 xml 布局的类。

例子:

public class PrivateHistory extends Activity{

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main_layout);

viewBundleMessage();

LinearLayout item = (LinearLayout)findViewById(R.id.menu_linear_layout);
View child = getLayoutInflater().inflate(R.layout.menu_list, null);
item.addView(child);

getHistoryInfo();

}
}

示例(菜单列表布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listViewMenu">

</ListView>

还有一个 getHistoryInfo 方法,我用它从数据库中检索一些历史信息,这样我就可以将每条记录放在一个 ListItem 中。

例子:

public void getHistoryInfo(){

//removed historyItem + database information

ListView lv = (ListView) findViewById(R.id.listViewMenu);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.history_list_view, historyItem));

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

showToast(historyItem.get(position).toString());
}
});

}

示例(history_list_view):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@drawable/bg_white_grey"
android:gravity="center_vertical"
android:typeface="serif"
android:layout_marginBottom="10dp"/>

所以这里的一切都有效,但前提是您要将文本添加到每个 ListItem 中。我想将图像添加到每个 ListItems,但是如何呢?如果这个类扩展了一个 ListActivity,那就没那么难了,但它扩展了一个 Activity。那么有人可以帮我吗?

任何想使用我的应用程序的人,请访问: http://www.4shared.com/android/tWfbNqZ6/Free_Wallet.html

最佳答案

要为列表自定义行(即 TextView 以外的任何内容),您应该创建自己的 adapter -执行。这是解释一般原则的另一个答案(我的):
Two views in each list item (该示例是关于两个 textview 而不是图像,但是只需将布局替换为两个 textview 以使用 imageview 的布局并更改 getView() 方法中的相应行)

关于android - 将 ImageView 添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com