android-custom-listview
android-custom-listview
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.hmkcode.android;
|
||||
|
||||
public class Item {
|
||||
|
||||
private String title;
|
||||
private String description;
|
||||
|
||||
|
||||
public Item(String title, String description) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.hmkcode.android;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ListActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.ListView;
|
||||
|
||||
public class MainActivity extends ListActivity {
|
||||
|
||||
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
// if extending Activity
|
||||
//setContentView(R.layout.activity_main);
|
||||
|
||||
// 1. pass context and data to the custom adapter
|
||||
MyAdapter adapter = new MyAdapter(this, generateData());
|
||||
|
||||
// if extending Activity 2. Get ListView from activity_main.xml
|
||||
//ListView listView = (ListView) findViewById(R.id.listview);
|
||||
|
||||
// 3. setListAdapter
|
||||
//listView.setAdapter(adapter); if extending Activity
|
||||
setListAdapter(adapter);
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<Item> generateData(){
|
||||
ArrayList<Item> items = new ArrayList<Item>();
|
||||
items.add(new Item("Item 1","First Item on the list"));
|
||||
items.add(new Item("Item 2","Second Item on the list"));
|
||||
items.add(new Item("Item 3","Third Item on the list"));
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.hmkcode.android;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MyAdapter extends ArrayAdapter<Item> {
|
||||
|
||||
private final Context context;
|
||||
private final ArrayList<Item> itemsArrayList;
|
||||
|
||||
public MyAdapter(Context context, ArrayList<Item> itemsArrayList) {
|
||||
|
||||
super(context, R.layout.row, itemsArrayList);
|
||||
|
||||
this.context = context;
|
||||
this.itemsArrayList = itemsArrayList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
|
||||
// 1. Create inflater
|
||||
LayoutInflater inflater = (LayoutInflater) context
|
||||
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
|
||||
// 2. Get rowView from inflater
|
||||
View rowView = inflater.inflate(R.layout.row, parent, false);
|
||||
|
||||
// 3. Get the two text view from the rowView
|
||||
TextView labelView = (TextView) rowView.findViewById(R.id.label);
|
||||
TextView valueView = (TextView) rowView.findViewById(R.id.value);
|
||||
|
||||
// 4. Set the text for textView
|
||||
labelView.setText(itemsArrayList.get(position).getTitle());
|
||||
valueView.setText(itemsArrayList.get(position).getDescription());
|
||||
|
||||
// 5. retrn rowView
|
||||
return rowView;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user