I would like to load only selected fields from my ArrayList to ListView.
I couldn't find that example so I ask.
I have an ArrayList of a structure as follows:
ArrayList<LogInfo> logInfoArray
Where LogInfo class have fields as follows:
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times; //contains fields of calendar class
I want to put in my ListView in each row selected fields from "times" and "codes"
How can I achieve that? I would like to use a cursor, if possible.
You can use a custom adapter extending from ArrayAdapter to which you can pass the ArrayList<LogInfo> .
You can then ovverride the getView(..) method of the Adapter to set the fields you want in the row of the Listview .
UPDATE
From this example at Android Custom Adapters
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class InteractiveArrayAdapter extends ArrayAdapter<LogInfo> {
private final List<LogInfo> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<LogInfo> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text1, text2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) view.findViewById(R.id.label1);
viewHolder.text2 = (TextView) view.findViewById(R.id.label2);
view.setTag(viewHolder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text1.setText(list.get(position).getName1());
holder.text2.setText(list.get(position).getName2());
return view;
}
}
Related
I'm trying to pass a String list from my custom array class to another class, but for some reason when I try to access the list, it always seems empty.
Here is my Custom Adapter class:
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends ArrayAdapter {
private final List<Model> orders;
private final Context context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public List<String> selectedItems = new ArrayList<>();
public CustomAdapter(#NonNull Context context, List<Model> orders) {
super(context, R.layout.company_list_order, orders);
this.context = context;
this.orders = orders;
}
static class ViewHolder {
protected TextView orderText;
protected CheckBox checkOrder;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
selectedItems = new ArrayList<>();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.company_list_order, parent, false);
viewHolder = new ViewHolder();
viewHolder.orderText = (TextView) convertView.findViewById(R.id.textViewList);
viewHolder.checkOrder = (CheckBox) convertView.findViewById(R.id.listCheckBox);
viewHolder.checkOrder.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
orders.get(getPosition).setSelected(buttonView.isChecked());
if (orders.get(getPosition).isSelected()) {
selectedItems.add(orders.get(getPosition).getName());
Log.d("Add", "Item added: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
} else {
selectedItems.remove(orders.get(getPosition).getName());
Log.d("Remove", "Item removed: " + orders.get(getPosition).getName());
Log.d("Size", "Selected items " + selectedItems.size());
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textViewList, viewHolder.orderText);
convertView.setTag(R.id.listCheckBox, viewHolder.checkOrder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkOrder.setTag(position);
viewHolder.orderText.setText(orders.get(position).getName());
viewHolder.checkOrder.setChecked(orders.get(position).isSelected());
return convertView;
}
public List<String> getListItems() {
return selectedItems;
}
}
At the bottom I have the method returning the list. How should I access this in another class?
Probably because you create a new ArrayList everytime getView() is called, which is called a lot. And when you call the method getListItems() the method getView() is called before due to scrolling the listview or something. Remove the line
selectedItems = new ArrayList<>();
from getView() method.
Another option is to make a class with static variables to hold the value during the applications lifetime
public class GlobalVars {
public static List<String> selectedItems;
}
I am trying to retrieve an image from an SQLite database. The image is stored as a BLOB and trying to retrieve it using an array. I'm not sure why its doing this. The permissions are all set correctly.
package com.example.joao_.quizathonegroupteamproject.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.joao_.quizathonegroupteamproject.DatabaseClasses.User;
import com.example.joao_.quizathonegroupteamproject.R;
import java.util.ArrayList;
/**
* Created by Quoc Nguyen on 13-Dec-16.
*/
public class UserListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<User> foodsList;
public UserListAdapter(Context context, int layout, ArrayList<User> foodsList) {
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
}
#Override
public int getCount() {
return foodsList.size();
}
#Override
public Object getItem(int position) {
return foodsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
User food = foodsList.get(position);
byte[] tblUsersImage = food.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(tblUsersImage, 0, tblUsersImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
You can retrieve BLOB image and stored it in Byte array like this:
byte[] array = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0 ,array.length);
and then set it into image view
holder.imageView.setImageBitmap(bitmap);
you didn't provide your SQLite database class but i think , your food.getImage(); return null value .
check dataInsert method OR getData in your SQLite db !
I created a simple android application with a ListView in it. I have my custom list adapter below:
package com.example.android.efaas;
import java.util.List;
import com.example.android.R;
import com.example.android.efaas.bean.ListItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
List<ListItem> data;
public CustomListAdapter(Activity activity, List<ListItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.list_menu, null);
ImageView img = (ImageView) rowView.findViewById(R.id.list_menu_image);
TextView title = (TextView) rowView.findViewById(R.id.list_menu_title);
ListItem item = data.get(pos);
img.setImageResource(item.getId());
title.setText(item.getTitle());
return rowView;
}
public ListItem getListItem(int pos){
return data.get(pos);
}
}
I have a public method getListItem, I tried to call it during ListView onClick but somehow I cant seem to make it work. Here's the image of the problem:
How do I resolve this?
You should cast adapter.getAdapter() to CustomListAdapter class then
the compile time error is due of the fact that adapter is not your CustomListAdapter. What you can do is cast the return value of adapter.getAdapter() to CustomListAdapter. Conversely you should use what's already available to retrieve the object at position. Like getItemAtPosition(int)
I want to let the adapter check if the submitted value is a file
but when I check
it only checks the first value so
for eg.
I submit
app (directory)
meta (directory)
this shoul look like this :
app with the directory icon
meta with the directory icon
but the out put is only app as directory
The adapter:
package org.alexander.fuchs.compress;
import java.io.File;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class adapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public adapter(Activity context, String[] names) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.label);
viewHolder.image = (ImageView) rowView
.findViewById(R.id.icon);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
File entry = new File(s);
if(entry.isDirectory() == true)
{
holder.image.setImageResource(R.drawable.ok);
}
else
{
holder.image.setImageResource(R.drawable.no);
}
return rowView;
}
}
I get duplicated items in a ListView. Scrolling back and down sometimes changes the item order.
I googled and found many threads reporting this bug, but none of them helped me in fixing my issue.
Here is my code:
Activity:
package com.github.progval.SeenDroid;
import java.util.ArrayList;
import java.util.List;
import com.github.progval.SeenDroid.lib.Connection;
import com.github.progval.SeenDroid.lib.Message;
import com.github.progval.SeenDroid.lib.MessageFetcher;
import com.github.progval.SeenDroid.lib.Query.ParserException;
import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class ShowUserActivity extends ListActivity {
private Connection connection;
public ArrayList<Message> listMessages = new ArrayList<Message>();
public MessageAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
this.connection = new Connection();
this.setTitle(R.string.homefeed_title);
this.listMessages = new MessageFetcher(this.connection).fetchUser();
this.bindUi();
}
private void bindUi() {
this.adapter = new MessageAdapter(this, this.listMessages);
this.setListAdapter(adapter);
// TODO Bind buttons
}
}
MessageAdapter:
package com.github.progval.SeenDroid;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
import com.github.progval.SeenDroid.lib.Message;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MessageAdapter extends BaseAdapter {
private Context context;
private List<Message> items = new ArrayList<Message>();
private int lastPosition = 0;
public MessageAdapter(Context context, List<Message> items) {
super();
this.context = context;
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LinearLayout view;
view = (LinearLayout) LinearLayout.inflate(this.context, R.layout.message, null);
Log.d("SeenDroid", String.format("Get view %d", position));
TextView title = new TextView(view.getContext());
title.setText(this.items.get(position).getTitle());
view.addView(title);
return view;
} else {
return convertView;
}
}
#Override
public int getCount() {
return this.items.size();
}
#Override
public Object getItem(int location) {
return this.items.get(location);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
}
By the way, the output is:
D/SeenDroid(30939): Get view 0
D/SeenDroid(30939): Get view 1
D/SeenDroid(30939): Get view 2
D/SeenDroid(30939): Get view 3
D/SeenDroid(30939): Get view 4
D/SeenDroid(30939): Get view 5
D/SeenDroid(30939): Get view 6
D/SeenDroid(30939): Get view 7
D/SeenDroid(30939): Get view 8
D/SeenDroid(30939): Get view 0
D/SeenDroid(30939): Get view 16
Regards,
ProgVal
Try this:
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LinearLayout view = (LinearLayout) LinearLayout.inflate(this.context,
R.layout.message, null);
Log.d("SeenDroid", String.format("Get view %d", position));
TextView title = new TextView(view.getContext());
title.setText(this.items.get(position).getTitle());
view.addView(title);
return view;
} else {
LinearLayout view = (LinearLayout) convertView;
TextView title = (TextView) view.getChildAt(0);
title.setText(this.items.get(position).getTitle());
return convertView;
}
}
Explanation: you got the duplicates because lists on Android reuse UI objects. You are expected to reuse convertView rather than create a new one if it is not null. Of course, you are responsible to set an appropriate value to the instance being reused. Otherwise the value is left from the last "usage".
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
} else {
gridView = (View) convertView;
}
gridView = inflater.inflate(R.layout.worker_listmain, null);
// your source code here!!! Run 100%
// I got this problem also, I found out the way to solve it!
// Please use my source code :D SIMPLE is PERFECT :D
return gridView;
}
ListView does not guarantee uniqueness of items you are added there. It is your responsibility. You are using ArrayList to store items and it can store as many duplicate items as you want.
If you want to removed duplicates put your items into set and then into list again:
listMessages = new ArrayList<Messages>(new LinkedHashSet<Message>(listMessages))
The LinkedHashSet will remove duplicates preserving the initial order of items, ArrayList will allow access elements by position.
A suggestion - add a log statement after title.setText and write the value obtained from getTitle(). You might get to know why you are getting duplicate entries.
You should use the ViewHolder paradigma in your ListAdapter
a nice example can be found here:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html