Intialize the listview and use setAdapter to set the adapter to listview
m_adapter = new MetadataArrayAdapter(this, R.layout.recentlistsong_item, RecentSongsList);
ListView recent = (ListView)findViewById(R.id.listsong_recent_listview);
recent.setAdapter(m_adapter);
Remove this recent.findViewById(R.id.listsong_recent_listview)
I am not able to see the list. What else I have to add here?
Check your adapter code and see the code in getView
Edit:
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>