I'm facing some issue with getting spinning values from list view.. in list view i have textview and related to spinner to it..
this is my Result.java
public class Result extends ListActivity{
SpinnerWrapper wrapper=new SpinnerWrapper();
Spinner sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
MyAdapter adap=new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules));
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setListAdapter(adap);
Button b=(Button) findViewById(R.id.button12);
//final Spinner sp=(Spinner) findViewById(R.id.spinner1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Result.this,Cal.class);
for(int i=0;i<15;i++){
//String text= sp1.getSelectedItem().toString();
//Log.d("This", text);
}
//Spinner sp1=og.getSp();
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(wrapper);
intent.putExtra("values", sp.getSelectedItem().toString());
//intent.putExtra("values", sp.setOnItemSelectedListener(wrapper));
startActivity(intent);
}
});
}
public void getObject(Spinner sp){
this.sp=sp;
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.item_layout, parent,false);
String[] items=getResources().getStringArray(R.array.modules);
TextView tv=(TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
Spinner sp=(Spinner)row.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
getObject(sp);
//og.setSp(sp);
//sp.setOnItemSelectedListener(wrapper);
//int dd=row.getId();
return row;
}
}
}
My Spinner Wrapper class is looks like this..
public class SpinnerWrapper implements OnItemSelectedListener {
public SpinnerWrapper() {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String text=parent.getSelectedItem().toString();
int intee=parent.getSelectedItemPosition();
String pos=Integer.toString(intee);
String tex=parent.getItemAtPosition(position).toString();
Log.d("Selected", text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I want to get spinning value of each spinner ..how can i do that??
//sp.setOnItemSelectedListener(wrapper);
I see your code is commented. so the OnItemSelectedListener is not called.
You call different listener for different spinner.
I don't see why you can't get the value by
String tex=parent.getItemAtPosition(position).toString();
Log.d("You have selected", tex);
You can create an ArrayList<String> and add the selected value to that ArrayList. It will be something like this.
static ArrayList<String> sth = new ArrayList<String>();
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"OnItemSelectedListener : "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
for (int i = 0; i < parent.getItemIdAtPosition(pos); i++) {
sth.add(parent.getItemAtPosition(pos).toString());
}
Log.w(getClass().getSimpleName(), "sth size " + sth.size());
}
You can pass that ArrayList around since the selected values are stored.
Related
private String[] genre = { "Action", "Adventure", "Comedy", "Biography", "Drama", "Family", "Horror", "Romance" };
private Integer images[] = { R.drawable.img_action, R.drawable.img_adventure, R.drawable.img_biografy, R.drawable.img_comedy, R.drawable.img_drama, R.drawable.img_family, R.drawable.img_horror, R.drawable.img_romantis };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setInitialImage();
spinner = (Spinner) findViewById(R.id.label_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( // make array adapter
this, android.R.layout.simple_spinner_item, genre
);
spinner.setAdapter(adapter); // connect adapter with spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { // get item from spinner
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
int index = spinner.getSelectedItemPosition();
if () {
currImage = images[0];
Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
.show();
}
setCurrentImage(); // called method
}
});
}
how, when I click one of the spinner items, then the image of the movie genre will appear.
what should I type in the if statement?
anyone can help me solve this problem?
Modify your onItemSelectedMethod to below
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
currImage = images[position];
Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
.show();
setCurrentImage(); // called method
}
The method onItemSelected doesn't work and I don't know why.
Below is my code.
Array adapter:
public class MyListAdapter extends ArrayAdapter {
Spinner spinner;
ListView listView;
/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
System.out.println("ciao1");
// spinner.setOnItemSelectedListener(this);
return row;
}
/* #Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) spinner.getSelectedItem();
System.out.println(selState);
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
}
fragment:
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
// listView.setOnItemClickListener(this);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
listView.setSelection(position);
String selState = (String) listView.getSelectedItem();
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
On ListViews you use onItemClicked. OnItemSelected is for Spinners. It's one of those Android nuisances...
In your method of setting OnItemSelectedListener(this) within the ArrayAdapter, I believe the issue is that, because every spinner in the list references the same instance of MyListAdapter, your OnItemSelected method is not distinguishing which spinner within your list was actually selected from-- it just references the spinner object that was last set to the MyListAdapter.spinner reference, which should be the last one in the list.
To remedy this, don't use spinner.getSelectedItem(). Instead use ((Spinner) parent).getSelectedItem(), since the parent object will be the actual spinner selected from. And at that point, you should make the spinner variable local to the getView method.
In the method of calling listView.setOnItemSelectedListener(...) in the fragment, I expect you meant to have listView.setOnItemClickListener(...), but that will listen for a click on a row in your list, not a selection from a spinner.
To detect a selection from one of your spinners, make the first modification above. To detect a click on a row in your listView as well, change to listView.setOnItemClickListener. Without more context for your objective or the actual errors occurring, it's hard to tell if both or just the first is desired.
EDIT:
From your comments, I think you should have this in your MyListAdapter:
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Spinner spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
parent.getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
return row;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) ((Spinner) parent).getSelectedItem();
System.out.println(selState);
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
And this in your fragment (no listeners):
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
I want to update a single row in my listview with different content once i press a button. I know i can use notifydatasetchanged() but that would update the whole listView.
I have read this answer and it fits perfectly for what I want to do.
I have done a listview with 5 rows and when I press the button I want to update the 4th row with a different text. I dont want to set the text programatically since this is just a dummy project just to see if refreshing a single row is possible and my real project is much more complex than just a textview.
So my question is: can i use getView() to update a single row in a listview?
Here is my code:
my Activity:
public class MainActivity extends Activity {
public ListView list1;
public listAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.my_list);
adapter = new listAdapter(this);
list1.setAdapter(adapter);
Button button1 = (Button) findViewById(R.id.my_button);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.setText("Different text");
View row2Update = list1.getChildAt(3);
list1.getAdapter().getView(3, row2Update, list1);
}
});
}
}
My adapter :
public class listAdapter extends BaseAdapter{
public Activity activity;
public String text="Normal Text";
public listAdapter(Activity activity){
this.activity = activity;
}
public void setText(String text){
this.text = text;
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout, null);
TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(text);
return rowView;
}
}
This is what the activity looks like:
But when I press my button nothing changes
You cannot call the getView() method of the adapter yourself. The adapter's getView() method is is only called, when
The listview is create
when the user scrolls the list view
when notifysetdatachanged() is called.
All these are done by the OS. GetView() is called for all the rows in the listview. It is not called for just a single row. So, if you want to change the rows, you have to provide the data again in a String[], ArrayList<> etc
If you want different text to appear for for a single row, onClick() of a button - you can do this
public class listAdapter extends BaseAdapter{
public Activity activity;
public ArrayList<String> text;
public listAdapter(Activity activity){
this.activity = activity;
}
public void setText(ArrayList<String> text){
this.text = text;
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout, null);
TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(text[position]);
return rowView;
}
}
And in your Activity :
list1 = (ListView) findViewById(R.id.my_list);
adapter = new listAdapter(this);
String[] entries={"Normal Text","Normal Text","Normal Text","Normal text","Normal text"};
ArrayList<String> text=Arrays.asList(entries);
adapter.setText(text);
list1.setAdapter(adapter);
Button button1 = (Button) findViewById(R.id.my_button);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.set(3,"Different Text");
adapter.setText(text);
list1.setAdapter(adapter);
}
});
There is another way of doing it also as #Andy suggested in one of the comments :
listViewPeople.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
//IF YOU WANT TO CHANGE THE CONTENT OF THE ROW CLICKED
if(position == someNumber) {
text.set(position,"different Text");
list1.setAdapter(text);
}
}
});
Sorry for the bold text. For some reason the CTRL+K is not working for the above code.
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
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.