Implement custom Android grid view - java

I have made a custom gridView and a item witch I want to implement in the gridView, but how can I set these custom gridView onCreate inside my HomeActivity class file?
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// how can I say here that I want to set the gridView from list_holder.xml
}
}
list_holder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_item_block_holder"
android:layout_alignParentEnd="false"
android:numColumns="auto_fit"
tools:listitem="#layout/item_block" />
</RelativeLayout>
item_block.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_block"
android:padding="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#color/secondaryBackgroundColor">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/secondaryBackgroundColor"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView2"
android:background="#drawable/no_image_available" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/secondaryBackgroundColor"
android:padding="10dp"
android:layout_margin="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="video title"
android:id="#+id/item_block_title"
android:textColor="#color/textTitleColor"
android:textStyle="bold"
android:textSize="15dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/item_block_description"
android:textColor="#color/textContentColor"
android:textSize="10dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12-09-16"
android:id="#+id/item_date"
android:textColor="#color/textContentColor"
android:textSize="10dp"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

HomeActivity.java
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Here Adapter like
AdapterDemoGrid adapterDemoGrid = new AdapterDemoGrid(this);
gridView.setAdapter(adapterDemoGrid);
}
}
Here, Adapter for Custom GridView Make this class :
public class AdapterDemoGrid extends BaseAdapter {
private final Context context;
private LayoutInflater mLayoutInflater;
public AdapterDemoGrid(Context context) {
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//lastPosition = -1;
}
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
//The view is not a recycled one: we have to inflate
view = mLayoutInflater.inflate(R.layout.item_block.xml, viewGroup, false);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view;
}
class ViewHolder {
/// Item Block views goes here...
}
}
Hopefully it will help you !!

Related

Android Studio: W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED

I am trying to make a movie list, but it only shows a white screen with the app name. Apparently, I have a very similar program to this and it runs perfectly fine. This is what I get when I run the program.
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
I found someone saying that this error happens if there is an error in xml codes, but I will still put my java codes as well.
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private Context mContext;
ArrayList<Movie> movieList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
final ArrayList<Movie> movieList = Movie.getMoviesFromFile("movies.json", this);
MovieAdapter adapter = new MovieAdapter(this, movieList);
mListView = findViewById(R.id.movie_list_view);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Movie selectedMovie = movieList.get(i);
Intent detailIntent = new Intent(mContext, MovieDetailActivity.class);
detailIntent.putExtra("title", selectedMovie.title);
detailIntent.putExtra("description", selectedMovie.description);
detailIntent.putExtra("poster", selectedMovie.poster);
startActivity(detailIntent);
}
});
}
The following is my adapter.
public class MovieAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Movie> mMovieList;
private LayoutInflater mInflater;
public MovieAdapter(Context mContext, ArrayList<Movie> mMovieList){
this.mContext = mContext;
this.mMovieList = mMovieList;
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount(){return mMovieList.size();}
#Override
public Object getItem(int pos){return mMovieList.get(pos);}
#Override
public long getItemId(int pos){return pos;}
#Override
public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item_movie, parent, false);
holder = new ViewHolder();
holder.titleTextView = convertView.findViewById(R.id.title);
holder.charactersTextView = convertView.findViewById(R.id.main_characters);
holder.descriptionTextView = convertView.findViewById(R.id.description);
holder.thumbnailImageView = convertView.findViewById(R.id.poster);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
TextView titleTextView = holder.titleTextView;
TextView descriptionTextView = holder.descriptionTextView;
TextView charactersTextView = holder.charactersTextView;
ImageView thumbnailImageView = holder.thumbnailImageView;
Movie movie = (Movie)getItem(pos);
titleTextView.setText(movie.title);
titleTextView.setTextSize(20);
charactersTextView.setText(movie.main_characters);
charactersTextView.setTextSize(13);
descriptionTextView.setText(movie.description);
descriptionTextView.setTextSize(9);
Picasso.with(mContext).load(movie.poster).into(thumbnailImageView);
return convertView;
}
private static class ViewHolder{
public TextView titleTextView;
public TextView descriptionTextView;
public ImageView thumbnailImageView;
public TextView charactersTextView;
}
}
And the followings are the xml codes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.junepyolee_miniapp1.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/movie_list_view" />
list_item_movie.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/poster"
android:layout_width="90dp"
android:layout_height="140dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="4dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:contentDescription="This is a thumbnail"
app:srcCompat="#mipmap/ic_launcher" />
<RelativeLayout
android:id="#+id/movie_list_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/poster"
android:layout_alignParentTop="false"
android:layout_toRightOf="#+id/poster">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:text="Title"
android:textSize="20sp" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:maxLines="3"
android:text="description"
android:textSize="9sp" />
<TextView
android:id="#+id/main_characters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/description"
android:layout_below="#+id/description"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:text="characters max 3 people"
android:textSize="13sp" />
<TextView
android:id="#+id/hasSeen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/main_characters"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:text="has seen?"
android:textSize="11sp" />
</RelativeLayout>
Does anyone have a solution for this one?
This is just a warning, and since it retries without the configuration option without (I am assuming from your comments) a followup error, this is not the source of your problem.
It's not a bad guess, since this is for an OpenGL-related class (android/opengl/EGL14), but it's not the source of your problem.
You can read more about EGL at https://www.khronos.org/egl.

custom listview on fragment - can't use getActivity()

I am trying to add delete button next to my item details basing on this answer.
I tried it on a new project, it works perefect:
and when I click delete, I delete the item.
sadly, I tried to use it in my own project when my listView is in calendar_tab.xml. calendar_tab uses CompactCalendarTab.java - fragment class.
so Android Studio errored:
E:\Downloads\MyCustomAdapter.java
Error:(49, 63) error: cannot find symbol method getSystemService(String)
I tried to change
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
to
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
but with no luck.
custom_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
calendar_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/calendar_tab"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbar_calendar"
android:background="#color/teal_300"
android:layout_alignParentTop="true"
android:padding="10sp"
android:layout_alignParentStart="true">
<ImageButton
android:id="#+id/back_button"
android:src="#mipmap/ic_arrow_back_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="goBackmain"
/>
<ImageButton
android:id="#+id/next_button"
android:src="#mipmap/ic_keyboard_arrow_left_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/showdate"
android:layout_toStartOf="#+id/showdate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#color/black"
android:id="#+id/showdate"
android:layout_alignBaseline="#+id/prev_button"
android:layout_alignBottom="#+id/prev_button"
android:layout_centerHorizontal="true" />
<ImageButton
android:id="#+id/prev_button"
android:src="#mipmap/ic_keyboard_arrow_right_black_24dp"
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/showdate"
android:layout_toEndOf="#+id/showdate" />
</RelativeLayout>
<com.github.sundeepk.compactcalendarview.CompactCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/compactcalendar_view"
android:layout_width="fill_parent"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="#null"
app:compactCalendarTextColor="#color/blue_grey_700"
app:compactCalendarCurrentSelectedDayBackgroundColor="#color/teal_300"
app:compactCalendarCurrentDayBackgroundColor="#color/teal_600"
app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator"
app:compactCalendarEventIndicatorStyle="small_indicator"
app:compactCalendarOtherMonthDaysTextColor="#534c4c"
app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true"
android:layout_below="#+id/toolbar_calendar"
/>
<ListView
android:id="#+id/bookings_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/compactcalendar_view"
>
</ListView>
my fragment:
public class CompactCalendarTab extends Fragment {
final ListView bookingsListView = (ListView) v.findViewById(R.id.bookings_listview);
adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mutableBookings);
final ArrayList<String> list = new ArrayList<>();
final MyCustomAdapter adapter = new MyCustomAdapter(list, this);
bookingsListView.setAdapter(adapter);
compactCalendarView = (CompactCalendarView)
v.findViewById(R.id.compactcalendar_view);
}
my custom adapter:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private CompactCalendarTab context;
public MyCustomAdapter(ArrayList<String> list, CompactCalendarTab context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_listview, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
}
Change this line
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
To this
LayoutInflater inflater = LayoutInflater.from(parent.getContext());

Recycler View and Card View not displaying cards

This is my Activity:
public String id;
public String passPhrase;
public ArrayList<SongCell> songs;
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_view_layout);
Context context = this.getApplicationContext();
Bundle stateData = getIntent().getExtras();
try
{
id = stateData.getString("id");
passPhrase = stateData.getString("passPhrase");
ArrayList data;
recyclerView = (RecyclerView) findViewById(R.id.song_list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<SongCell>();
for (int i=0; i<5;i++)
{
data.add(new SongCell("Song "+i,"Artist "+i, null));
}
myAdapter = new MyAdapter(data);
recyclerView.setAdapter(myAdapter);
}
catch(Exception e)
{
Log.d("Error: ", e.toString());
}
}
card.xml
<android.support.v7.widget.CardView android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/song_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/song_name"
android:textSize="30sp"
android:text="Song Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/vote_button"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="#drawable/arrow" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#2d2d2d">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:layout_marginBottom="10dp"
android:background="#222222"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/featSongImage1"
android:contentDescription="#string/newsongimage"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/songname"
android:id="#+id/featSongName1" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/featSongButt1"
android:background="#drawable/arrow"
android:contentDescription="#string/votearrow" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="fill_horizontal"
android:gravity="fill_horizontal|center">
<android.support.v7.widget.RecyclerView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
And this is my custom adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<SongCell> data;
public MyAdapter(ArrayList<SongCell> dataI)
{
data = dataI;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_card,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int pos) {
viewholder.songName.setText(data.get(pos).getName());
viewholder.artist.setText(data.get(pos).getArtist());
viewholder.image.setImageBitmap(data.get(pos).getArtwork());
}
#Override
public int getItemCount() {
return 0;
}
#Override
public int getItemViewType(int position) {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView songName;
TextView artist;
ImageView image;
ImageButton voteButton;
public ViewHolder(View v)
{
super(v);
this.songName = (TextView) v.findViewById(R.id.song_name);
this.image = (ImageView) v.findViewById(R.id.song_photo);
this.voteButton = (ImageButton) v.findViewById(R.id.vote_button);
}
}
}
I have looked at a ton of guides on how to get this working but it still doesn't work. Anyone have any clue what's going on? I'm on version 25.0.1, and have all the modules imported. But it's not adding a single card into the layout.
I thought it would be difficult to debug such a big code you uploaded. But luckily my eye went on this line
#Override
public int getItemCount() {
return 0;
}
return 0; in adapter.
Your list size is always 0.
instead write this
#Override
public int getItemCount() {
return data.size();
}
EDIT-1:
You will also get null pointer exception here
viewholder.artist.setText(data.get(pos).getArtist());
Because you are not initializing the artist variable in ViewHolder class.
Edit-2
#Override
public int getItemCount() {
return 0;
}
you can remove this particular code from your adapter. Because overriding the methods of class that we don't use might sometimes result in errors that you can't even imagine.

Android: setOnItemClickListener doesn't work

I'm attempting to implement a `setOnItemClickListener however it never seems to fire for some reason. The most common fix for this seems to be adding:
android:focusable="false"
android:focusableInTouchMode="false"
`
...however I still cannot seem to reach the setOnItemClickListener or fire the toast within it.
Any suggestions are appreciated.
Java Source:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preinstall_activity);
mContext = PreinstallActivity.this;
//fetchLinks(final UserData userData);
fetchLinks();
gridView = (GridView) findViewById(R.id.gridView2);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(PreinstallActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
XML Source:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".PreinstallActivity">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#color/primaryColor"
android:layout_width="match_parent"
android:paddingRight="10dp"
android:layout_height="wrap_content"
app:theme="#style/FreeMo.ToolBarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<ImageView
android:id="#+id/action_bar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|start"
android:background="#drawable/ic_toggle"
/>
<TextView
android:id="#+id/headline_text"
android:paddingLeft="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/rec_apps_title"
android:textColor="#color/edit_text"
android:textSize="#dimen/headline_text_size" />
<ImageView
android:id="#+id/user_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|end"
android:background="#drawable/ic_menu"></ImageView>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/rec_apps_txt"
android:padding="12dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/rec_apps_txt"
android:textColor="#color/text_view"
android:textSize="#dimen/default_text_size" />
<GridView
android:layout_height="wrap_content"
android:id="#+id/gridView2"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="0dp">
</GridView>
</LinearLayout>
XML Source 2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp">
<ImageView
android:layout_height="64dp"
android:id="#+id/imageView1"
android:layout_width="64dp"
android:src="#drawable/ic_lock_handle"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="0dp"
android:layout_centerHorizontal="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="10sp"
android:ellipsize="marquee"></TextView>
</RelativeLayout>
Java Source 2:
public class PreinstallAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<String> listFlag;
private Activity activity;
public PreinstallAdapter(Activity activity, ArrayList<String> listCountry, ArrayList<String> listFlag) {
super();
this.listCountry = listCountry;
this.listFlag = listFlag;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.preinstall_grid, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listCountry.get(position));
Picasso.with(activity).load(listFlag.get(position)).into(view.imgViewFlag);
// view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
}
1.make sure that your Gridview have width and height.
2.add
android:focusable="false"
android:focusableInTouchMode="false"
to the LinearLayout . like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:focusable="false"
android:focusableInTouchMode="false"
```````````````````````````````````>

The ListView from Fragment extending ListFragment is repeating itself. Any idea what's wrong?

This is the activity
public class Homepage extends FragmentActivity implements ChatFragment.OnFragmentInteractionListener{
SearchFragment searchFragment;
ChatFragment chatFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
searchFragment = new SearchFragment();
chatFragment = new ChatFragment();
if(savedInstanceState == null){
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}
}
#Override
protected void onResume(){
Bundle bundle = getIntent().getExtras();
if(bundle != null){
//text.setText("user_id: " + bundle.get("user_id") + " ,username: " + bundle.get("username") + " ,password: " + bundle.get("password"));
}
super.onResume();
}
#Override
public void onChatFragmentInteraction(Uri uri){
}
public void openSearchFragment(View view){
if(!searchFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,searchFragment).commit();
}
public void openChatFragment(View view){
if(!chatFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}
public void openProfile(View view){
}
}
This is the layout of the activity
I like making my activities screen responsive.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="the_activity_location"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_weight="90"
android:orientation="vertical">
<fragment
android:id="#+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="#layout/fragment_search"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:backgroundTint="#color/whiteClr"
android:background="#color/whiteClr"
android:layout_weight="10"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search_img"
android:background="#null"
android:layout_weight="25"
android:layout_marginLeft="0dp"
android:layout_gravity="center"
android:onClick="openSearchFragment"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chat_img"
android:background="#null"
android:layout_weight="50"
android:layout_gravity="center"
android:onClick="openChatFragment"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/profile_img"
android:background="#null"
android:layout_weight="25"
android:layout_gravity="center"
android:onClick="openProfile"/>
</LinearLayout>
</LinearLayout>
This is the fragment
public class SearchFragment extends ListFragment implements AdapterView.OnItemClickListener{
ArrayList<String> list;
ListViewAdapter myAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
View view = inflater.inflate(R.layout.fragment_search,container,false);
list = new ArrayList<String>();
list.add("I");
list.add("am");
list.add("Iron");
myAdapter = new ListViewAdapter(list,getActivity().getBaseContext());
setListAdapter(myAdapter);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
}
The Layout of the fragment is inside a linear layout consisting of a
ListView with "#android:id/list" as the ID and a TextView.
Don't worry about the adapter. it's working fine.. I tested it out on an activity. Works perfectly fine.
This is what it looks like now
It is correct. You have two SearchFragment. One declared static in your layout, the other added through a FragmentTransaction. Fragments declared static in the layout can't be replaced using a programmatically transaction. Just replace
<fragment
android:id="#+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="#layout/fragment_search"/>
with
<FrameLayout
android:id="#+id/homepageFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
and it is going to work as you expect

Categories

Resources