i have two problems
1. spinner cannot be selected.
2. getting the spinner item if selected
i tried lots of things but dunno really what's with it if someone would help me i'd be really thankful
here's the code:
`
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List values;
public MobileArrayAdapter(Context context, List values) {
super(context, R.layout.req_row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.req_row, parent, false);
TextView req_date = (TextView) rowView.findViewById(R.id.req_date);
TextView request_time = (TextView) rowView.findViewById(R.id.request_time);
TextView task = (TextView) rowView.findViewById(R.id.tsk);
TextView issue = (TextView) rowView.findViewById(R.id.issue);
TextView employee_name = (TextView) rowView.findViewById(R.id.emp_name);
TextView contactno = (TextView) rowView.findViewById(R.id.contact);
TextView department = (TextView) rowView.findViewById(R.id.dep);
TextView comment = (TextView) rowView.findViewById(R.id.comment);
// final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
HashMap<String, String> m = new HashMap<String, String>();
m = (HashMap) values.get(position);
req_date.setText(m.get("req_date"));
request_time.setText(m.get("req_time"));
task.setText("TSK/"+m.get("id"));
issue.setText(m.get("Issue"));
employee_name.setText(m.get("Emp_Name"));
contactno.setText(m.get("Contact_No"));
department.setText(m.get("Department"));
comment.setText(m.get("Comment"));
final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
R.layout.support_simple_spinner_dropdown_item, specialistsList);
dataAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
empSpinner.setAdapter(dataAdapter);
empSpinner.setSelection(position);
empSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
sp = empSpinner.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Button send = (Button) rowView.findViewById(R.id.snd_tsk);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Send().execute();
}
});
return rowView;
}
}
`
Related
I have 3 classes relating to my checkbox section of my app in Android studio, atm the check box loads, but when selecting and deselecting the value doesn't save when I go bak into it from the main menu. any help would great!!!
public class WatchList extends AppCompatActivity {
ArrayList dataModels;
ListView listView;
private WatchListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("watchlist", "created watchlist activity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_list);
listView = (ListView) findViewById(R.id.listview2);
dataModels = new ArrayList();
dataModels.add(new WatchListClass(R.drawable.kookaburra,"Kookaburra","Albury", false));
dataModels.add(new WatchListClass(R.drawable.cockatoo, "Cockatoo" , "Bathurst", true));
dataModels.add(new WatchListClass(R.drawable.emu,"Emu", "Echuca", true));
dataModels.add(new WatchListClass(R.drawable.magpie, "Magpie", "Sydney", true));
adapter = new WatchListAdapter(dataModels, getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
WatchListClass dataModel= (WatchListClass) dataModels.get(position);
dataModel.checked = !dataModel.checked;
adapter.notifyDataSetChanged();
}
});
}
}
public class WatchListAdapter extends ArrayAdapter {
private ArrayList dataSet;
Context mContext;
private static class ViewHolder {
TextView birdWatchName, birdWatchLocation;
ImageView birdWatchImage;
CheckBox checkBox;
}
public WatchListAdapter(ArrayList data, Context context) {
super(context, R.layout.watch_list, data);
this.dataSet = data;
this.mContext = context;
}
#Override
public int getCount() {
return dataSet.size();
}
#Override
public WatchListClass getItem(int position) {
return (WatchListClass) dataSet.get(position);
}
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.watch_list, parent, false);
viewHolder.birdWatchImage = (ImageView) convertView.findViewById(R.id.birdWatchImage);
viewHolder.birdWatchName = (TextView) convertView.findViewById(R.id.birdWatchName);
viewHolder.birdWatchLocation = (TextView) convertView.findViewById(R.id.birdWatchLocation);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
WatchListClass item = getItem(position);
viewHolder.birdWatchImage.setImageResource(item.birdWatchImage);
viewHolder.birdWatchName.setText(item.birdWatchName);
viewHolder.birdWatchLocation.setText(item.birdWatchLocation);
viewHolder.checkBox.setChecked(item.checked);
return result;
}
}
public class WatchListClass {
public String birdWatchName, birdWatchLocation;
int birdWatchImage;
boolean checked;
WatchListClass(int birdWatchImage, String birdWatchName,String birdWatchLocation, boolean checked) {
this.birdWatchName = birdWatchName;
this.birdWatchLocation = birdWatchLocation;
this.birdWatchImage = birdWatchImage;
this.checked = checked;
}
}
u can user shared preferences for check Box
try this one
How to save checkbox value with shared preferences?
you can use local database in app to store check box values and retrieve it back.
Can you help me out, I'm learning! ;-)
I have two buttons "+" and "-" and I want them to increase or decrease the amount by one up or one down. How do I make sure that it will only have effect on the right textview using an adapter.
Now all the buttons only effects the first one. I know I have to get the position/id of the array. But I don't know how.
btw
Merk = Brand
Aantal = Amount
public class ListViewDemo2 extends Activity {
private ArrayList<String> data = new ArrayList<String>();
int aantal = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_demo2);
ListView lv = (ListView) findViewById(R.id.bestel_lijst);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
generateListContent();
}
private void generateListContent(){
for (int i =0; i < 55; i++){
data.add("this is row number: " + i);
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal-1;
displayAantal(aantal);
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal+1;
displayAantal(aantal);
}
});
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
mainViewholder.Merk.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView Merk;
TextView Aantal;
Button btnup;
Button btndown;
}
public void displayAantal(int aantal) {
TextView aantalView = (TextView) findViewById(R.id.Aantal);
aantalView.setText(String.valueOf(aantal));
}
}
your problem seems to be in your displayAantal(int) method, in particular, in this line:
TextView aantalView = (TextView) findViewById(R.id.Aantal);
You do not need to re-initialize that field here, since it is already initialized inside getView():
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
}
String str = getItem(position);
mainViewholder.Merk.setText(str);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
return convertView;
}
I made a listview in Android Studio. The listview has a image in every item but I don't know how to make it clickable. I did browse the internet for a solution but the hard part seems to be implementing it in my own code. I cannot figure that out.
Image in question = ex_img
-- Keep in mind when the image is clicked, it should also know its position in the list.
Thanks for reading and I hope you can help me out.
Adapter for list:
public class CustomList extends ArrayAdapter<String> {
private String[] ex_name;
private String[] ex_diff;
private String[] ex_muscle;
private String[] ex_dpr;
private Integer[] ex_img;
private Activity context;
public CustomList(Activity context, String[] ex_name, String[] ex_diff, String[] ex_muscle, String[] ex_dpr,
Integer[] ex_img) {
super(context, R.layout.row_layout, ex_name);
this.context = context;
this.ex_name = ex_name;
this.ex_muscle = ex_muscle;
this.ex_diff = ex_diff;
this.ex_dpr = ex_dpr;
this.ex_img = ex_img;
}
//LIST --> XML
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.row_layout, null, true);
TextView list_name = (TextView) listViewItem.findViewById(R.id.ex_name);
TextView list_diff = (TextView) listViewItem.findViewById(R.id.ex_diff);
TextView list_muscle = (TextView) listViewItem.findViewById(R.id.ex_muscle);
ImageView list_image = (ImageView) listViewItem.findViewById(R.id.ex_img);
TextView list_dpr = (TextView) listViewItem.findViewById(R.id.ex_dpr);
list_name.setText(ex_name[position]);
list_muscle.setText(ex_muscle[position]);
list_diff.setText(ex_diff[position]);
list_dpr.setText(ex_dpr[position]);
list_image.setImageResource(ex_img[position]);
return listViewItem;
}
}
Part from MainActivity that might be needed:
private Integer img[] = {
R.drawable.ic_favorite_white_24dp,
R.drawable.ic_location_on_white_24dp,
R.drawable.ic_update_white_24dp,
R.drawable.ic_local_dining_white_24dp,
R.drawable.ic_local_dining_white_24dp
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList customList = new CustomList(this, name, diff, muscle, dpr, img);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(customList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),"You Clicked "+name[i],Toast.LENGTH_SHORT).show();
}
});
}
Add onClickListener to your ImageView in getView() -
list_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here you have the position too.
});
Just make the position parameter final in getView()
I want get values of edittext in listview.
This is my CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItem;
ImageView imgIcon, add_cart;
TextView txtTitle;
EditText colli, prezzo, quantita;
String colliStr, prezzoStr, quantitaStr;
ArrayList<String> ArrayPrezzo = new ArrayList<String>();
ArrayList<String> ArrayQuantita = new ArrayList<String>();
ArrayList<String> ArrayColli = new ArrayList<String>();
CustomAdapter(Context context, List<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.single_row, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.icon);
txtTitle = (TextView) convertView.findViewById(R.id.title);
add_cart = (ImageView) convertView.findViewById(R.id.icon);
colli = (EditText) convertView.findViewById(R.id.editText3);
prezzo = (EditText) convertView.findViewById(R.id.editText2);
quantita = (EditText) convertView.findViewById(R.id.editText);
add_cart.setFocusable(true);
add_cart.setClickable(true);
final RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(R.drawable.product);
txtTitle.setText(row_pos.getTitle());
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE I WANT GET THE VALUES OF EDITTEXT;
}
});
return convertView;
}
can someone help me please? In ly listview there are:
textview | edittext1 | edittext2 | edittext3 | imageview
I don't know how to get values of edittext at position current when i click the button.
Can you make a example with my code?
Thnank you
UPDATE:
I have edited the listener:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prezzoStr = prezzo.getText().toString();
Toast.makeText(context, prezzoStr, Toast.LENGTH_SHORT)
.show();
}
});
but prezzoStr is empty. Toast doesn't show nothing.
I think need use row_pos variable for getting the value of a determinate edittext.
Help please
It's not difficult
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String colliValue = colli.getText().toString());
String prezzoValue = prezzo.getText().toString());
String quantitaValue = quantita.getText().toString());
}
});
You have to get parent view and then find text view by id:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) findByIdRecursively(v, R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});
...
public View findByIdRecursively(View view, int targetId) {
View result = view.findViewById(targetId);
if (result != null) {
return result;
}
View parent = (View) view.getParent();
if (parent == null) {
return null;
}
return findByIdRecursively(parent, targetId);
}
The view in onClick is the view that was clicked (add_cart in this case)
... or you can do:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
add_cart = (ImageView) convertView.findViewById(R.id.icon);
add_cart.setTag(convertView);
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) ((View)v.getTag()).findViewById(R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});
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.