我必须填充自定义列表视图。 此列表视图包含一个文本框。
Listview XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listsong2_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/panel_shape" >
<Button
android:id="@+id/back_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Back_Recent_Song" />
</LinearLayout>
<ListView
android:id="@+id/listsong_recent_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
</ListView>
这是自定义列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="@+id/songTitlerecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:padding="10dp"
android:color="#f3f3f3"/>
</LinearLayout>
主XML文件:
<include android:id="@+id/list_song_layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="@layout/list_recentsongs_screen" />
如何在不扩展listactivity的情况下使用setlistadapter?
代码:
ListView recent = (ListView)findViewById(R.id.listsong_recent_listview);
ArrayList<Data> List = new ArrayList<Data>();
//Populate the array list
Log.d("Debug1*****************","******************");
List.add(new Data("test album1")); // Set Name
List.add(new Data("test album2"));
List.add(new Data("test album3"));
for (int i = 0; i < List.size(); i++) {
// creating new HashMap
Data dataobj = List.get(i);
String Name = dataobj.getName();
Log.d("Debug" ,"Name = " + Name);
}
//-------------------------------------------------------------------------------------------//
Log.d("Debug2*****************","******************");
Integer size = List.size();
Log.d("Debug3*****************","******************");
Log.d("DEBUG123","size of list "+ size);
Log.d("Debug4*****************","******************");
m_adapter = new MetadataArrayAdapter(this, R.layout.recentlistsong_item, List); // MetadataArrayAdapter is a class extending ArrayAdapter<Data>
// Data is a pojo class
recent.findViewById(R.id.listsong_recent_listview);
Log.d("Debug7*****************","******************");
recent.setAdapter(m_adapter);
MetaAdapter类:
public class MetadataArrayAdapter extends ArrayAdapter<MusicMetadata> {
// declaring our ArrayList of items
private ArrayList<MusicMetadata> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<PojoClass> objects,
* because it is the list of objects we want to display.
*/
public MetadataArrayAdapter(Context context, int textViewResourceId, ArrayList<MusicMetadata> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listsong_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
MusicMetadata i = objects.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView tt = (TextView) v.findViewById(R.id.songTitlerecent);
// check to see if each individual textview is null.
// if not, assign some text!
if (tt != null){
tt.setText("Title :" + i.getAlbum());
}
}
// the view must be returned to our activity
return v;
}
}
我无法看清单。我还有什么要补充的?
参考链接:android using setlistadapter() without extending listactivity
如何在不扩展列表活动的情况下使用setAdapter?
请告诉我有什么问题?
先谢谢
答案 0 :(得分:0)
初始化listview并使用setAdapter将适配器设置为listview
m_adapter = new MetadataArrayAdapter(this, R.layout.recentlistsong_item, RecentSongsList);
ListView recent = (ListView)findViewById(R.id.listsong_recent_listview);
recent.setAdapter(m_adapter);
删除此recent.findViewById(R.id.listsong_recent_listview)
我无法看清单。我还有什么要补充的?
检查适配器代码并查看getView
编辑:
public class MainActivity extends Activity {
MetadataArrayAdapter m_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ListView recent = (ListView)findViewById(R.id.lv);
ArrayList<Data> List = new ArrayList<Data>();
List.add(new Data("test album1")); // Set Name
List.add(new Data("test album2"));
List.add(new Data("test album3"));
m_adapter = new MetadataArrayAdapter(this, R.layout.list_song_item, List);
recent.setAdapter(m_adapter);
}
}
Data.java
public class Data {
String name;
public Data(String name)
{
this.name= name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MetadatArrayAdapter
public class MetadataArrayAdapter extends ArrayAdapter<Data> {
private ArrayList<Data> objects;
LayoutInflater inflater ;
public MetadataArrayAdapter(Context context, int textViewResourceId, ArrayList<Data> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_song_item, null);
holder = new ViewHolder();
holder.tv= (TextView) convertView.findViewById(R.id.songTitlerecent);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Data i = objects.get(position);
if (i != null) {
holder.tv.setText(i.getName());
}
return convertView;
}
static class ViewHolder
{
TextView tv;
}
}
的test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listsong2_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/back_btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Back_Recent_Song" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
list_song_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="@+id/songTitlerecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:padding="10dp"
android:color="#f3f3f3"/>
</LinearLayout>