I am having a bit of a problem, i created an arrayadapter that works just fine, but when i click in the delete button, it deletes the item but the listview doesn´t change. I now i am missing a bit of code, but I don´t know what it is?
ListarSocio.java
package com.example.polideportivo1;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class ListarSocio extends Activity {
ListView listview;
ArrayAdapterSocio socioArrayAdapter;
ArrayList<Socios>socio = VariablesGlobales.getInstance().getSocios();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_socio_main);
socioArrayAdapter = new ArrayAdapterSocio(ListarSocio.this, R.layout.list_socio, socio);
listview = (ListView)findViewById(R.id.ListaMainSocio);
listview.setItemsCanFocus(false);
listview.setAdapter(socioArrayAdapter);
}
}
ArrayAdapterSocio.java
package com.example.polideportivo1;
import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.Duration;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ArrayAdapterSocio extends ArrayAdapter<Socios>{
Context context;
int layoutResourceId;
ArrayList<Socios>socio = VariablesGlobales.getInstance().getSocios();
public ArrayAdapterSocio(Context context, int layoutResourceId, ArrayList<Socios> soc) {
super(context, layoutResourceId, soc);
this.context= context;
this.layoutResourceId = layoutResourceId;
this.socio = soc;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View item = convertView;
CapturadorSocio CapturadorSocio = null;
if (item == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
item = inflater.inflate(layoutResourceId, parent, false);
CapturadorSocio = new CapturadorSocio();
CapturadorSocio.nombre = (TextView) item.findViewById(R.id.textNombre);
CapturadorSocio.apellido = (TextView) item.findViewById(R.id.textApellido);
CapturadorSocio.documento = (TextView) item.findViewById(R.id.textCI);
CapturadorSocio.sexo = (TextView) item.findViewById(R.id.textSexo);
CapturadorSocio.estadoCivil = (TextView) item.findViewById(R.id.textEstadoCivil);
CapturadorSocio.nacionalidad = (TextView) item.findViewById(R.id.textNacionalidad);
CapturadorSocio.fechaNacimiento = (TextView) item.findViewById(R.id.textNacimiento);
CapturadorSocio.domicilio = (TextView) item.findViewById(R.id.textDomicilio);
CapturadorSocio.localidad = (TextView) item.findViewById(R.id.textLocalidad);
CapturadorSocio.telfijo = (TextView) item.findViewById(R.id.textTelFijo);
CapturadorSocio.telcelular = (TextView) item.findViewById(R.id.textTelCel);
CapturadorSocio.correo = (TextView) item.findViewById(R.id.textCorreo);
CapturadorSocio.edit = (Button) item.findViewById(R.id.btnEdit);
//CapturadorSocio.delete = (Button) item.findViewById(R.id.btnDelete);
item.setTag(CapturadorSocio);
} else {
CapturadorSocio = (CapturadorSocio) item.getTag();
}
Socios socios = socio.get(position);
CapturadorSocio.nombre.setText(socios.obtenerNombre());
CapturadorSocio.apellido.setText(socios.obtenerApellido());
CapturadorSocio.documento.setText(socios.obtenerCI());
CapturadorSocio.sexo.setText(socios.obtenerSexo());
CapturadorSocio.estadoCivil.setText(socios.obtenerEstadoCivil());
CapturadorSocio.nacionalidad.setText(socios.obtenerNacionalidad());
CapturadorSocio.fechaNacimiento.setText(socios.obtenerFechaNacimiento());
CapturadorSocio.domicilio.setText(socios.obtenerDomicilio());
CapturadorSocio.localidad.setText(socios.obtenerLocalidad());
CapturadorSocio.telfijo.setText(socios.obtenerTelefonoFijo());
CapturadorSocio.telcelular.setText(socios.obtenerCelular());
CapturadorSocio.correo.setText(socios.obtenerCorreo());
CapturadorSocio.delete.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
Socios borrarSocio = socio.get(position);
VariablesGlobales.getInstance().getSocios().remove(borrarSocio);
}});
return item;
}
static class CapturadorSocio {
TextView nombre;
TextView apellido;
TextView documento;
TextView sexo;
TextView estadoCivil;
TextView nacionalidad;
TextView fechaNacimiento;
TextView domicilio ;
TextView localidad;
TextView telfijo;
TextView telcelular;
TextView correo;
Button edit;
Button delete;
}
}
When you delete an item you should remove it from your socio list and then call notifyDataSetChanged() on your adapter instance.
Hope that helps
Related
I'm having an issue passing an array into a listview
I can pass an image to the second activty but when I
try to pass an array to the listview I get a lang.NullPointerException error
and the app crashes.
any helpful input would be great Thanks
here is the code
Main Activity
package com.example.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
// Defined the text values to show in ListView
String[] values = new String[]{"Barack Obama","Donald Trump","Bill Clinton",
"Hillary Clinton","Joe Biden"};
// Defined the image ids to show in ListView
Integer[] images = {
R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,
R.drawable.image5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create the custom view
CustomView adapter = new CustomView(MainActivity.this, values, images);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.container);
//set adapter
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if(position == 0){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[]{"Barack Obama","The White House","1-234-567-8900",
"Barack.Obama#whitehouse.com","note"};
Integer image = images[0];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 1){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[] {"Donald Trump","Trump Tower","1-234-567-8901",
"Donald.Trump#whitehouse.com","note"};
Integer image = images[1];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 2){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details = new String[] {"Bill Clinton","New York","1-234-567-8902",
"Bill.Clinton#whitehouse.com","note"};
Integer image = images[2];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 3){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details = new String[] {"Hillary Clinton","New Jersey","1-234-567-8903",
"Hillary.Clinton#whitehouse.com","note"};
Integer image = images[3];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
if(position == 4){
Intent contact = new Intent(MainActivity.this, Details.class);
String[] details= new String[] {"Joe Biden","OHIO","1-234-567-8903",
"Joe.Biden#whitehouse.com","note"};
Integer image = images[4];
contact.putExtra("image", image);
contact.putExtra("details", details);
startActivity(contact);
}
}
});
}
}
Second Activity I want to pass the array to
package com.example.androidlistview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Details extends Activity {
ListView list;
String []web={"Name:","Address:","Phone Number:","Email:","Note:"};
int image;
String[] details;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// create the custom view
CustomList Adapter= new CustomList(Details.this, web, details);//, details should go here
// Get ListView object from xml
list=(ListView)findViewById(R.id.list);
//set adapter
list.setAdapter(Adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
image= bundle.getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);
}
}
}
and the custom view i'm using for the Second Activity details
package com.example.androidlistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomList extends ArrayAdapter<String>{
private final Activity mContext;
private final String[] web;
private final String[] details;
public CustomList(Activity mContext,String[] web, String[] details){ //, String[] details should go here
super(mContext, R.layout.list_single, web);
this.mContext=mContext;
this.web=web;
this.details=details;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = mContext.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText(web[position]);
TextView txtTitle1 = (TextView)rowView.findViewById(R.id.txt1);
txtTitle1.setText(details[position]);
return rowView;
}
}
This is what i did to fix this
Main Activity
package com.example.androidcontacts;
import android.app.ListActivity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity {
ListView mylist;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri allContacts = Uri.parse("content://"+"com.example.androidcontacts.Books" + "/Contact");
Cursor c;
CursorLoader cursorLoader = new CursorLoader(
this,allContacts,null,null,null,null);
c = cursorLoader.loadInBackground();
String[] columns = new String[] {Contact._ID,Contact.TITLE,Contact.AUTHOR,Contact.YEAR,Contact.IMAGE};
int[] views = new int[] {R.id.contactID,R.id.contactName,R.id.contactAuthor,R.id.contactYear,R.id.Image};
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this, R.layout.activity_main,
c, columns, views,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
this.setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
//Intent i=new Intent(MainActivity.this,Details.class);
//startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_search){
Intent intent = new Intent(this, Search.class);
startActivity(intent);
finish();
return true;
}
if(id == R.id.action_add){
Intent intent = new Intent(this, Add.class);
startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Second activity Details
package com.example.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Details extends Activity {
//Variables for Bundle
int image;
String name;
String address;
String phone;
String email;
String note;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Button SMS = (Button) findViewById(R.id.button1);
SMS.setOnClickListener(new OnClickListener() {
//when button1 is clicked open messenger
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phone, null)));
}
});
Button Email = (Button) findViewById(R.id.button2);
Email.setOnClickListener(new OnClickListener() {
//when button2 is clicked open email
#Override
public void onClick(View v) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
startActivity(Intent.createChooser(emailIntent, "Choose Email Client"));
}
});
Button Phone = (Button) findViewById(R.id.button3);
Phone.setOnClickListener(new OnClickListener() {
//when button3 is clicked start call
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)));
}
});
// get details passed from MainActivity
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
image= bundle.getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);
name = bundle.getString("name");
TextView nameView = (TextView) findViewById(R.id.name1);
nameView.setText(name);
address = bundle.getString("address");
TextView addressView = (TextView) findViewById(R.id.address1);
addressView.setText(address);
phone = bundle.getString("phone");
TextView phoneView = (TextView) findViewById(R.id.phone1);
phoneView.setText(phone);
email = bundle.getString("email");
TextView emailView = (TextView) findViewById(R.id.email1);
emailView.setText(email);
note = bundle.getString("note");
TextView noteView = (TextView) findViewById(R.id.note1);
noteView.setText(note);
}
}
}
My Custom View
package com.example.androidlistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends ArrayAdapter<String>{
private final Activity context;
private final String[] values;
private final Integer[] images;
//Constructor
public CustomView(Activity context,String[] values, Integer[] images) {
super(context, R.layout.single_row, values);
this.context = context;
this.values = values;
this.images = images;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText(values[position]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
imageView.setImageResource(images[position]);
return rowView;
}
}
I'm making a gallery application.
I have the code for that task, but I'd like to show all SD internal storage images. Only I can see a specific folder in my application, I understand why. This is a method that I found on the internet.
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.instinctcoder.gridview.R;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
private List<String> imageItems;
private String imagePath="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//location where photo saved
imagePath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";//I'd like to add more Folders, but it doesn't work
imageItems = new ArrayList<>();
getImages();
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.grid_item, imageItems);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imagePath = parent.getAdapter().getItem(position).toString();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
}
});
gridView.setAdapter(gridAdapter);
}
private List<String> getImages(){
new File(imagePath ).mkdirs();
File fileTarget = new File(imagePath);
File[] files = fileTarget.listFiles();
imageItems.clear();
if (files!=null){
for (File file: files){
imageItems.add(file.getAbsolutePath());
}
}
return imageItems;
}
}
GridViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.instinctcoder.gridview.R;
import java.util.ArrayList;
import java.util.List;
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private List<String> data = new ArrayList<String>();
private int resourceId;
private LayoutInflater inflater ;
public GridViewAdapter(Context context, int resourceId, List<String> data) {
super(context, resourceId, data);
this.resourceId = resourceId;
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
ImageView image;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Glide.with(context)
.load("file://" + data.get(position))
.fitCenter()
.centerCrop()
.into(holder.image);
return row;
}
Detailactivity.java
package com.instinctcoder.gridview.Galeria;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.instinctcoder.gridview.R;
public class DetailActivity extends AppCompatActivity {
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
String imagePath = getIntent().getStringExtra("imagePath");
ImageView picturesView = (ImageView) findViewById(R.id.image);
Glide.with(DetailActivity.this)
.load("file://" + imagePath)
.fitCenter()
.centerCrop()
.into(picturesView);
}
}
In manifest I have declared permissions storage, parent activity.
Also, I'm add glide library on gradle.
I have error message like this
The method setContentView(int, FindPeopleFragment) is undefined for the type FindPeopleFragment
The constructor BoxOfficeMoviesAdapter(FindPeopleFragment, ArrayList<BoxOfficeMovie>) is undefined
The constructor Intent(FindPeopleFragment, Class<BoxOfficeDetailActivity>) is undefined
So, what I need to improve on in my code? This my code, I want to display a ListView Box Office in the FindPeopleFragment
BoxOfficeMoviesAdapter.java
import info.androidhive.slidingmenu.R;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class BoxOfficeMoviesAdapter extends ArrayAdapter<BoxOfficeMovie> {
public BoxOfficeMoviesAdapter(Context context, ArrayList<BoxOfficeMovie> aMovies) {
super(context, 0, aMovies);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
BoxOfficeMovie movie = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_box_office_movie, null);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
TextView tvCriticsScore = (TextView) convertView.findViewById(R.id.tvCriticsScore);
TextView tvCast = (TextView) convertView.findViewById(R.id.tvCast);
ImageView ivPosterImage = (ImageView) convertView.findViewById(R.id.ivPosterImage);
// Populate the data into the template view using the data object
tvTitle.setText(movie.getTitle());
tvCriticsScore.setText("Score: " + movie.getCriticsScore() + "%");
tvCast.setText(movie.getCastList());
Picasso.with(getContext()).load(movie.getPosterUrl()).into(ivPosterImage);
// Return the completed view to render on screen
return convertView;
}
}
BoxOfficeMovieDetail.java
import info.androidhive.slidingmenu.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class BoxOfficeDetailActivity extends Activity {
private ImageView ivPosterImage;
private TextView tvTitle;
private TextView tvSynopsis;
private TextView tvCast;
private TextView tvAudienceScore;
private TextView tvCriticsScore;
private TextView tvCriticsConsensus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_office_detail);
// Fetch views
ivPosterImage = (ImageView) findViewById(R.id.ivPosterImage);
tvTitle = (TextView) findViewById(R.id.tvTitle);
tvSynopsis = (TextView) findViewById(R.id.tvSynopsis);
tvCast = (TextView) findViewById(R.id.tvCast);
tvCriticsConsensus = (TextView) findViewById(R.id.tvCriticsConsensus);
tvAudienceScore = (TextView) findViewById(R.id.tvAudienceScore);
tvCriticsScore = (TextView) findViewById(R.id.tvCriticsScore);
// Load movie data
BoxOfficeMovie movie = (BoxOfficeMovie) getIntent().getSerializableExtra(BoxOfficeActivity.MOVIE_DETAIL_KEY);
loadMovie(movie);
}
// Populate the data for the movie
#SuppressLint("NewApi")
public void loadMovie(BoxOfficeMovie movie) {
if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setTitle(movie.getTitle());
}
// Populate data
tvTitle.setText(movie.getTitle());
tvCriticsScore.setText(Html.fromHtml("<b>Critics Score:</b> " + movie.getCriticsScore() + "%"));
tvAudienceScore.setText(Html.fromHtml("<b>Audience Score:</b> " + movie.getAudienceScore() + "%"));
tvCast.setText(movie.getCastList());
tvSynopsis.setText(Html.fromHtml("<b>Synopsis:</b> " + movie.getSynopsis()));
tvCriticsConsensus.setText(Html.fromHtml("<b>Consensus:</b> " + movie.getCriticsConsensus()));
// R.drawable.large_movie_poster from
// http://content8.flixster.com/movie/11/15/86/11158674_pro.jpg -->
Picasso.with(this).load(movie.getLargePosterUrl()).
placeholder(R.drawable.large_movie_poster).
into(ivPosterImage);
}
}
FindPeopleFragment.java
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.JsonHttpResponseHandler;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeActivity;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeDetailActivity;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeMovie;
import info.androidhive.slidingmenu.boxoffice.BoxOfficeMoviesAdapter;
import info.androidhive.slidingmenu.boxoffice.RottenTomatoesClient;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class FindPeopleFragment extends Fragment {
private ListView lvMovies;
private BoxOfficeMoviesAdapter adapterMovies;
private RottenTomatoesClient client;
public static final String MOVIE_DETAIL_KEY = "movie";
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_box_office, this);
lvMovies = (ListView) getView().findViewById(R.id.lvMovies);
ArrayList<BoxOfficeMovie> aMovies = new ArrayList<BoxOfficeMovie>();
adapterMovies = new BoxOfficeMoviesAdapter(this, aMovies);
lvMovies.setAdapter(adapterMovies);
// Fetch the data remotely
fetchBoxOfficeMovies();
setupMovieSelectedListener();
}
private void fetchBoxOfficeMovies() {
client = new RottenTomatoesClient();
client.getBoxOfficeMovies(new JsonHttpResponseHandler() {
#Override
public void onSuccess(int code, JSONObject body) {
JSONArray items = null;
try {
items = body.getJSONArray("movies");
ArrayList<BoxOfficeMovie> movies = BoxOfficeMovie.fromJson(items);
adapterMovies.addAll(movies);
adapterMovies.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void setupMovieSelectedListener() {
lvMovies.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View item, int position, long rowId) {
Intent i = new Intent(FindPeopleFragment.this, BoxOfficeDetailActivity.class);
i.putExtra(MOVIE_DETAIL_KEY, adapterMovies.getItem(position));
startActivity(i);
}
});
}}
Please help me to fix it, thanks
The constructor BoxOfficeMoviesAdapter(FindPeopleFragment,
ArrayList) is undefined
In your Fragment use getActivity() instead of this when you pass context to the constructor.
Change this
adapterMovies = new BoxOfficeMoviesAdapter(this, aMovies);
to
adapterMovies = new BoxOfficeMoviesAdapter(getActivity(), aMovies);
The method setContentView(int, FindPeopleFragment) is undefined for
the type FindPeopleFragment
setContentView(R.layout.activity_box_office, this);
must be removed
setContentview is a method of Activity class not Fragment
Change to
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_box_office,container,false);
// rest of the code
return view;
}
Further getView() returns null
Change this
lvMovies = (ListView) getView().findViewById(R.id.lvMovies);
to
lvMovies = (ListView) view.findViewById(R.id.lvMovies);
The constructor Intent(FindPeopleFragment,
Class) is undefined
Finally Change this
Intent i = new Intent(FindPeopleFragment.this, BoxOfficeDetailActivity.class);
to
Intent i = new Intent(getActivity(), BoxOfficeDetailActivity.class);
You better read the Fragment documentation before going any further.
Getting error on My array Adapter class .The error is on Startavtivity line i dont know why it is its working on other pahes but it shows error on this page .So could you please help me out
ERROR" The Constructor Intent(MyArrayAdapter,Class<Add_new_employee >undefined)
package com.example.employeemanager;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyArrayAdapter extends ArrayAdapter<Student> {
Context context;
int layoutResourceId;
ArrayList<Student> students = new ArrayList<Student>();
public MyArrayAdapter(Context context, int layoutResourceId,ArrayList<Student> studs)
{
super(context, layoutResourceId, studs);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.students = studs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View item = convertView;
StudentWrapper StudentWrapper = null;
if (item == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
item = inflater.inflate(layoutResourceId, parent, false);
StudentWrapper = new StudentWrapper();
StudentWrapper.name = (TextView) item.findViewById(R.id.textName);
StudentWrapper.age = (TextView) item.findViewById(R.id.textAge);
StudentWrapper.address = (TextView) item.findViewById(R.id.textAddr);
StudentWrapper.edit = (Button) item.findViewById(R.id.btnEdit);
StudentWrapper.delete = (Button) item.findViewById(R.id.btnDelete);
StudentWrapper.checkBox = (CheckBox) item.findViewById(R.id.checkBox1);
item.setTag(StudentWrapper);
}
else
{
StudentWrapper = (StudentWrapper) item.getTag();
}
Student student = students.get(position);
StudentWrapper.name.setText(student.getName());
StudentWrapper.age.setText(student.getAge());
StudentWrapper.address.setText(student.getAddress());
StudentWrapper.edit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent
(MyArrayAdapter.this,Add_new_employee.class) );
}
});
StudentWrapper.delete.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
StudentWrapper.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
Toast.makeText(context, "Checkox", Toast.LENGTH_LONG).show();
}
});
return item;
}
protected void startActivity(Intent intent) {
// TODO Auto-generated method stub
}
static class StudentWrapper
{
TextView name;
TextView age;
TextView address;
Button edit;
Button delete;
CheckBox checkBox;
}
}
Inseted of this
startActivity(new Intent
(MyArrayAdapter.this,Add_new_employee.class) );
use this
context.startActivity(new Intent
(context,Add_new_employee.class) );
Note: Make sure you had declared Add_new_employee in android manifest.xml
Try
startActivity(new Intent
(getBaseContext(),Add_new_employee.class) );
I am a beginner programmer so please bear with me. I have been able to create a listview with images in it and thought I could modify it to add a checkbox. However, this setup does not work. Any suggestions to how I may fix my code to create a listview with both an image and checkbox?
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CheckboxList extends ListActivity{
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static CheckBox box0,box1,box2,box3;
static final String[] title = new String[] {
"Box 1", "Box 2",
"Box 3 ","Box 4" };
static final CheckBox[] boxes= new CheckBox[] {box0,box1,box2,box3};
private Integer[] imgid = {
R.drawable.magnum,R.drawable.mg,R.drawable.mk,
R.drawable.mk46};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],boxes[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.check_list,
R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
private class RowData {
protected int mId;
protected String mTitle;
protected CheckBox mCBox;
RowData(int id,String title,CheckBox cbox){
mId=id;
mTitle = title;
mCBox=cbox;
}
#Override
public String toString() {
return mId+" "+mTitle+" "+mCBox;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
CheckBox box = null;
ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.check_list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
box = holder.getcheckbox();
box.setChecked(false);
i11=holder.getImage();
i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private CheckBox cbox = null;
private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public CheckBox getcheckbox() {
if(null == cbox){
cbox = (CheckBox) mRow.findViewById(R.id.checkBox);
}
return cbox;
}
public ImageView getImage() {
if(null == i11){
i11 = (ImageView) mRow.findViewById(R.id.img);
}
return i11;
}
}
} }
Look at this links.it may helpfull......
http://www.vogella.com/articles/AndroidListView/article.html
CustomListview With Images and Checkboxes Example