I want a listview displaying what is in to arrays with a customlist however the listview becomes blank when I run the app. Neither the TextView or Image of the list_item.xml is showing.
Here is MainActivity.java
public class MainActivity extends Activity {
ListView list;
List<Integer> users = new ArrayList<Integer>();
List<String> imageId = new ArrayList<String>();
Integer[] userNames;
String[] imageUrls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
users.add(50);
imageId.add("https://color.adobe.com/build2.0.0-buildNo/resource/img/kuler/color_wheel_730.png");
userNames = new Integer[users.size()];
userNames = users.toArray(userNames);
imageUrls = new String[imageId.size()];
imageUrls = imageId.toArray(imageUrls);
CustomList adapter = new
CustomList(MainActivity.this, userNames, imageUrls);
list=(ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
And here is CustomList.java:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final Integer[] userNames;
private final String[] imageUrl;
static class ViewHolderItem {
TextView textViewItem;
}
public CustomList(Activity context,
Integer[] users, String[] imageUrl) {
super(context, R.layout.list_item);
this.context = context;
this.userNames = users;
this.imageUrl = imageUrl;
}
static class ViewHolder{
TextView userName;
ImageView image;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_item, null, true);
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.userName = (TextView)convertView.findViewById(R.id.userName);
holder.image = (ImageView)convertView.findViewById(R.id.img);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.userName.setText(String.valueOf(userNames[position]));
Picasso.with(context).load(imageUrl[position]).resize(1000, 1000).centerInside().into(holder.image);
return convertView;
}
}
Here is list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/userName" android:padding="10dp"/>
<ImageView
android:id="#+id/img"
android:tag="#string/app_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
/>
</TableLayout>
Simply add this line in your CustomList.class
#Override
public int getCount() {
return userNames.length;
}
Also, I noticed that there's no width and height in the TextView.
Related
I use custom adapter for my listView, and I want to make the textView with custom font.
the listView is in fragment:
todolist_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="#+id/checkable_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
todolistfragment.java
public class toDoListFragment extends Fragment {
ListView checkableList;
Button showSelectedButton;
List<itemsModel> itemsList;
public toDoListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todolist, container, false);
checkableList = rootView.findViewById(R.id.checkable_list);
itemsList = new ArrayList<>();
itemsList.add(new itemsModel(false, "item1"));
itemsList.add(new itemsModel(false, "item2"));
itemsList.add(new itemsModel(false, "item3"));
itemsList.add(new itemsModel(false, "item4"));
itemsList.add(new itemsModel(false, "item5"));
itemsList.add(new itemsModel(false, "item6"));
itemsList.add(new itemsModel(false, "item7"));
final customList adapter = new customList(getActivity(), itemsList);
checkableList.setAdapter(adapter);
checkableList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemsModel model = itemsList.get(i);
if(model.isSelected())
model.setSelected(false);
else
model.setSelected(true);
itemsList.set(i, model);
adapter.updateRecords(itemsList);
}
});
return rootView;
}
}
so I created customList.java:
public class customList extends BaseAdapter {
Activity activity;
List<itemsModel> itemsList;
LayoutInflater inflater;
public customList(Activity activity) {
this.activity = activity;
}
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
}
#Override
public int getCount() {
return itemsList.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if(view == null){
view = inflater.inflate(R.layout.list_view_item_row, viewGroup, false);
holder = new ViewHolder();
holder.itemText = view.findViewById(R.id.itemName);
holder.checkBox = view.findViewById(R.id.checkboxIcon);
view.setTag(holder);
}else
holder = (ViewHolder) view.getTag();
itemsModel model = itemsList.get(i);
holder.itemText.setText(model.getItemsList());
if(model.isSelected())
holder.checkBox.setBackgroundResource(R.drawable.checked);
else
holder.checkBox.setBackgroundResource(R.drawable.check);
return view;
}
public void updateRecords(List<itemsModel> itemsList){
this.itemsList = itemsList;
notifyDataSetChanged();
}
class ViewHolder{
TextView itemText;
ImageView checkBox;
}
}
and the layout "list_view_item_row.xml" is the layout with the imageview Checkbox and the textview items:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="5dp" >
<ImageView
android:id="#+id/checkboxIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:background="#drawable/check"/>
<TextView
android:id="#+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/checkboxIcon"
android:layout_toStartOf="#+id/checkboxIcon"
android:padding="10dp"
android:text="Item Name"
android:textColor="#android:color/black"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
/>
I set the custom font manually... like this:
TextView textView;
textView = rootView.findViewById(R.id.text1);
Typeface customFontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Assistant-Bold.ttf");
textView.setTypeface(customFontBold);
where I can set the custom font?
Thank you.
Made changes in class try this
public class customList extends BaseAdapter {
Activity activity;
List<itemsModel> itemsList;
LayoutInflater inflater;
private Typeface customFontBold;
public customList(Activity activity) {
this.activity = activity;
}
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
customFontBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Assistant-Bold.ttf");
}
#Override
public int getCount() {
return itemsList.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
view = inflater.inflate(R.layout.list_view_item_row, viewGroup, false);
holder = new ViewHolder();
holder.itemText = view.findViewById(R.id.itemName);
holder.checkBox = view.findViewById(R.id.checkboxIcon);
holder.itemText.setTypeface(customFontBold);
view.setTag(holder);
} else
holder = (ViewHolder) view.getTag();
itemsModel model = itemsList.get(i);
holder.itemText.setText(model.getItemsList());
if (model.isSelected())
holder.checkBox.setBackgroundResource(R.drawable.checked);
else
holder.checkBox.setBackgroundResource(R.drawable.check);
return view;
}
public void updateRecords(List<itemsModel> itemsList) {
this.itemsList = itemsList;
notifyDataSetChanged();
}
class ViewHolder {
TextView itemText;
ImageView checkBox;
}
}
I used akhilesh0707 answer, but I changed it.
public customList(Activity activity, List<itemsModel> itemsList) {
this.activity = activity;
this.itemsList = itemsList;
inflater = activity.getLayoutInflater();
customFontBold = Typeface.createFromAsset(activity.getApplication().getAssets(), "fonts/Assistant-Bold.ttf");
}
Thanks akhilesh0707.
I have created a custom list view adapter and items for the list view is stored at String.xml. When I run the app I only shows a blank activity.
Please help me to fix this issue.
This is my custom adapter java class.
public class UserAdapter extends ArrayAdapter<User> {
public UserAdapter(Context context, ArrayList<User> users, String[] number) {
super(context, 0, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
User user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_singlerow, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.org_name);
TextView tvNum = (TextView) convertView.findViewById(R.id.cn_num);
// Populate the data into the template view using the data object
tvName.setText(user.company_name);
tvNum.setText(user.contact_num);
return convertView;
}}
List View model class.
public class User {
public String company_name;
public Integer contact_num;
public User(String company_name, Integer contact_num) {
this.company_name = company_name;
this.contact_num = contact_num;
}}
Main Activity class where I attach adapter to the view.
public class activity_one extends Activity {
String[] number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
number = getResources().getStringArray(R.array.dummy_data);
ArrayList<User> arrayOfUsers = new ArrayList<User>();
UserAdapter adapter = new UserAdapter(this, arrayOfUsers,number);
ListView listView = (ListView) findViewById(R.id.listView2);
listView.setAdapter(adapter);
}}
Since i am not 100% sure on what you want to do, ill show you a piece of my code and you can deduce from that what you need.
My DrawerListAdapter is an custom ListView adapter, and every item on that list consists of 3 objects - a String name, String imageurl, String description.
public class DrawerListAdapter extends ArrayAdapter<String> {
private final static int settingsCount = 4; // can ignore this
private Activity context;
// Next 3 lines: All data required for this adapter
private static ArrayList<String> itemname = null;
private static ArrayList<String> imgid;
private static ArrayList<String> itemDescription;
public DrawerListAdapter(Activity context, ArrayList<String> itemname, ArrayList<String> itemDescription, ArrayList<String> imgid) {
// R.layout.drawer_list_item is a layout that represents ONE item
// (not 100% sure, but i think the super class inflates this view itemname.size() times)
super(context, R.layout.drawer_list_item, itemname);
this.context=context;
// saving the required data
DrawerListAdapter.itemDescription = itemDescription;
DrawerListAdapter.itemname=itemname;
DrawerListAdapter.imgid=imgid;
}
#Override
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.drawer_list_item, null,true);
// the views that are present in R.layout.drawer_list_item
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.tvDescription);
txtTitle.setText(itemname.get(position));
extratxt.setText(itemDescription.get(position));
// setting the image here of the list item here
if (position < settingsCount) {
imageView.setImageResource(Integer.parseInt(imgid.get(position)));
} else {
Glide.with(context).load(imgid.get(position)).into(imageView);
}
return rowView;
};
// need to override the getCount. this function just returns the amount of items in your list
#Override
public int getCount(){
if (itemname != null)
return itemname.size();
return 0;
}
This is initialized by:
private void init() {
mDrawerList = (ListView) findViewById(R.id.left_drawer);
if (adapter == null) {
ArrayList<String> itemname = new ArrayList<String>();
itemname.add("item1");
itemname.add("item2");
itemname.add("item3");
itemname.add("item4");
ArrayList<String> imgid = new ArrayList<String>();
imgid.add(R.drawable.pic1 + "");
imgid.add(R.drawable.pic2 + "");
imgid.add(R.drawable.pic4 + "");
imgid.add(R.drawable.pic3 + "");
ArrayList<String> itemDescription = new ArrayList<String>();
itemDescription.add("desc1");
itemDescription.add("desc2");
itemDescription.add("desc3");
itemDescription.add("desc4");
adapter=new DrawerListAdapter(this, itemname, itemDescription, imgid);
setOnItemClickListener(); // sets onClick for each item on the list
}
mDrawerList.setAdapter(adapter);
}
and the drawer_list_item.xml just in case you are intrested:
<?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="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#429bd9" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:maxLines="2"
android:textColor="#79e5a4" />
</LinearLayout>
I am trying to fill my list from database using BaseAdapter. Logcat don't show any error. Here is my code.
In this DatabaseHelper class, I have one table STUDENTS.
DatabaseHelper.java
public long saveData(Student std) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("NAME",std.name);
cv.put("CITY",std.city);
cv.put("AGE",std.age);
long id = db.insert("STUDENTS", null, cv);
db.close();
return id;
}
public ArrayList<Student> getAllStudent() {
ArrayList<Student> studentArrayList = new ArrayList<Student>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.query("STUDENTS",null,null,null,null,null,null);
Log.e("COUNT",""+cursor.getCount());
while (cursor.moveToNext()) {
Student s = new Student();
s.name = cursor.getString(cursor.getColumnIndex("NAME"));
s.city = cursor.getString(cursor.getColumnIndex("CITY"));
s.age = cursor.getString(cursor.getColumnIndex("AGE"));
studentArrayList.add(s);
}
db.close();
return studentArrayList;
}
MyBaseAdapter.java
public class MyBaseAdapter extends BaseAdapter {
ArrayList<Student> myList = new ArrayList<Student>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context,ArrayList<Student> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return myList.size();
}
#Override
public Student getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
myViewHolder viewHolder;
if(convertView == null){
convertView = inflater.inflate(R.layout.single_row, null);
viewHolder = new myViewHolder();
convertView.setTag(viewHolder);
} else {
viewHolder = (myViewHolder) convertView.getTag();
}
viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.cityTextView);
viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.ageTextView);
viewHolder.nameTextView.setText(myList.get(position).name);
viewHolder.cityTextView.setText(myList.get(position).city);
viewHolder.ageTextView.setText(myList.get(position).age);
return convertView;
}
public class myViewHolder {
TextView nameTextView, cityTextView, ageTextView;
}
}
This is my model class
Student.java
public class Student {
String name;
String city;
String age;
}
and in my MainActivity I am trying to fill data into list.
MainActivity.java
public class MainActivity extends ActionBarActivity {
public EditText searchEditText;
public Button addButton;
public ListView studentListView;
public DatabaseHelper dbHelper;
public ArrayList<Student> arrayList;
public MyBaseAdapter myBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchEditText = (EditText)findViewById(R.id.searchEditText);
addButton = (Button)findViewById(R.id.addButton);
studentListView = (ListView) findViewById(R.id.studentListView);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AddNewStudent.class);
startActivity(i);
}
});
dbHelper = new DatabaseHelper(getApplicationContext());
arrayList = dbHelper.getAllStudent();
myBaseAdapter = new MyBaseAdapter(MainActivity.this,arrayList);
studentListView.setAdapter(myBaseAdapter);
myBaseAdapter.notifyDataSetChanged();
}
}
single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:id="#+id/container">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/nameTextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/cityTextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/ageTextView" />
</LinearLayout>
I don't know where I went wrong.
Finally, I have found the answer for this. You just need to add couple of line in getView() method to assign value from list to model class object.
public View getView(int position, View convertView, ViewGroup parent) {
Student std = new Student(); //this line I added
std = myList.get(position); //this line I added
myViewHolder viewHolder;
if(convertView == null){
convertView = inflater.inflate(R.layout.single_row, null);
viewHolder = new myViewHolder();
convertView.setTag(viewHolder);
} else {
viewHolder = (myViewHolder) convertView.getTag();
}
viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.cityTextView);
viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.ageTextView);
viewHolder.nameTextView.setText(myList.get(position).name);
viewHolder.cityTextView.setText(myList.get(position).city);
viewHolder.ageTextView.setText(myList.get(position).age);
return convertView;
}
I'm using drag-sort-listview to sort rows of my ListView, and I have created a demo using this library, but it is not working. When I try to drag rows it is not working.
public class MainActivity extends Activity implements OnItemClickListener {
public static final String[] titles = new String[] { "Strawberry",
"Banana", "Orange", "Mixed" };
public static final String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits" };
public static final Integer[] images = { R.drawable.drag,
R.drawable.drag, R.drawable.drag, R.drawable.drag };
//ListView listView;
DragSortListView listView;
List<RowItem> rowItems;
CustomListViewAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (DragSortListView) findViewById(R.id.listview);
//listView = (ListView) findViewById(R.id.list);
final CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
listView.setDropListener(new DropListener() {
#Override
public void drop(int from, int to) {
if (from != to) {
//DragSortListView list = getListView();
RowItem item = adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
listView.moveCheckState(from, to);
//Log.d("DSLV", "Selected item is " + listView.getCheckedItemPosition());
}
}
});
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(), "Item "
+ (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:dslv="http://schemas.android.com/apk/res/com.example.testingui"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.mobeta.android.dslv.DragSortListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="5dp"
dslv:collapsed_height="2dp"
dslv:drag_enabled="true"
dslv:drag_handle_id="#drawable/drag"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onMove"
dslv:float_alpha="0.6"
dslv:max_drag_scroll_speed="0.5"
dslv:remove_enabled="true"
dslv:remove_mode="flingRemove"
dslv:slide_shuffle_speed="0.3"
dslv:sort_enabled="true"
dslv:track_drag_sort="true"
dslv:use_default_controller="true" />
</RelativeLayout>
CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.descs);
holder.txtTitle = (TextView) convertView.findViewById(R.id.titles);
holder.imageView = (ImageView) convertView.findViewById(R.id.icons);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/icons"
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="#+id/titles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icons"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16dp" />
<TextView
android:id="#+id/descs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/titles"
android:layout_toRightOf="#+id/icons"
android:paddingLeft="10dp"
android:textColor="#3399FF"
android:textSize="14dp" />
</RelativeLayout>
RowItem.java
public class RowItem {
private int imageId;
private String title;
private String desc;
public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return title + "\n" + desc;
}
}
Use Drag handle Id in DragSortListView tag in Xml
dslv:drag_handle_id="#id/drag_handle"
Make an id.xml file and put drag handle id as above to make it common and use the same id on dragable handle image view in your row item.
Hope it helps.
I'm using a paginator with a few Fragments.
I want to show a message when there are no entries in the list (Fragment).
I tried to use Textview with android:id="#id/android:empty but it doesn't work.
Custom list layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_medium" >
<TextView
android:id="#+id/label_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/padding_medium"
android:text="#string/label_value"
android:textColor="#android:color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/label_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/padding_medium"
android:text="#string/title_time"
android:textColor="#android:color/white"
android:textSize="16sp" />
</RelativeLayout>
List adapter class:
public class JournalAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
private final List<String> dates;
public JournalAdapter(Context context, List<String> values,
List<String> dates) {
super(context, R.layout.layout_journal_list, values);
this.context = context;
this.values = values;
this.dates = dates;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.layout_journal_list, parent, false);
TextView value = (TextView) rowView.findViewById(R.id.label_glucose);
TextView date = (TextView) rowView.findViewById(R.id.label_date);
value.setText(values.get(position));
date.setText(dates.get(position));
return rowView;
}
}
List parsing method:
private void initListView() {
values = dataSource.getAllValues();
List<String> values = new ArrayList<String>();
List<String> dates = new ArrayList<String>();
for (Value value : values) {
values.add(Double.toString(value.getValue()));
dates.add(secondsToDate(value.getDate()));
}
setListAdapter(new JournalAdapter(context, values, dates));
listView = getListView();
listView.setEmptyView(noItems("NO ITEMS"));
listView.setOnItemClickListener(this);
}
noItems method:
private TextView noItems(String text) {
TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
emptyView.setText(text);
emptyView.setTextColor(Color.WHITE);
emptyView.setTextSize(20);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
return emptyView;
}
This worked for me (got the solution from this question asked by William Remacle). What you might have missed is adding the created TextView to the ListView layout parent (second line of code from the bottom of the noItems method below):
In my ListFragment I used the following method to get a view for the empty view:
private TextView noItems(String text) {
TextView emptyView = new TextView(getActivity());
//Make sure you import android.widget.LinearLayout.LayoutParams;
emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//Instead of passing resource id here I passed resolved color
//That is, getResources().getColor((R.color.gray_dark))
emptyView.setTextColor(getResources().getColor(R.color.gray_dark));
emptyView.setText(text);
emptyView.setTextSize(12);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
//Add the view to the list view. This might be what you are missing
((ViewGroup) getListView().getParent()).addView(emptyView);
return emptyView;
}
Then in the onStart() method of the the ListFragment set the the empty view:
#Override
public void onStart() {
super.onStart();
getListView().setEmptyView(
noItems(getResources().getString(R.string.widget_empty)));
}
android:id="#android:id/empty"
and not
android:id="#id/android:empty"