Change GridView images on item click at run time in android - java

i am using GridView in application. and i wants that when i click on item it should replace on another array picture. actually i used two array when application run first time, the GridView will show the first array picture , after that when i click on item it should replace by the second array picture according to its position.
i did try but did't solve. so any one help me..
GridViewContent.java
public class GridViewContent extends BaseAdapter {
private Context context;
public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};
public GridViewContent(Context c){
context=c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
return myimage;
}
}
MainActivity.java
public class MainActivity extends Activity {
Context ctx;
GridViewContent grid1 = new GridViewContent(ctx);
// GridView grid;
/*
* static final int[] numbers = new int[] {
* R.drawable.question,R.drawable.question
* ,R.drawable.question,R.drawable.question
* ,R.drawable.question,R.drawable.question, R.drawable.question,
*
* };
*/
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
// grid.setOnItemClickListener(itemClickListener);
grid.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
ImageView pictImage = new ImageView(ctx);
int imgView = OriginalArray[id];
pictImage.setImageResource(imgView);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gv_memory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
thanks in advance....

You need to do some little changes as below:
public class GridViewContent extends BaseAdapter {
private Context context;
public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };
public GridViewContent(Context c){
context=c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
myimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myimage.setImageResources(Original[arg2]);
}
});
return myimage;
}
}

Implement onitemclicklistener for gridview.Refer this site http://www.mkyong.com/android/android-gridview-example/. write your changing logic inside this and call adapter.notifydatasetChanged(). http://stackoverflow.com/questions/6201884/how-can-i-change-image-on-gridview-runtime

Related

Need help in Multiplying the edit text value with a text view value from a custom list view in android

I have created a custom list view with a image view, 2 text view,one edit text one is for displaying brand names a string value another for displaying loyalty points, edit text for entering the quantity by the user which will be multiplied with loyalty point from text view.I have tried to multiplying the user entered value in each list item (from edit text)with the text view loyalty points value,I have implemented my own logic for multiplying it but it is not working, I have posted my code here for your reference correct me if any thing wrong in the code
MainActivity.java
public class MainActivity extends Activity {
ListView lv;
Context context;
static TextView ttv;
ArrayList prgmName;
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
public static String [] prgmNameList={"Britania","Rin","Goodrej","ITC","Nestle","Wheel","Ariel","Tide"};
public static String [] loyal_pts={"10","5","15","25","20","18","30","32"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
ttv= (TextView) findViewById(R.id.tot_point);
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages,loyal_pts));
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
String [] result;
String [] Pts;
Context context;
int [] imageId;
int total_points;
String con_pts;
int lyl_pts;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages, String[] loyal_pts) {
// TODO Auto-generated constructor stub
result=prgmNameList;
Pts=loyal_pts;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
TextView tv1;
ImageView img;
EditText etxt;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
// final int total_points,con_pts,lyl_pts;
rowView = inflater.inflate(R.layout.item_list, null);
try{
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.etxt= (EditText) rowView.findViewById(R.id.edit_txt);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.tv1= (TextView) rowView.findViewById(R.id.textView2);
holder.tv.setText(result[position]);
holder.tv1.setText(" " + Pts[position] + "pts");
holder.img.setImageResource(imageId[position]);
System.out.println("total points"+total_points);
holder.etxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
lyl_pts = Integer.parseInt(String.valueOf(Pts[position]));
con_pts = holder.etxt.getText().toString().trim();
total_points = lyl_pts * Integer.parseInt(con_pts);
Toast.makeText(context,total_points,LENGTH_LONG).show();
System.out.println("heyy"+total_points);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
catch(Exception e){
e.printStackTrace();
}
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Toast.makeText(context, "You Clicked " + result[position], LENGTH_LONG).show();
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
});
return rowView;
}
}
getView() is called whenever a new item is displayed on screen. at the initial phase nothing will be in your EditText so that you are not getting expected result from this line in getView() method.
con_pts=holder.etxt.getText().toString().trim();
I suggest either putting some initial value at etxt Or moving the logic in afterTextChanged like here
#Override
public void afterTextChanged(Editable s) {
lyl_pts=Integer.parseInt(String.valueOf(Pts[position]));
con_pts=holder.etxt.getText().toString().trim();
total_points = lyl_pts * Integer.parseInt(con_pts);
System.out.println("total points"+total_points);
}

click to start new activity in grid view

I have grid view in my application. on click of each image in gridview new activity should start.
now when I click on them, a toast appears.
how I manage setOnClicklistener in my adaptor I can't have startactivity.
enter code here:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MyActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
and here is myactivity:
public class MyActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Add onItemClickListener on GridView
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position==0){
Intent iinent= new Intent(MyActivity.this,secondactivity.class);
startActivity(iinent);
}
}
});

Hide items in GridView if two items have same images using Android

i am developing a project using GridView where i need to hide items if a user click such two items having same images then both item should be hide.
First time when project will load it will have one Question Marks image on all item.
when user click item the background image will show then if he click another item having the same picture then both item should be hide if not, it again show the Question Marks image.
How this is possible?
here is my MainActivity.java
public class MainActivity extends Activity {
Context ctx;
int imagesArray[];
GridViewContent adapter;
List<Integer> pictures;
boolean flage=false;
int img1=-1,img2=-1;
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
shuffleArray();
adapter= new GridViewContent(this);
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
}
private void shuffleArray() {
// TODO Auto-generated method stub
pictures= new ArrayList<Integer>();
for (int index = 0; index < OriginalArray.length; index++)
{
pictures.add(OriginalArray[index]);
}
Collections.shuffle(pictures);
}
public class GridViewContent extends BaseAdapter {
private Context context;
//abstract change();
public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};
public GridViewContent(Context c){
context=c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
convertView=LayoutInflater.from(context).inflate(R.layout.main, null);
final ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
//myimage.setImageResource(pictures.get(pictureArray[position]));
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
myimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// int post1,post2;
myimage.setImageResource(pictures.get(position));
if(flage==false)
{
img1=pictures.get(position);
flage=true;
}else if(flage==true){
//post2=position;
img2=pictures.get(position);
checkResult();
//notifyDataSetChanged();
flage=false;
}
//else if(f)
}
});
return myimage;
}
}
public void checkResult() {
if(img1==img2)
{
//pictures.remove(post2);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show();
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
}
}
}
You could try and remember the index of a GridView item you clicked. When a second click occurs, check the remembered index. If it is populated (meaning the user already clicked on an arbitrary image once), extract both drawables from the adapter and check if they are equal. If they are, remove them from the adapter and update it. Do not forget to reset the remembered index.
In code, this might look something like this:
int firstClickIndex = -1;
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
if (-1 == firstClickIndex) {
firstClickIndex = position;
} else {
final int fstDrawable = adapter.get(firstClickIndex);
final int sndDrawable = adapter.get(position);
if (fstDrawable == sndDrawable) {
// Remove stuff, then reset firstClickIndex.
} else {
// Reset firstClickIndex.
}
}
}
});

how to store value of checkboxes when creating a custom listView android

public class AppleSupportList extends BaseAdapter {
private LayoutInflater mInflater;
private final String[] values;
public AppleSupportList(Context context, String[] values) {
mInflater = LayoutInflater.from(context);
this.values = values;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customlist1, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.labelContact1);
holder.img = (ImageView) convertView.findViewById(R.id.imageContact1);
holder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(values[position]);
holder.check.setId(position);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.check.isChecked()) {
holder.check.setChecked(false);
//store id
} else {
holder.check.setChecked(true);
//remove id
}
}
});
holder.check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
//store id
} else {
//remove id
}
}
});
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView img;
CheckBox check;
}
}
i am creating a custom list view with check boxes.all works fine except when i scroll the list the check boxes get checked/unchecked automatically.
How can i store the state of check boxes.
I have gone through all related questions but nothing worked for me.
Please take a look at my code and help me out.
Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener. Use a SparseBooleanArray.
Drawing from Romain guy's solution #
https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M
Then
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
Then use the checked state to set text to check box
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
Use the below example as reference and modify according your requirements. I have not used a viewholder in the sample. Use a view holder as you used in your code.
Example
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
int count;
private CheckBoxAdapter mCheckBoxAdapter;
String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.lv);
listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
listView.setAdapter(mCheckBoxAdapter);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
for(int i=0;i<GENRES.length;i++)
{
if(mCheckBoxAdapter.mCheckStates.get(i)==true)
{
result.append(GENRES[i]);
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}
});
}
public void onItemClick(AdapterView parent, View view, int
position, long id) {
mCheckBoxAdapter.toggle(position);
}
class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
String[] gen;
CheckBoxAdapter(MainActivity context, String[] genres)
{
super(context,0,genres);
mCheckStates = new SparseBooleanArray(genres.length);
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gen= genres;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return gen.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.checkbox, null);
tv= (TextView) vi.findViewById(R.id.textView1);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ gen [position]);
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
checkbox.xml
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="34dp"
android:text="TextView" />
<CheckBox
android:id="#+id/checkBox1"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:layout_marginRight="22dp"
android:layout_marginTop="23dp" />
</RelativeLayout>

filter text in listview android

I have a one EditText and one ListView. Item in ListView contains two TextView's. I need to filter ListView if the user enters something in EditText. My adapter:
public class AdapterUserIsp extends ArrayAdapter{
public static ArrayList<ItemUserIsp> data = new ArrayList<ItemUserIsp>();
Context context;
int ch = 0;
public static TextView header4;
#SuppressWarnings("unchecked")
public AdapterUserIsp(Context context, int textViewResourceId, ArrayList<ItemUserIsp> arr) {
super(context, textViewResourceId, arr);
if (arr != null) {
data = arr;
}
this.context = context;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int num) {
// TODO Auto-generated method stub
return data.get(num);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#SuppressLint("NewApi")
#Override
public View getView(int i, View someView, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(context);
if (someView == null) {
someView = inflater.inflate(R.layout.item_user_isp, arg2, false);
}
TextView header1 = (TextView) someView.findViewById(R.id.tvUserNameIsp);
TextView header2 = (TextView) someView.findViewById(R.id.tvUserPositionIsp);
header1.setText(data.get(i).header1);
header2.setText(data.get(i).header2);
return someView;
}
}
My activity:
final ListView lv = (ListView)findViewById(R.id.users);
final AdapterUserIsp adapter = new AdapterUserIsp(this,R.id.users,data);
final EditText search= (EditText)findViewById(R.id.search);
search_isp.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s);
lv.invalidateViews();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
But it don't work. Why? Thanks!
You have to update your adapter.
If you just add some new items to your list arr you have to call:
adapter.notifyDataSetChanged();
If you want to redraw all items you could set the adapter again:
lv.setAdapter(adapter);
Hope this will help you :)
insert the following line after lv.invalidateViews(); on onTextChanged method.
adapter.notifyDataSetChanged();
and also you have to implement your adapter class from Filterable if you want to override the filter method.
if adapter.notifyDataSetChanged(); doesn't work, do not use final for adapter, get adapter inside the onTextChanged and after filter your text.
YourAdapterOrWhateverAdapter adapter = (YourAdapterOrWhateverAdapter) listView.getAdapter();
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
NOTE: This is just guess..

Categories

Resources