i have an activtiy that represents an ingredients list creation and searching a recipe according to the list created,what i want to do is get the text from the eduttext after the user inserts text and presses add,and then for the added text to appear in the list view,for some reason the text dosen't change and i have no idea why. can anyone find my mistake?
xml of a single item of the list view:
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/ingredientItemTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="62dp"
android:layout_marginTop="37dp"
android:textColor="@color/black"
android:layout_toLeftOf="@+id/removeButton"
/>
<Button
android:id="@+id/removeButton"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignBaseline="@+id/ingredientItemTv"
android:layout_alignBottom="@+id/ingredientItemTv"
android:layout_alignParentRight="true"
android:background="@drawable/remove_icon"
android:text="@string/remove" />
</RelativeLayout>
xml of the activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg3"
android:orientation="vertical" >
<TextView
android:id="@+id/fridgeReipeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:textColor="@color/black"
android:text="@string/fridgeReipeTitle" />
<EditText
android:id="@+id/ingredientEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fridgeReipeTv"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:ems="10"
/>
<Button
android:id="@+id/ingAddBtn"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_below="@+id/ingredientEt"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:background="@drawable/aqua_button"
android:text="@string/addString" />
<TextView
android:id="@+id/recipeFindTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/findRecipe"
android:textColor="@color/black" />
<ListView
android:id="@+id/lIngredientsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/recFindBtn"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ingAddBtn"
android:layout_marginBottom="79dp" >
</ListView>
<Button
android:id="@+id/recFindBtn"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/fridgeReipeTv"
android:background="@drawable/find_icon"
android:text="@string/find" />
</RelativeLayout>
the java of the activity :
package com.androidbegin.parselogintutorial;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class FridgeRecipe extends Activity
{
ListView ingsListView;
EditText ingredientEt;
TextView mainTitleTv,endTileTv;
Button addingBtn, searchBtn,removeBtn;
ArrayList<IngrdientView> ingredients;
int idInList = 0;
IngredientsListAdapter ila;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fridge_recipe);
ingsListView = (ListView)findViewById(R.id.lIngredientsListView);
ingredientEt = (EditText)findViewById(R.id.ingredientEt);
mainTitleTv = (TextView)findViewById(R.id.fridgeReipeTv);
endTileTv = (TextView)findViewById(R.id.recipeFindTv);
addingBtn = (Button)findViewById(R.id.ingAddBtn);
searchBtn = (Button)findViewById(R.id.recFindBtn);
removeBtn = (Button)findViewById(R.id.removeButton);
ingredients = new ArrayList<IngrdientView>();
ila = new IngredientsListAdapter(FridgeRecipe.this, ingredients);
ingsListView.setAdapter(ila);
final String ingredient = ingredientEt.getText().toString();
addingBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
idInList++;
ingredients.add(new IngrdientView(ingredient,idInList));
ingredientEt.setText("");
ila.notifyDataSetChanged();
}
});
ingsListView.setOnItemClickListener(mMessageClickedHandler);
}
// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
if(v.getId() == R.id.removeButton)
{
ingredients.remove(position);
}
}
};
}
the java of the adapter :
package com.androidbegin.parselogintutorial;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
public class IngredientsListAdapter extends ArrayAdapter<IngrdientView> implements ListAdapter, View.OnClickListener
{
Context context;
ArrayList<IngrdientView> ingredients;
public IngredientsListAdapter(Context context,ArrayList<IngrdientView> ingredients)
{
super(context, 0);
this.context = context;
this.ingredients = ingredients;
}
@Override
public void registerDataSetObserver(DataSetObserver observer)
{
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer)
{
// TODO Auto-generated method stub
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return ingredients.size();
}
@Override
public IngrdientView getItem(int position)
{
return ingredients.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds()
{
// TODO Auto-generated method stub
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View root;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.item_view, null);
}
else
{
root = convertView;
TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);
Button removeBtn = (Button)root.findViewById(R.id.removeButton);
IngrdientView ingView = ingredients.get(position);
//ingredientName.setText(ingredientName.getText().toString());
ingView.setIngredientName(ingredients.get(position).toString());
removeBtn.setOnClickListener(this);
}
return root;
}
@Override
public int getItemViewType(int position)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount()
{
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean isEmpty()
{
// TODO Auto-generated method stub
return ingredients.size() == 0;
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
@Override
public boolean areAllItemsEnabled()
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position)
{
// TODO Auto-generated method stub
return false;
}
}
One thing I can see which is wrong in your code is that you don't do anything to newly created views. Change your code to this and see if it changes anything:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View root;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.item_view, null);
}
else
{
root = convertView;
}
TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);
Button removeBtn = (Button)root.findViewById(R.id.removeButton);
IngrdientView ingView = ingredients.get(position);
//ingredientName.setText(ingredientName.getText().toString());
ingView.setIngredientName(ingredients.get(position).toString());
removeBtn.setOnClickListener(this);
return root;
}
You're not doing anything with your newly created view; you will need to populate it. On another note, you shouldn't re-subscribe the listener on cached views (ie after your else). You need to do it the first and only time you create your view (so in the if(convertView == null)
otherwise you'll end up getting multiple call backs (at least in other programming languages you'd do).
I would also suggest using what android calls a "holder pattern" (Google it) for your list views to allow smooth scrolling and again, be careful with subscriptions.
I would also suggest making sure your getView() is called when you add a new item. From the brief overview of your code, it does seem to be the case but just to make sure, put in a break point and make sure it's called.
Check out this developer guide for more examples and further reading.
Also it's you've and not youv'e :)
Add the text into your Collection list (Arraylist or array). Modify the collection in the list view adapter (listview_adapter.setCollection (Your collection)). Then call listview_adapter.notifyDataSetChanged();
Or
Reinitialize the listview_adapter with new Collection list. and add the adapter into the listview.
the changes that i made for it to work were in the adapter :
first change:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View root;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.item_view, null);
}
else
{
root = convertView;
TextView ingredientName = (TextView)root.findViewById(R.id.ingredientItemTv);
ingredientName.setText(ingredients.get(position).getIngredientName()+"");
Button removeBtn = (Button)root.findViewById(R.id.removeButton);
removeBtn.setTag(position);
//IngrdientView ingView = ingredients.get(position);
removeBtn.setOnClickListener(this);
}
return root;
}
second change:
@Override
public void onClick(View v)
{
int index = (Integer)v.getTag();
IngrdientView data = ingredients.get(index);
ingredients.remove(data);
// ((AdapterView)adapterView).setAdapter(null);
// ((AdapterView)adapterView).setAdapter(this);
this.notifyDataSetChanged();
}
hope this helps someone sometime..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.