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.
Related
I am creating an app in android studio.
I need to update the list view with a new url that is sent via text message.
How do I add the new url to my list?
This is what I currently have for my main code.
public class MainActivity extends AppCompatActivity {
ListView list;
WebView webView;
ArrayList<String> sites = new ArrayList<>("https://www.google.com", "https://www.yahoo.com", "https://www.apple.com");
String [] url = {"https://www.google.com", "https://www.yahoo.com", "https://www.apple.com"};
MyBroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listview);
MyListAdapter adapter = new MyListAdapter(this, sites);
sites.add("new site");
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
list.setDividerHeight(5);
webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient(){});
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
Toast.makeText(getApplicationContext(), sites.get(position), Toast.LENGTH_SHORT).show();
webView.loadUrl("https://www.google.com");
webView.loadUrl(url[position]);
}
});
receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(receiver,filter);
}
}
This is myAdapterList:
public class MyListAdapter extends ArrayAdapter<String> {
private Activity context_;
private ArrayList<String> sites;
//constructor
public MyListAdapter(Activity context, ArrayList<String> sites){
super(context,R.layout.row,sites);
context_ = context;
this.sites = sites;
}
public View getView (int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater inflater = context_.getLayoutInflater();
convertView = inflater.inflate(R.layout.row,null);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
if(holder == null){
holder = new ViewHolder(convertView);
holder.tv_link.setText(sites.get(position));
}
else{
holder.tv_link.setText(sites.get(position));
}
return convertView;
}
private class ViewHolder{
TextView tv_link;
public ViewHolder(View row){
tv_link = (TextView) row.findViewById(R.id.tv_link);
}
}
}
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;
}
in my android application I am displaying the contact list of phone in a list view and a check box in each row for selecting contacts. But when I am selecting a particular row about tenth row is also getting selected automatically. I am giving my code below, if any one knows please help..
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
final ViewHolder mHolder;
if (convertView == null)
{
// gridView = new View(context);
// get layout from mobile.xml
//gridView = inflater.inflate(R.layout.contact, null);
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
));
}
else
{
mHolder = (ViewHolder) convertView.getTag();
mHolder.textSelector.setOnCheckedChangeListener(null);
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textSelector.setFocusable(false);
return convertView;
}
private class ViewHolder {
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Well Thats because ViewHolder will recycle the Views everytime you Scroll
i suggest you to Use onCLick on ListItem instead checkbox
To overcome this Declare a SparseBooleanArray
SparseBooleanArray sba=new SparseBooleanArray();//this should be global
Then set the items checked state as soon as you render it
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
mHolder.textSelector.setChecked(sba.get(position));
Then write a onClickListener to you convertView and check it manually
convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mHolder.textSelector.setChecked(isChecked);
sba.put(position,isChecked); //storing the state
}
}
);
**Well Now the sba has list items checked and you can use that for further Actions**
# Jocheved... edit your code like this...
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
SparseBooleanArray sba=new SparseBooleanArray();
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder mHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textName.setSelected(true);
mHolder.textSelector.setChecked(sba.get(position));
mHolder.textSelector.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if(mHolder.textSelector.isChecked())
{
sba.put(position, true);
}
else
{
sba.put(position, false);
}
}
});
return convertView;
}
private class ViewHolder
{
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
I have a created a ListView with a custom layout showing the users saved on Parse.com
This is the code for the custom adapter I have created
public class StatusAdapter extends ArrayAdapter {
protected Context mContext;
protected List mStatus;
public StatusAdapter(Context context, List status) {
super(context, R.layout.usertem, status);
mContext = context;
mStatus = status;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.usertem, null);
holder = new ViewHolder();
holder.usernameHomepage = (TextView) convertView
.findViewById(R.id.userName);
holder.statusHomepage = (TextView) convertView
.findViewById(R.id.userEmail);
holder.proPic = (ParseImageView) convertView
.findViewById(R.id.medopic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseUser statusObject = (ParseUser) mStatus.get(position);
// title
String username = statusObject.getString("name");
holder.usernameHomepage.setText(username);
// content
String status = statusObject.getEmail();
holder.statusHomepage.setText(status);
ParseFile picc = statusObject.getParseFile("profile_picture");
holder.proPic.setParseFile(picc);
holder.proPic.loadInBackground();
return convertView;
}
public static class ViewHolder {
TextView usernameHomepage;
TextView statusHomepage;
ParseImageView proPic;
}
}
But the problem I have is that I want to listen to list item clicks where my activity extends simple activity not List Activity this is my activity
public class UsersList extends Activity {
private ParseQueryAdapter<ParseObject> mainAdapter;
protected List<ParseUser> mUsers;
ListView listhope;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_list);
listhope = (ListView) findViewById(R.id.userlistlist);
ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("_User");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> list, ParseException e) {
if (e == null) {
mUsers = list;
StatusAdapter adapter = new StatusAdapter(listhope.getContext(), mUsers);
listhope.setAdapter(adapter);
} else {
}
}
});
}
[...]
Please help fast
In your onCreate write
listhope.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//position is the row on which the user has clicked
ParseUser parseUser=mUsers.get(position);
//now you have the objext parseUser and this objects is what you needed.
}
});
First im loading the data dynamically to a grid which is in the PMenu.java, then each item has view more button. once I pressed that I want to load the image, name and its amount in the item description view.
Im using a custom grid to load data to the grid in PMenu.java, and I have placed a button in the custom grid, so that it will navigate to viewmore.java.
I want to know once i press the button then how to load the data to viewmore.java file
PMenu.java fragment
GridView grid;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_grid_main, container, false);
new PMenuAsyncTask(getActivity(), this).execute();
grid = (GridView) view.findViewById(R.id.grid);
return view;
}
#Override
public void onTaskCompleted(JSONArray responseJson) {
try {
List<String> descriptions = new ArrayList<String>();
List<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);
if ((object.getString("MainCategoryID")).equals("1")
&& (object.getString("SubCategoryID")).equals("1")) {
Log.i("ImageURL ", object.getString("ImageURL"));
imageUrls.add(object.getString("ImageURL"));
Log.i("Description ", object.getString("Description"));
descriptions.add(object.getString("Description"));
}
}
CustomGrid adapter = new CustomGrid(getActivity(), descriptions,
imageUrls);
grid.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
CustomGrid class
public class CustomGrid extends BaseAdapter {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public CustomGrid(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return descriptions.size();
}
#Override
public Object getItem(int position) {
return descriptions.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.fragment_pizza, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
Button backButton = (Button) convertView.findViewById(R.id.button1);
backButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent next = new Intent(context, viewmore.class);
context.startActivity(next);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
}
});
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
viewmore.java
public class viewmore extends Activity {
private Context context;
private final List<String> descriptions;
private final List<String> imageUrls;
public viewmore(Context c, List<String> descriptions, List<String> imageUrls) {
this.context = c;
this.descriptions = descriptions;
this.imageUrls = imageUrls;
}
private ActionBar actionBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_cart:
return true;
case R.id.action_search:
// search action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.activity_viewmore, parent, false);
holder.ivImage = (ImageView) convertView
.findViewById(R.id.grid_image);
holder.tvHeader = (TextView) convertView
.findViewById(R.id.grid_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvHeader.setText(descriptions.get(position));
Picasso.with(this.context).load(imageUrls.get(position)).into(holder.ivImage);
return convertView;
}
private class ViewHolder {
private TextView tvHeader;
private ImageView ivImage;
}
}
You can use intent.putExtra("key", value) methods before starting the viewmore Activity.
Then in the viewmore Activity, you can get these data from the getIntent().get*Extra("key") methods.
Like:
Intent next = new Intent(context, viewmore.class);
next.putExtra("description", descriptions.get(position));
next.putExtra("imageUrl", imageUrls.get(position));
context.startActivity(next);
Then (in viewmore Activity):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_viewmore);
String description = getIntent().getStringExtra("description");
String imageUrl = getIntent().getStringExtra("imageUrl");
// Get the TextView using its ID defined in the layout activity_viewmore.xml
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText(description);
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Picasso.with(this).load(imageUrl).into(imageView);
}