add all edittexts data of listview in arraylist - android

I have a list view containing textview and edit texts and i have to add these values in array list.As i am trying it not all values are added to Arraylist,only values of that particular visible view gets added.How should I modify my code so that all the data gets added to arraylist.
list = (ListView) findViewById(R.id.list);
personList = new ArrayList<HashMap<String,String>>();
flags=0;
getData();
placeord=(Button)findViewById(R.id.placeorder);
placeord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isNetworkConnected()) {
ArrayList<String> m = new ArrayList<String>();
ArrayList<String> si = new ArrayList<String>();
EditText q;
TextView sizes;
for (int i =0; i<= list.getLastVisiblePosition() - list.getFirstVisiblePosition(); i++) {
View v = list.getChildAt(i);
q = (EditText) v.findViewById(R.id.numberofitems);
sizes = (TextView) v.findViewById(R.id.size);
if(q.getText().toString().length()>0) {
si.add(sizes.getText().toString());
flags=1;
}
m.add(q.getText().toString());
}
if (flags == 0) {
Toast.makeText(getApplicationContext(), "please add some quantity", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(getApplicationContext(),val,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(placecolor.this, placeorder2.class);
Bundle extras1 = new Bundle();
extras1.putStringArrayList("numberofitems", m);
extras1.putStringArrayList("sizes", si);
intent.putExtras(extras1);
startActivity(intent);
}
}
}
}

It happens because android recycle the view of ListView and Recyclerview. You need to add(save) data of EditText by implementing OnTextChangeListener. By implementing it you can get the entered text and assign it to particular position in List<>.
Example :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_list, null);
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.editText = (EditText) convertView.findViewById(R.id.editText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView.setText(arr[position].name);
holder.editText.setText(arr[position].nameValue);
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arr[holder.ref].nameValue = arg0.toString(); // Set value in list
}
});
return convertView;
}

Related

Search in Android ListView

I am trying to add a search the ListView capability following this tutorial. The problem is that I am not able to show anything when I type something in the search EditText. I want to be able to display what the user is searching but that is not what's happening. What changes do I need to make to the following code so this can be achieved?
Code:
private void loadList()
{
Cursor res = tbl_business_desc.getRegisterData();
int count = res.getCount();
lvListItemsData = new android.projects.helper.mylist.ListViewItems[count];
res.moveToFirst();
for (int i = 0; i < count; i++)
{
String ControlNum = res.getString(res.getColumnIndex(tbl_business_desc.col_ControlNum));
String BizName = res.getString(res.getColumnIndex(tbl_business_desc.col_BizName));
String Owner = res.getString(res.getColumnIndex(tbl_business_desc.col_Owner));
String Add = res.getString(res.getColumnIndex(tbl_business_desc.col_Add));
String Tel = res.getString(res.getColumnIndex(tbl_business_desc.col_Tel));
String Email = res.getString(res.getColumnIndex(tbl_business_desc.col_Email));
lvListItemsData[i] = new android.projects.helper.mylist.ListViewItems(ControlNum,BizName,Owner,Add,Tel,Email);
res.moveToNext();
}
lvList = (ListView) findViewById(R.id.lvMylist);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Set the adapter to our ListView
lvListadapter = new android.projects.helper.list.ListViewItemsAdapter(this, R.layout.listview_item_row_masterlist_body, lvListItemsData);
lvList.setAdapter(lvListadapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MyListActivity.this.lvListadapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
showStatus(position);
}
});
}
ListViewItemAdapter Code
public class ListViewItemsAdapter extends ArrayAdapter<ListViewItems> {
Context mContext;
int layoutResourceId;
ListViewItems data[] = null;
public ListViewItemsAdapter(Context mContext, int layoutResourceId, ListViewItems[] data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItem = convertView;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
listItem = inflater.inflate(layoutResourceId, parent, false);
// get the elements in the layout
TextView textControlNum = (TextView) listItem
.findViewById(R.id.txtControlNum);
TextView textBizName = (TextView) listItem
.findViewById(R.id.txtBizName);
TextView textOwner = (TextView) listItem
.findViewById(R.id.txtOwner);
TextView textAdd = (TextView) listItem
.findViewById(R.id.txtAdd);
TextView textTel = (TextView) listItem
.findViewById(R.id.txtTel);
TextView textEmail = (TextView) listItem
.findViewById(R.id.txtEmail);
ListViewItems listViewItems = data[position];
try {
textControlNum.setText(listViewItems.ControlNum);
textBizName.setText(listViewItems.BizName);
textOwner.setText(listViewItems.Owner);
textAdd.setText(listViewItems.Add);
textTel.setText(listViewItems.Tel);
textEmail.setText(listViewItems.Email);
}
catch (Exception e){}
if(position % 2 == 0){
listItem.setBackgroundColor(Color.rgb(243,251,254));
}else{
listItem.setBackgroundColor(Color.rgb(255,255,255));
}
return listItem;
}
}
You might need to overrride filter in your adapter class. See this SO answer. Hope that will help.
First, add a break point at line
MyListActivity.this.lvListadapter.getFilter().filter(cs);
to see whether the event has been triggered by text change.
Second, as you could see in source code of ArrayFilter, it filters the item by code
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
for (String word : words) {
if (word.startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
Therefore, if there is nothing left, it might be that, it should be nothing left.

Can not set value to list view using custom adapter

When I am trying to set value to the list view using custom adapter it shows only the last entered value in the hash map. hash map is static.I dont know why I am not getting all the values in the hashmap for that keys I am used in hashmap.
here my code
public class Nextclass extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextclass);
list = (ListView)findViewById(R.id.list1);
SelectItems();
ViewItems();
}
private List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct code ,desc ,name "
+ " from item", null);
while (c.moveToNext()){
String cod = c.getString(c.getColumnIndex("code"));
datanums.put("code", cod);
String desc1 = c.getString(c.getColumnIndex("desc"));
datanums.put("desc", desc1);
String name = c.getString(c.getColumnIndex("name"));
datanums.put("name", name);
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
private void ViewItems() {
// TODO Auto-generated method stub
arrTemp = new String[datas.size()];
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());
}
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.code);
holder.textview3 = (TextView) convertView.findViewById(R.id.desc);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(datanums.get("name"));
holder.textview3.setText(datanums.get("code"));
holder.textView2.setText(datanums.get("desc"));
holder.editText1.setText(arrTemp[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1;
int ref;
}
please help.I used alternate methods,but not worked.thanks in advance
holder.textView1.setText(datas.get(position).get("name"));
holder.textview3.setText(datas.get(position).get("code"));
holder.textView2.setText(datas.get(position).get("desc"));
you are adding hash map in the array list but in the adapter you are trying to get data from the hash map, instead of that you need to take value from arraylist which will return hashmap object and from that object you need to get value for your keys.
hope this will help.

edit text in list view empty or not

In my list view ,I have Edittext and some textviews. when I am trying to iterate over listview to get edittext values, I got some problems.I want to perform actions only if my edittext in the listview is not empty. otherwise i want to continue the iteration.I need only non empty values from the edittexts when iterate through listview. my listview name is "list" here.
my code is
if (list != null) {
for (int i = 0; i < list.getChildCount(); i++) {
ed2 = (EditText) list.getChildAt(i).findViewById(R.id.pcs);
if (ed2.getText().toString().trim().length() != 0) {
TextView im_codetext = (TextView) vie.findViewById(R.id.code);
imcode = im_codetext.getText().toString();
try {
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor cr = newDB.rawQuery("select distinct im_srp_pc, im_subgroup_code from itemmaster where im_code =" + imcode, null);
while (cr.moveToNext()) {
srppc = cr.getString(cr.getColumnIndex("im_srp_pc"));
String subcode = cr.getString(cr.getColumnIndex("im_subgroup_code"));
databas.put("subgp", subcode);
}
srps = Float.valueOf(srpval);
qtys = Integer.valueOf(ed2.getText().toString());
} catch (NumberFormatException e) {
srps = 0;
}
float amnts = srps * qtys;
sumnet = sumnet + amnts;
else{
Log.v(" empty", " empty");
}
}
}
}
My adapter class is
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
holder.textview3 = (TextView) convertView.findViewById(R.id.code);
holder.editText2 = (EditText) convertView.findViewById(R.id.pcs);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
String [] names = new String[550];
String [] codes = new String[550];
String [] prize = new String[550];
DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
c.moveToFirst();
while (c.moveToNext()){
Log.v("item detailss", c.toString());
String cod = c.getString(c.getColumnIndex("im_code"));
codes[c.getPosition()] =cod;
String desc1 = c.getString(c.getColumnIndex("im_desc"));
names[c.getPosition()] = desc1;
String price = c.getString(c.getColumnIndex("im_srp"));
prize[c.getPosition()] = price;
}
holder.textView1.setText(names[position+1]);
holder.textview3.setText(codes[position+1]);
holder.textView2.setText(prize[position+1]);
holder.editText2.setText(arrTemp[position]);
holder.editText2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
}
please help.thanks in advance.
I have a Solution.
Declare a HashMap in your custom adapter class.
public class CustomAdapter extends BaseAdapter{
public HashMap<Integer,String> textMap = new HashMap<Integer,String>();
....
In getView() Method get text from HasMap and set in EditText.
#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.list_item, null);
holder.editText = (EditText)rowView.findViewById(R.id.editText);
holder.editText.setText(textMap.get(position));
....
And in TextChangeListner add text in HashMap by position.
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
textMap.put(position,holder.editText.getText().toString());
}
});
Now in your Activity you can get HashMap of adapter and get its value by position. and if there is no text in particuller position ,means edttext is empty.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
listView = (ListView)findViewById(R.id.listView);
final CustomAdapter customAdapter = new CustomAdapter(this,names);
listView.setAdapter(customAdapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<names.length;i++){
Toast.makeText(MainActivity.this, customAdapter.textMap.get(i), Toast.LENGTH_SHORT).show();
}
}
});

How to get text from EditText within ListView with multiple Views?

I have a ListView that contains two TextViews and one EditText in each row. I have searched and searched and cannot figure out how to get the text that the user enters into the EditText. My most recent attempt implements a TextWatcher.
public void addIngredientToMeal(String ingredientName) {
ingredient_list = (ListView) findViewById(R.id.ingredients_in_meal_listView);
int numberOfIngredients = ingredient_list.getChildCount();
ListAdapter adapter = new ListAdapter();
adapter.name_array = new String[numberOfIngredients+1];
for (int i = 0; i < numberOfIngredients; i++)
adapter.name_array[i] = (String) ingredient_list.getItemAtPosition(i);
adapter.name_array[numberOfIngredients] = ingredientName;
ingredient_list.setItemsCanFocus(true);
ingredient_list.setAdapter(adapter);
}
private class ListAdapter extends BaseAdapter{
public String[] name_array, qty_array;
#Override
public int getCount() {
// TODO Auto-generated method stub
if(name_array != null && name_array.length != 0){
return name_array.length;
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return name_array[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = NewMeal.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.ingredients_in_meal_layout, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.ingredient_name_in_new_meal_textView);
holder.editText1 = (EditText) convertView.findViewById(R.id.ingredient_qty_in_new_meal_editText);
holder.textView2 = (TextView) convertView.findViewById(R.id.ingredient_unit_in_new_meal_textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(name_array[position]);
holder.textView2.setText(databaseAdapter.dbQuery(name_array[position]));
holder.editText1.setText(qty_array[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
qty_array[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
TextView textView1, textView2;
EditText editText1;
int ref;
}
}
}
For those who may have a similar issue, I solved the problem I was having.
The problem:
Need to get values entered in multiple editTexts that are contained within a listView. Using listviewname.getItemAtPosition(integer) only gets the value of the first editText. Moreover, I had multiple views contained in each row of the listview, so getItemAtPosition only returned the value of the first view in the first row of the listView.
The solution:
Define a listView object that identifies the listview that we care
about
Define a View object that can get the listview
Define whatever field(s) you are trying to get from the listview (EditTexts, TextViews Etc.)
Get the number of rows that the listview currently contains
Set the view object using listviewname.getChildAt(position)
Assign the field (say, an editText) using something like (EditText) viewObject.findViewById(R.id.edittextid)
Get the value from the field using editTextObject.getText().toString()
Here's some sample code that could be implemented on a button press:
ListView lvName = (ListView) findViewById(R.id.listview1);
View v;
EditText et;
int listLength = lv.getChildCount();
String[] valueOfEditText = new String(listLength);
for (int i = 0; i < listLength; i++)
{
v = lvName.getChildAt(i);
et = (EditText) v.findViewById(R.id.edittext1);
valueOfEditText[i] = et.getText().toString();
}
getFocusedChild(position) it will return a View, or findFocus()
View row = ingredient_list.getFocusedChild();
EditText et = (EditText)row.findViewById(R.id.ingredient_qty_in_new_meal_editText);
Edit
Sorry i misunderstood create a gist will toast with the input
https://gist.github.com/imjarp/9313c687ccbe339d052f

ListView not showing right item after filtering

Its been a week since I'm stuck with this problem.
I developed an application with a listView and editText to make a search. When I make a search the new list is showed very well but when I click on the item it redirect me to the item of the inicial list.
I don't know what to do please help me. These are my codes.
-For the ReaderListAdapter :
public class ReaderListAdapter extends BaseAdapter {
ArrayList<Reader> listReader = new ArrayList<Reader>();
ArrayList<Reader> arrayList;
LayoutInflater layoutInflater;
Context context;
int lastPosition = -1;
// constructeur
public ReaderListAdapter(Context context, ArrayList<Reader> listReader) {
this.listReader = listReader;
this.context = context;
arrayList = new ArrayList<Reader> ();;
layoutInflater = LayoutInflater.from(this.context);
arrayList.addAll(listReader);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listReader.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listReader.get(position);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
static class ViewHolder {
TextView nomView;
TextView priceView;
ImageView pictureView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.reader_row, null);
holder = new ViewHolder();
// initialisation des vues
holder.nomView = (TextView) convertView.findViewById(R.id.name);
holder.priceView = (TextView) convertView.findViewById(R.id.price);
holder.pictureView = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// affchier les données convenablement dans leurs positions
holder.nomView.setText(listReader.get(position).getName());
holder.priceView.setText(String.valueOf(listReader.get(position).getPrice()));
holder.pictureView.setBackgroundDrawable(listReader.get(position).getPicture());
// changer R.anim.ton_effet
Animation animation = AnimationUtils.loadAnimation(context,(position > lastPosition)
? R.anim.up_from_bottom: R.anim.up_from_bottom);
convertView.startAnimation(animation);
position=lastPosition;
return convertView;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
listReader.clear();
if(charText.length()==0){
listReader.addAll(arrayList);
}
else{
for (Reader c : arrayList) {
if (c.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
listReader.add(c);
}
}
}
notifyDataSetChanged();
}
}
-For the MainActivity:
public class MainActivity extends Activity {
String[] listNames = { "kooora","yahoo", "hespress"};
int[] listPrices = { 1, 2, 3 };
ArrayList<Reader> listReader = new ArrayList<Reader>();;
ArrayList<Reader> listReaderNew;
ListView lv;
EditText search;
ReaderListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listReader);
search = (EditText) findViewById(R.id.search);
Drawable[] listPictures = {getResources().getDrawable(R.drawable.a1),getResources().getDrawable(R.drawable.a2),getResources().getDrawable(R.drawable.a3)};
for (int i = 0; i < listPictures.length; i++) {
listReader.add(new Reader(i + 1, listNames[i], listPictures[i], listPrices[i]));
}
adapter=new ReaderListAdapter(getApplicationContext(), listReader);
lv.setAdapter(adapter);
//lv.setAdapter(new ReaderListAdapter(getApplicationContext(), listReader));
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = search.getText().toString().toLowerCase(Locale.getDefault());
MainActivity.this.adapter.filter(text);
}});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent=new Intent(MainActivity.this,WebActivity.class);
switch (position) {
case 0:
intent.setData(Uri.parse("http://www.kooora.com")) ;break;
case 1:
intent.setData(Uri.parse("http://www.yahoo.com")) ;break;
case 2:
intent.setData(Uri.parse("http://www.hespress.com")) ;break; }if (intent != null) {
startActivity(intent);
}}});}}
Please help me. Thanks in advance!
I think the code in onItemClick is causing this.
The problem:
searchTerm = "yahoo"
You filter yahoo and show the listitem for yahoo. Lets says its position in the list is 0. When the user clicks it, the onItemClick will be called and case 0: will be executed.
The correct logic should be,
Reader reader = parent.getAdapter().getItem(position);
String searchTerm = reader.getName(); // or whichever is the id for that listitem
if(searchTerm.contains("yahoo")) {
intent.setData(Uri.parse("http://www.yahoo.com"))
} // and so on

Categories

Resources