Add and remove custom item to listview - java

Currently I'm adding a custom row (contains and edit text and a plus and minus for adding/removing a row) to a list view. However, each time a row is added the contents from all edit texts are wiped.
public class ingredientView extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public ingredientView(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.ingredient_item, null);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, loadedFood);
//Handle TextView and display string from your list
InstantAutoComplete listItemText = (InstantAutoComplete) view.findViewById(R.id.recipe_ingredients);
listItemText.setAdapter(adapter);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.ingredient_minus_button);
ImageButton addBtn = (ImageButton)view.findViewById(R.id.ingredient_plus_button);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
if (list.size()>1){
list.remove(position); //or some other task
notifyDataSetChanged();}
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.add(position+1,"");
notifyDataSetChanged();
}
});
return view;
}
private static final String[] loadedFood = new String[]{
"eggs","milk","butter","flour"
};
}

Related

How to add new item on button press in Base Adapter

I'm trying to crate something that creates a new ListView item on button press, I thing I have somewhere some bug, but as I'm new to this topic I don't know where.
I've tried rewriting the code several times, I've tried to use notifyDataSetChanged(); - it does nothing
tried googling looking on other topics here...
Here is my MainActivity.java:
public Button btn;
private ListView lv;
private CustomeAdapter customeAdapter;
public ArrayList<EditModel> editModelArrayList;
int populateListMaxNum =3;
int listNumber = populateListMaxNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
btn = (Button) findViewById(R.id.btn);
editModelArrayList = populateList();
customeAdapter = new CustomeAdapter(this,editModelArrayList);
lv.setAdapter(customeAdapter);
/* TODO activate button */
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addToList();
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<EditModel> populateList(){ //this part works perfectly
ArrayList<EditModel> list = new ArrayList<>();
for(int i = 0; i < populateListMaxNum; i++){
EditModel editModel = new EditModel();
//editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
}
return list;
}
/*TODO make it work = expand */
private void addToList(){ // this part doesn't work nor it doesn't fail
EditModel editModel = new EditModel();
editModelArrayList.add(editModel);
customeAdapter.notifyDataSetChanged();
}
}
Here is my CustomeAdapter.java class:
public class CustomeAdapter extends BaseAdapter {
private Context context;
public static ArrayList<EditModel> editModelArrayList;
public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) {
this.context = context;
CustomeAdapter.editModelArrayList = editModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return editModelArrayList.size();
}
#Override
public Object getItem(int position) {
return editModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.editText = convertView.findViewById(R.id.editid);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
return convertView;
}
private class ViewHolder {
protected EditText editText;
}
}
I expect to create a new list item (EditText + TextView) but nothing happens (except the Toast message)
(After some tweaks the app crashes due to arrayindexoutofboundsexception length=3 index=3 error, but not in this setting)
here are up to all files nessessary: https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965
Main linear layout in lv_item.xml has match_parent height. Try change it to wrap_content. It seems like one row is just taking whole screen.

How to change the background color of a list item at click on a button?

I have a ListView and a CustomAdapter. The elements are all successfully loaded into the list. Now I want to change the background color of a certain element of the list by clicking on an external button. But I do not know how to access a specific item in the list.
Here is the CustomAdapter class:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
}
You can do it like this inside your getView() method
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
}
});
If you have a button on your view then performs the listener on that button
If you want to get your selected item view from your parent activity then :
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
selectedposition = position ;
}
});
View view = listView.getAdapter().getView(selectedposition,null,listview);
Then change its background:
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
please define your color in your color.xml file
If you have more than one view then , create an ArrayList<View> and do some loop
create a custom listener interface in your activity and your
adapter will implement this.
public interface OnClickListenerFromActivity {
void onActivityButtonClick(int position);
}
on click in your button call your listener's method
mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));
implement this listener into your adapter
public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
public void onActivityButtonClick(int position) {
// get your item through position and
// set your color here
}
}

Arraylist reading the last element as the second last element

I have an Adapter Class that inflates items for a gridview. I am using an ArrayList to store certain details of the gridview item that is clicked. The problem that I am facing is that the last element is not recognised separately. If I click on the last element ,it is giving information about the element above it. Rest all other elements are fine.
public class MyAdaptertwo extends BaseAdapter
{
public List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
int j;
public static List<list> rowitems = new ArrayList<list>();
static int size=0;
public MyAdaptertwo(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("S", R.drawable.s,"1",R.drawable.v));
items.add(new Item("F", R.drawable.t,"2",R.drawable.v));
}
#Override
public int getCount()
{
return items.size();
}
#Override
public Object getItem(int i)
{
j=i;
return items.get(i);
}
public void setCost(int i,String a)
{
items.get(i).cost=a;
}
#Override
public long getItemId(int i)
{
return items.get(i).colorId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
ImageView vn;
TextView name;
TextView costs;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.vnon, v.findViewById(R.id.vnon));
v.setTag(R.id.text, v.findViewById(R.id.text));
v.setTag(R.id.cost,v.findViewById(R.id.cost));
}
picture = (ImageView)v.getTag(R.id.picture);
vn = (ImageView)v.getTag(R.id.vnon);
name = (TextView)v.getTag(R.id.text);
costs = (TextView)v.getTag(R.id.cost);
Item item = (Item)getItem(i);
picture.setImageResource(item.colorId);
name.setText(item.name);
costs.setText(item.cost);
vn.setImageResource(item.vnId);
Button add=(Button)v.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
list item = new list(items.get(j - 1).colorId, items.get(j - 1).name, Integer.toString(co[j-1]), Integer.toString(no[j - 1]));
rowitems.add(item);
//something
}
});
Button sub=(Button)v.findViewById(R.id.sub);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
//something
}
});
return v;
}
private class Item
{
final String name;
final int colorId;
String cost;
final int vnId;
Item(String name, int drawableId,String cost,int vnId)
{
this.name = name;
this.colorId = drawableId;
this.cost=cost;
this.vnId=vnId;
}
}
}
Use setTag and getTag method of View class, To get click Item object in onClick method.
Item item = (Item)getItem(i);
Button add=(Button)v.findViewById(R.id.add);
add.setTag(i);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (int)v.getTag();
//do something with item object
Item item = items.get(position);
list listItem = new list(item.colorId, item.name, Integer.toString(co[position]), Integer.toString(no[position]));
rowitems.add(listItem);
}
});

Android Refresh ListActivity from BaseAdapter

I have a ListActivity and a custom adapter that extends BaseAdapter.
On each row of the ListView, there is a button and a few more views. onClickListener is declared in the adapter.
How can I refresh list view after button is clicked ?
Here is the code
List Activity:
.........
super.onCreate(savedInstanceState);
here im reading content from a file and then creating adapter with that content
adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
..........
Adapter:
.......
public class UvArrayAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater linflater;
private TextView txt_1, txt_2, txt_3;
private ProgressBar pr1;
private String[] str1;
private String[] str2;
private String[] str3;
private Integer[] int1;
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
#Override
public void onClick(View arg0) {
Log.v("TAG", "onItemClick at position" + mPosition);
}
}
public UvArrayAdapter(Context context, String[] s1, String[] s2, Integer[] i1) {
mContext = context;
str1 = s1;
str2 = s2;
int1 = i1;
linflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return str1.length;
}
#Override
public Object getItem(int arg0) {
return str1[arg0];
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = linflater.inflate(R.layout.uv_rowlayout, null);
}
convertView.setOnClickListener(new OnItemClickListener(position));
txt_1 = (TextView) convertView.findViewById(R.id.textView1);
pr1 = (ProgressBar) convertView.findViewById(R.id.progressBar1);
pr1.setProgress(int1[position]);
txt_1.setText(str1[position]);
txt_2 = (TextView) convertView.findViewById(R.id.textView2);
txt_2.setText(str2[position]+"mV");
Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.v("onClickListener", "button - clicked at position " position);
do some changes in mentioned file and after that i need to update the list view with new data
}});
return convertView;
}
}
adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated
Note: If you get any errors with the above line, you could try the following code (where mListView is the name of my ListView object)
((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged();
Another way would be to invalidate the List so that it is redrawn form the updated data set once again.
mListView=(ListView) findViewById(R.id.MyListView);
mListView.invalidate();
Use notifyDataSetChanged()
adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray);
setListAdapter(adapter);
UvArrayAdapter.notifyDataSetChanged();
// To make the adapter aware of the changes

Adding a new row on button click to a list view

I want to add a new row on clicking add button in a list view in android.
Can someone help me out?
Here is the code I have so far:
public class prsnlhstry<EventArgs> extends Activity implements OnItemClickListener {
public static ArrayList<String> arr_sort_textview1= null;
public static ArrayList<String> arr_sort_textview2=null;
public static ArrayList<String> arr_sort_textview3=null;
public Resources ApptResources= null;
private ContextWrapper mycontext;
tododata todo;
int clickcounter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
todo = new tododata(this);
List<listObject> List = new ArrayList<listObject>();
for(int i = 0; i <= 5; i++){
listObject lo = new listObject();
lo.grade = Integer.toString(i);
lo.reason = Integer.toString(i);
lo.school = Integer.toString(i);
List.add(lo);
}
CustomAdapter ca = new CustomAdapter(this);
lv = (ListView)findViewById(R.id.listview);
lv.setAdapter(ca);
lv.setOnItemClickListener(this);
ca.notifyDataSetChanged();
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
}
private class listObject {
String grade;
String school;
String reason;
}
public class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return todo.getLength();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String temp = null;
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
// Creates a ViewHolder and store references to the views
// we want to bind data to.
holder = new ViewHolder();
holder.textview1 = (TextView) convertView.findViewById(R.id.item2);
holder.textview2 = (TextView) convertView.findViewById(R.id.item3);
holder.textview3 = (TextView) convertView.findViewById(R.id.item4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview1.setText(todo.getgrade(position));
holder.textview2.setText(todo.getschool(position));
holder.textview3.setText(todo.getreason(position));
return convertView;
}
}
static class ViewHolder {
TextView textview1;
TextView textview2;
TextView textview3;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
Just add the item to your list and refresh the adapter, nothing special.
You need to increment your "tododata" data structure on button click and then call to notifyDataSetChanged() method:
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
todo.addNewRow();
ca.notifyDataSetChanged();
Toast.makeText(prsnlhstry.this, "Row Added Successfully", Toast.LENGTH_SHORT).show();
}
});
To compile the above code you will need to define "ca" variable as the final one, or make it class member.
I hope it helps.

Categories

Resources