How to show all images in this gallery app? - java

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.

Related

error error: cannot find symbol class GithubRepoAdapter

I'm a beginner to java and android. I'm sure this is a basic question.
I get an error when trying to import a class defined in a file adjacent to the current file. The code is available at https://github.com/morenoh149/HarryLearnsAndroid/blob/master/HttpDemo/app/src/main/java/com/harrymoreno/httpdemo/MainActivity.java#L44
// MainActivity.java
package com.harrymoreno.httpdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.harrymoreno.httpdemo.GitHubRepoAdapter;
public class MainActivity extends AppCompatActivity {
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pagination_list);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
GithubClient client = retrofit.create(GithubClient.class);
Call<List<GithubRepo>> call = client.reposForUser("morenoh149");
call.enqueue(new Callback<List<GithubRepo>>() {
#Override
public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
List<GithubRepo> repos = response.body();
// error error: cannot find symbol class GithubRepoAdapter
listView.setAdapter(new GithubRepoAdapter(MainActivity.this, repos));
}
#Override
public void onFailure(Call<List<GithubRepo>> call, Throwable t) {
Toast.makeText(MainActivity.this, "error :(", Toast.LENGTH_SHORT).show();
}
});
}
}
and
// GitHubRepoAdapter.java
package com.harrymoreno.httpdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class GitHubRepoAdapter extends ArrayAdapter<GithubRepo> {
private Context context;
private List<GithubRepo> values;
public GitHubRepoAdapter(Context context, List<GithubRepo> values) {
super(context, R.layout.list_item_pagination, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_pagination, parent, false);
}
TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text);
GithubRepo item = values.get(position);
String message = item.getName();
textView.setText(message);
return row;
}
}
You have spelling mistake on that error line.
It should be GitHubRepoAdapter, not GithubRepoAdapter.

Word list in a RecyclerView and the favourite section of my app doesn't work properly

I developed a vocabulary app where users can select their favorite words to a favourite section. I have two problems.
First, my code looks okay but when I choose a word from the list it doesn't show in the favorite section immediately. However, when I re-open the application it appears there.
The second problem is with the words which repeat in list with any run.
Here's my code :
Adapter list
package farmani.com.essentialwordsforielts.mainPage;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.innerpage.ActivityInnerPage;
public class AdapterList extends RecyclerView.Adapter<ViewHolder> {
Context context;
LayoutInflater inflater;
TextView title;
ImageView avatar;
LinearLayout cardAdapter;
public AdapterList(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.adapter_card_view, parent, false);
title = (TextView) view.findViewById(R.id.title1);
avatar = (ImageView) view.findViewById(R.id.avatar);
cardAdapter = (LinearLayout) view.findViewById(R.id.card_adapter);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(MainActivity.list.get(position).getWord());
String img = MainActivity.list.get(position).getImg();
int id = MainActivity.context.getResources().getIdentifier(img, "drawable", MainActivity.context.getPackageName());
holder.avatar.setImageResource(id);
holder.cardAdapter.setOnClickListener(clickListener);
holder.cardAdapter.setId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = view.getId();
Intent intent = new Intent (MainActivity.context, ActivityInnerPage.class);
intent.putExtra("name", "list");
intent.putExtra("id", position + "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.context.startActivity(intent);
}
};
#Override
public int getItemCount() {
return MainActivity.list.size();
}
}
Adapter favorite
package farmani.com.essentialwordsforielts.mainPage;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.innerpage.ActivityInnerPage;
public class AdapterFav extends RecyclerView.Adapter<ViewHolder> {
Context context;
LayoutInflater inflater;
TextView title;
ImageView avatar;
LinearLayout cardAdapter;
public AdapterFav(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.adapter_card_view, parent, false);
title = (TextView) view.findViewById(R.id.title1);
avatar = (ImageView) view.findViewById(R.id.avatar);
cardAdapter = (LinearLayout) view.findViewById(R.id.card_adapter);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(MainActivity.favorite.get(position).getWord());
String img = MainActivity.favorite.get(position).getImg();
int id = MainActivity.context.getResources().getIdentifier(img, "drawable", MainActivity.context.getPackageName());
holder.avatar.setImageResource(id);
holder.cardAdapter.setOnClickListener(clickListener);
holder.cardAdapter.setId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = view.getId();
Intent intent = new Intent (MainActivity.context, ActivityInnerPage.class);
intent.putExtra("name", "favorite");
intent.putExtra("id", position + "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.context.startActivity(intent);
}
};
#Override
public int getItemCount() {
return MainActivity.favorite.size();
}
}
When you update your data set call adapater.notifyChange();

I have a video gallery try to get videos files from a specific directory i am getting an error parameters cannot be resolved to a variable

I am making a video gallery trying to get videos files from a specific directory i am getting an error parameters cannot be resolved to a variable here is my code of the gallery activity.I am a newbie to java and my English is not that good.Thanks in advance for helping.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity implements OnItemClickListener {
Cursor cursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String selection=MediaStore.Video.Media.DATA +" like?";
String[] selectionArgs=new String[]{"/storage/sdcard0/phd"};
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");
ListView listView = (ListView) this.findViewById(R.id.ListView);
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID };
String[] mediaColumns = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.MIME_TYPE };
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);
ArrayList<VideoViewInfo> videoRows = new ArrayList<VideoViewInfo>();
if (cursor.moveToFirst()) {
do {
VideoViewInfo newVVI = new VideoViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));
Cursor thumbCursor = managedQuery(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
Log.v("", newVVI.title);
newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
Log.v("", newVVI.mimeType);
videoRows.add(newVVI);
} while (cursor.moveToNext());
}
listView.setAdapter(new VideoGalleryAdapter(this, videoRows));
listView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
int mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE);
String videoFilePath = cursor.getString(fileColumn);
String mimeType = cursor.getString(mimeColumn);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File newFile = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(newFile), mimeType);
startActivity(intent);
}
}
}
class VideoViewInfo {
String filePath;
String mimeType;
String thumbPath;
String title;
}
class VideoGalleryAdapter extends BaseAdapter {
private Context context;
private List<VideoViewInfo> videoItems;
LayoutInflater inflater;
public VideoGalleryAdapter(Context _context,
ArrayList<VideoViewInfo> _items) {
context = _context;
videoItems = _items;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return videoItems.size();
}
public Object getItem(int position) {
return videoItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View videoRow = inflater.inflate(R.layout.row, null);
ImageView videoThumb = (ImageView) videoRow
.findViewById(R.id.ImageView);
if (videoItems.get(position).thumbPath != null) {
videoThumb.setImageURI(Uri
.parse(videoItems.get(position).thumbPath));
}
TextView videoTitle = (TextView) videoRow
.findViewById(R.id.TextView);
videoTitle.setText(videoItems.get(position).title);
return videoRow;
}
}

The method and The constructor is undefined for the type

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.

ArrayAdapter delete an item with a button

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

Categories