I've a ListView with multiple TextView in a single row. The TextView values are name, groupname, date and some hidden fields. And a spinner outside of this ListView. Once i select item from spinner ListView needs to filtered. That is the other row will be gone. I've try setVisibility() method but it's not works good. I'm using simple adapter. How can i achieve this. Please help me.
My piece of sample code:
Spinner draft_filter = (Spinner) findViewById(R.id.draft_filter);
draft_filter.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Spinner draft_spinner = (Spinner) arg0;
String selected_string = (String) draft_spinner.getSelectedItem();
//Save this String and filter the listview using this
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ListView level1 = (ListView) findViewById(R.id.listview);
level1.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
for (int i=0; i<level1.getChildCount(); i++)
{
// Set the Visibility of the row here
}
});
//For loading listview
//Here fillmaps is the arraylist contains hashmap values
String[] drafts = new String[] { "child_name", "age"};
int[] controls = new int[] { R.id.item1, R.id.item2};
adapter = new SimpleAdapter(getApplicationContext(), fillMaps,
R.layout.draft_observation_list_layout, drafts, controls);
level1.setAdapter(adapter);
How to filter this listview Please help.
Call adapter each time when Spinner item value is change with filtered list value.
I tried lots of stuff but the solution is very simple:
((SimpleAdapter) MyActivity.this.adapter).getFilter().filter(filter_text);
Related
I have created simple list view,what i want to do is that when user click on list view,selected record should show in text view in my code it work properly,but problem is that when user again click i want to show that record in second text view,please give me idea/code how to do this..please please help me...i am new in android..
public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv1,tv2,tv3,tv4;
ListView li;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=(ListView)findViewById(R.id.listView1);
tv=(TextView)findViewById(R.id.setText);\
tv1=(TextView)findViewById(R.id.setText1);
tv2=(TextView)findViewById(R.id.setText2);
tv3=(TextView)findViewById(R.id.setText3);
tv4=(TextView)findViewById(R.id.setText4);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);
li.setAdapter(adapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
tv.setText(""+li.getItemAtPosition(position));
}
});
There are many ways you can deal with it.
ArrayList<Boolean> firstTimers = new ArrayList<Boolean>();
for (Int I =0; I< item.length(); I++){
firstTimers.add(false);
}
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
if( !firstTimers.get(position ){
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
tv.setText(""+li.getItemAtPosition(position));
firstTimers.set(position,true);
}else{ // show in other textview
}
}
});
I hope i'm understanding you right. You want different textviews to have text depending on how many tiems it's clicked.If so, set a static counter to count how many times it's clicked. In your OnItemClick increment this counter and check for it's value, then set the text of the TextViews depending on it's value.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
if (myCounter >=2){
someTextView.setText(""+li.getItemAtPosition(position));
}else{
anotherTextView.setText("blabla")'
}
}
Edit: Make an ArrayList holding your TextViews in the correct order. Using the position int given in OnItemClick you can take out the correct TextView and use .setText on them!
I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}
I have listview which has 5000 items.
I want to get item of listview at some position and do smth with it, I call getListView().getChildAt(1) method and it returns null.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
cur = ItemDao.getItemsCursor();
column_names = new String[] { ItemDao.NAME, ItemDao.MEASUREUNIT, ItemDao.REM };
columns = new int[] { R.id.item_name, R.id.item_measureunit, R.id.item_rem };
scadap = new SimpleCursorAdapter(this, R.layout.list_items, cur, column_names, columns, 0);
ListView list = getListView();
list.setDivider(getResources().getDrawable(android.R.drawable.divider_horizontal_bright));
list.setDividerHeight(10);
setListAdapter(scadap);
list.invalidateViews();
(getListView().getChildAt(getListView().getFirstVisiblePosition())).setVisibility(View.INVISIBLE);
}
The problem might be due to trying to retrieve list view's child item on onCreate() where any view-related methods will return null (or empty) because the list has not been rendered yet.
One solution could be to get your listview and set the adapters and listeners in onPostCreate() method.
Try to use OnItemClickListener() for getting selected item
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String getSelectedItemOfList = yourList.get(pos); // here you can get selected item.
}
});
Hope this helps.
This is my code :
Here _alProduct is an ArrayList defined static in the main class.
I am deleting an item from _alProduct on long click of listview.
Now I want to display the listview with the deleted item removed.
public class MCRActivity2 extends Activity {
TextView tvShoppingCart;
ListView lvSelectedItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mcractivity2);
lvSelectedItems = (ListView) findViewById(R.id.lvSelectedItems);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MobileCashRegisterActivity._alProduct);
lvSelectedItems.setAdapter(adapter);
lvSelectedItems.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> adapter, View view,
int position, long arg3) {
// TODO Auto-generated method stub
String text = "Clicked item is " + adapter.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();// ""+ lvSelectedItems.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
MobileCashRegisterActivity._alProduct.remove(position);
MobileCashRegisterActivity._alPrice.remove(position);
MobileCashRegisterActivity._alQuantity.remove(position);
return false;
}
});
}
}
Call remove() on your ArrayAdapter, instead of calling remove() on the ArrayList. This will both remove the item from the ArrayList and tell the AdapterView to refresh its contents.
i have one spinner which display data from database. now i want to display data in grid view which selected in spinner.
so plz give good solution for my this question.
I think you are trying to get the value selected from the spinner and display it in grid
View,if so try this..
Spinner spin_type = (Spinner) findViewById(R.id.Spinner_type);
spin_type.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selected Value=array_type[arg2].toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Where array_type your String array that is having complete "DATA"..
You will get the Value selected in the Spinner in "Selected Value"..