I have a gridview which is supposed to contain an image and a text below it. I am using Picasso to load the images but when I run the app, nothing appears in the imageviews!
Below is my MainActivity.java code:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"C++","VB.NET","JAVA", "JavaScript", "MySQL", "PHP"};
public static String [] prgmImgFiles = {"cpp.png", "vb.net.png", "java.png", "js.png", "mysql.png", "php.png"};
public static Integer [] prgmImages={R.mipmap.img_0, R.mipmap.img_1, R.mipmap.img_2};
public ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(prgmNameList));
public ArrayList<Integer> arrayList2 = new ArrayList<Integer>(Arrays.asList(prgmImages));
public ArrayList<String> arrayList3 = new ArrayList<String>(Arrays.asList(prgmImgFiles));
public static GridViewAdapter gvd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onLoadClick(View v)
{
gv=(GridView) findViewById(R.id.gridView2);
gvd = new GridViewAdapter(this, arrayList, arrayList3);
gv.setAdapter(gvd);
ImageView iv2 = (ImageView)findViewById(R.id.imageViewTest);
// addItem("JavaScript", "http://10.0.2.2/picgal/images/js.png");
Picasso.with(this).load("http://10.0.2.2/picgal/images/java.png").into(iv2);
}
public void addItem(String txt, String ImgID)
{
arrayList.add(txt);
arrayList3.add(ImgID);
gvd.notifyDataSetChanged();
}
}
Below is my GridViewAdapter.java code:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by ITM on 7/4/2016.
*/
public class GridViewAdapter extends BaseAdapter {
ArrayList<String> result;
Context context;
ArrayList<String> imageId;
private static LayoutInflater inflater=null;
public static final String server = "http://10.0.2.2/picgal/images/";
public Holder holder=new Holder();
public GridViewAdapter(MainActivity mainActivity, ArrayList<String> prgmNameList, ArrayList<String> prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
public TextView tv;
public ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView;
rowView = inflater.inflate(R.layout.img_layout, null);
holder.tv=(TextView) rowView.findViewById(R.id.txt);
holder.img=(ImageView) rowView.findViewById(R.id.img);
holder.tv.setText(result.get(position));
Log.i("getView", imageId.get(position));
Picasso.with(context).load(imageId.get(position)).into(holder.img);
// holder.img.setImageResource(imageId.get(position));
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result.get(position) +"\n"+getCount(), Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
The activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.badihbarakat.gridviewapp.MainActivity">
<GridView
android:layout_width="wrap_content"
android:layout_height="250dp"
android:id="#+id/gridView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffe5e5"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:scrollIndicators="left" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
android:id="#+id/btnLoad"
android:layout_below="#+id/gridView2"
android:layout_centerHorizontal="true"
android:onClick="onLoadClick" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageViewTest"
android:layout_below="#+id/btnLoad"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The img_layout.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:orientation="vertical">
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/frame" />
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"
android:textAlignment="center"/>
</LinearLayout>
I have added a test ImageView below the button to test the Picasso object working. It is working fine. Can any one help please.
Thanks
1.You are passing wrong arguments in the gridView's constructor.
gvd = new GridViewAdapter(this, arrayList, arrayList3);
should be
gvd = new GridViewAdapter(this, arrayList, arrayList2);
as stated in your code, arrayList2 consists of Images.
2.In adapter, change the second argument in constructor:
from ArrayList<String> to ArrayList<Integer>
public GridViewAdapter(MainActivity mainActivity, ArrayList<String> prgmNameList, ArrayList<Integer> prgmImages) {
//...
}
After this, try passing it to Picasso.
New :
Can you try the following code :
//server url
String imageURL = ""
//array of images passed in constructor to append with url
String arrayOfImages[] = imageId;
String completeImageURL = server + arrayOfImages[position];
Picasso.with(context)
.load(completeImageURL)
.into(holder.img);
Best ever I used , ImageLoader Library
https://github.com/nostra13/Android-Universal-Image-Loader
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageUri, imageView);
Thanks for all of you who assisted in this question.
Actually, after revising the Picasso code, I discovered that the path to the image file was missing!
What I have put was:
Picasso.with(context).load(imageId.get(position)).into(holder.img);
As imageId.get(position) contains only the file name and not the full path on the server, the Picasso was not able to find the file!
The correct code is:
Picasso.with(context).load(server+imageId.get(position)).into(holder.img);
Where server is a String containing the full path of the images folder.
Thanks again to all of you.
Related
When there is not much data list of ListView, TextView can be seen well. But when there is a lot of data and the List fills the screen, TextView written test disappears. Please see the picture below
enter image description here
enter image description here
Here is the code source.
https://github.com/hardcodingJeon/ExpandList_recyclerView
thankyou!
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private ExpandableListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
ArrayList<GroupItem> DataList = new ArrayList<>();
listView = (ExpandableListView)findViewById(R.id.mylist);
for (int i=1;i<25;i++) {
GroupItem temp = new GroupItem("A"+i);
temp.childItems.add(new GroupItem.ChildItem("1","2"));
temp.childItems.add(new GroupItem.ChildItem("2","3"));
temp.childItems.add(new GroupItem.ChildItem("4","5"));
DataList.add(temp);
}
ExpandAdapter adapter = new ExpandAdapter(getApplicationContext(), R.layout.group_row, R.layout.activity_child, DataList);
listView.setIndicatorBounds(width - 50, width);
listView.setAdapter(adapter);
}
}
ExpandAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ExpandAdapter extends BaseExpandableListAdapter {
private Context context;
private int groupLayout = 0;
private int chlidLayout = 0;
private ArrayList<GroupItem> DataList;
private LayoutInflater myinf = null;
public ExpandAdapter(Context context,int groupLay,int chlidLay,ArrayList<GroupItem> DataList){
this.context = context;
this.groupLayout = groupLay;
this.chlidLayout = chlidLay;
this.DataList = DataList;
this.myinf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
convertView = myinf.inflate(this.groupLayout, parent, false);
}
TextView groupName = (TextView)convertView.findViewById(R.id.groupName);
groupName.setText(DataList.get(groupPosition).groupTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
convertView = myinf.inflate(this.chlidLayout, parent, false);
}
RecyclerView recyclerView = convertView.findViewById(R.id.recyclerView);
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(context,DataList.get(groupPosition).childItems);
LinearLayoutManager mManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mManager);
recyclerView.setAdapter(recyclerAdapter);
return convertView;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return DataList.get(groupPosition).childItems.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 1;
}
#Override
public GroupItem getGroup(int groupPosition) {
// TODO Auto-generated method stub
return DataList.get(groupPosition);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return DataList.size();
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
}
GroupItem.java
import java.util.ArrayList;
public class GroupItem {
public String groupTitle;
public ArrayList<ChildItem> childItems = new ArrayList<>();
public GroupItem(String groupTitle) {
this.groupTitle = groupTitle;
}
public static class ChildItem {
String beforePrice;
String afterPrice;
public ChildItem(String beforePrice, String afterPrice) {
this.beforePrice = beforePrice;
this.afterPrice = afterPrice;
}
}
}
RecyclerAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter {
Context context;
ArrayList<GroupItem.ChildItem> items;
public RecyclerAdapter(Context context, ArrayList<GroupItem.ChildItem> items) {
this.context = context;
this.items = items;
}
//뷰id를 참조함
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.child_item,parent,false);
VH holder = new VH(itemView);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
VH vh = (VH)holder;
GroupItem.ChildItem item = items.get(position);
vh.beforePrice.setText( item.beforePrice );
vh.afterPrice.setText( item.afterPrice );
}
#Override
public int getItemCount() {
return items.size();
}
class VH extends RecyclerView.ViewHolder{
TextView beforePrice;
TextView afterPrice;
Button btn;
public VH(#NonNull View itemView) {
super(itemView);
beforePrice = itemView.findViewById(R.id.child_item_beforePrice);
afterPrice = itemView.findViewById(R.id.child_item_afterPrice);
btn = itemView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, items.get(getLayoutPosition()).beforePrice+"\n"+items.get(getLayoutPosition()).afterPrice, Toast.LENGTH_SHORT).show();
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indicatorRight="?android:attr/expandableListPreferredItemIndicatorRight"
android:id="#+id/mylist" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_child.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="8dp"
app:contentPaddingTop="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<RelativeLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="15dp">
<TextView
android:id="#+id/child_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="월간유니콘\nPLUS 31"
android:textColor="#color/black"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:gravity="center"/>
<TextView
android:id="#+id/child_item_period"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 개월간 매일 2회"
android:layout_below="#id/child_item_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"/>
<LinearLayout
android:id="#+id/child_item_Linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/child_item_period"
android:orientation="vertical"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="정기구독"
android:layout_below="#id/child_item_period"/>
<!--data-->
<TextView
android:id="#+id/child_item_beforePrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="62000원"/>
<!--data-->
<TextView
android:id="#+id/child_item_afterPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="29000/월"/>
</LinearLayout>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="구매"
android:layout_toRightOf="#id/child_item_Linear"
android:layout_below="#id/child_item_period"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
child_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/childName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dip"
android:paddingLeft="40dip"
/>
<CheckBox
android:textColor="#111111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox" />
</LinearLayout>
group_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/groupName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textSize="20dp"
android:paddingLeft="5px"
android:textColor = "#FFA500"
/>
</LinearLayout>
you have set the ExpandableListView height as wrap content. Hence when the no of elements increase it pushes the TextView below the available screen . I would suggest
1.using ConstraintLayout or RelativeLayout
2. Affixing the TextView at the very bottom and then arranging your Expandable list view relative to that
Otherwise set the height of Expandable ListView to a particular predetermined value(Not Adviced) and it should work perfectly
The app crashes as soon as any of the list Activities are launched:
NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter' on a null object reference
My instructor says its happening because the layouts haven't been configured correctly. Right now, the list_item is being inflated as the Activity layout. Please create a separate layout, such as activity_list and add the ListView element there to be referenced in the individual Activity files
Here's the code
MainActivity
package com.example.tourguide;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button event = (Button) findViewById(R.id.events);
Button mall = (Button) findViewById(R.id.malls);
Button resturant = (Button) findViewById(R.id.resturants);
Button university = (Button) findViewById(R.id.uinversities);
event.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent event = new Intent(MainActivity.this, events.class);
startActivity(event); }
});
mall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mall = new Intent(MainActivity.this, malls.class);
startActivity(mall);
}
});
resturant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent resturant = new Intent(MainActivity.this, resturants.class);
startActivity(resturant);
}
});
university.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent university = new Intent(MainActivity.this, universities.class);
startActivity(university);
}
});
}}
WordAdapter.java
package com.example.tourguide;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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 java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<listitem> {
public WordAdapter(Activity context, ArrayList<listitem> listitems) {
super(context, 0, listitems);
}
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
listitem currentItem = getItem(position);
TextView name = (TextView) listItemView.findViewById(R.id.text1);
name.setText(currentItem.getName());
ImageView image = (ImageView) listItemView.findViewById(R.id.image1);
image.setImageResource(currentItem.getImage());
TextView dist = (TextView) listItemView.findViewById(R.id.text2);
dist.setText(currentItem.getDist());
TextView price = (TextView) listItemView.findViewById(R.id.text2);
price.setText(currentItem.getPrice());
return listItemView;
}
}
listitem.java
package com.example.tourguide;
import android.widget.ImageView;
public class listitem {
private String name="";
private int image;
private String dist="";
private String price="";
public listitem(String namea, int imagea, String dista, String pricea){
name = namea;
imagea = image;
dist= dista;
price=pricea;
}
/*********** Set Methods ******************/
public void setName(String name)
{
this.name = name;
}
public void setImage(int image)
{
this.image = image;
}
public void setDist(String dist)
{
this.dist = dist;
}
public void setPrice(String price)
{
this.price = price;
}
/*********** Get Methods ****************/
public String getName()
{
return this.name;
}
public int getImage()
{
return this.image;
}
public String getDist()
{
return this.dist;
}
public String getPrice()
{
return this.price;
}
}
Univerisities.java
package com.example.tourguide;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
public class universities extends Activity {
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.list_item);
ArrayList<listitem> listitemArrayList = new ArrayList<listitem>();
listitemArrayList.add(new listitem("Prince Sultan University", R.drawable.u1, "AlNarjes Dist", "$"));
listitemArrayList.add(new listitem("Princess Nourah University", R.drawable.u2, "AlNarjes Dist", "$"));
listitemArrayList.add(new listitem("King Saud University", R.drawable.u3, "AlNarjes Dist", "$"));
WordAdapter adapter = new WordAdapter(this, listitemArrayList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.example.tourguide.MainActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="29sp"
android:textColor="#3DC195"
android:textAlignment="center"
android:text="Tour Guide App" />
<TextView
android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#6AA13F"
android:textSize="18sp"
android:text="Tour Guide App is designed to help you discover Riyadh city sightseeings including events, malls, resturants and universities.the app will be developed periodically to add more features... stay tuned" />
<Button
android:id="#+id/events"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Events"
android:onClick="onClickEvent"/>
<Button
android:id="#+id/malls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Malls"
android:onClick="onClickMall"/>
<Button
android:id="#+id/resturants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resturants"
android:onClick="onClickRest"/>
<Button
android:id="#+id/uinversities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Universities"
android:onClick="onClickUniv"/>
</LinearLayout>
activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/image1"
android:layout_width="163dp"
android:layout_height="96dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:layout_marginTop="30dp"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#DB0BA4"
android:textSize="20sp"
/>
<TextView
android:id="#+id/text2"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:textColor="#456233"
android:textSize="15sp"
/>
<TextView
android:id="#+id/text3"
android:layout_width="113dp"
android:layout_height="wrap_content"
android:textColor="#C1B03D"
android:textSize="15sp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
In the file Univerisities.java you should change your layout file to activity_list.xml.
Line
setContentView(R.layout.list_item);
Must be changed to :
setContentView(R.layout.activity_list);
Bro,
First you are trying to inflate the ListView item into you activity, which is wrong:
Change the setContentView(R.layout.list_item) to setContentView(R.layout.activity_list);
Also please check out the ViewHolder pattern for ListView's since the your adapter's getView method should be improved by that to make scrolling even more smooth:
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
and if no items are appearing in the list, you should also override the getItemCount on adatpterto return the size of the list.
I've utilized the following code from another StackOverflow post (How could i add a spinner in listview with its listitems by using customadapter?):
package com.android.main;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
public class DemoListSpinnerActivity extends Activity {
ListView _listview;
String[] itemsarray=new String[]{"one","two","three"};
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_listview=(ListView)findViewById(R.id.listView1);
_listview.setAdapter(new CustomAdapter(this));
}
private class CustomAdapter extends BaseAdapter
{
LayoutInflater inflater;
public CustomAdapter(Context context)
{
inflater=LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return 1;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if(convertview==null)
{
convertview=inflater.inflate(R.layout.listrow,null);
viewHolder=new ViewHolder();
viewHolder.spinner=(Spinner)convertview.findViewById(R.id.spinner1);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)convertview.getTag();
}
return convertview;
}
public class ViewHolder
{
Spinner spinner;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<ListView android:layout_height="wrap_content" android:id="#+id/listView1" android:layout_width="match_parent"></ListView>
</LinearLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/spinner1"></Spinner>
</LinearLayout>
While the layouts are obviously different from mine... I have a button that I want to be able to add a new ListViewItem.
And this new Spinner (inside of the ListViewItem) needs to be autopopulated with the String[].
So assuming I have:
FloatingActionButton addButton= (FloatingActionButton) findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Add New Spinner Item
// Populate Spinner with String[]
}
});
I'm looking to add a new spinner item to the ListView, and then populate that list with the string[] earlier defined.
I have a problem with my app. I have a list of items and when I select one item i want to showcase a popup with some text and with a button that will take me to another activity. With the code that i have so far i have the list but is not functional. I need to add the popup and the transition to another activity.Can someone help me with my problem?
This is what i have so far..:
MainActivity:
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
public class Lesson extends Activity {
public static int [] prgmImages= {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,};
public static String[] prgmNameList = {"Ex1","Ex2","Ex3","Ex4","Ex5","Ex6","Ex7","Ex8","Ex9","Ex10","Ex11","Ex12",};
public static String[] prgmDescription = {"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds"};
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson);
context = this;
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages, prgmDescription));
}
}
CustomAdapter.java :
package com.example.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
String [] description;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(Lesson lesson, String[] prgmNameList, int[] prgmImages, String[] prgmDescription){
// TODO Auto-generated constructor stub
result=prgmNameList;
description=prgmDescription;
context=lesson;
imageId=prgmImages;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
TextView tv2;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_field, null);
holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv2.setText(description[position]);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "You Clicked "+result[position], 2000).show();
}
});
return rowView;
}
}
activity_lessons.xml:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Lesson" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_field:
<?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/textView1"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_alignTop="#+id/imageView1"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="200dp"
android:layout_height="18dp"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
You can open alert on item click like this :
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "You Clicked "+result[position], 2000).show();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
Intent intent = new Intent(context, Activity2.class);
context.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
I am newbie in android. I want to create a custom listview arrayadapter. I have followed some tutorial but my emulator shows nothing for the Custom ListView. Can anyone help me figure out where is wrong with my code? Thanks in advance.
Here is my custom_listview_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customListView" />
</LinearLayout>
This is my custom_listview_row.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"
android:gravity="center_vertical"
android:minHeight="64dp">
<!-- <ImageView
android:id="#+id/clv_imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="#string/empty"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/> -->
<TextView
android:id="#+id/clv_textView2"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/tv_definition"
android:textIsSelectable="true" />
<TextView
android:id="#+id/clv_textView"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="#string/tv_word"
android:textIsSelectable="true" />
</RelativeLayout>
Here is my MyPerformanceArrayAdapter.java
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyPerformanceArrayAdapter extends ArrayAdapter<DefinitionObject>{
private List<DefinitionObject> entries;
private Activity activity;
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId, List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_listview_row, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.clv_textView);
holder.item2 = (TextView) v.findViewById(R.id.clv_textView2);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final DefinitionObject custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getWord());
holder.item2.setText(custom.getFav());
}
return v;
}
}
and lastly, this is my Activity named TempCLV.java
package com.example.myidictionary;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
public class TempCLV extends Activity {
private MySQLiteDefinitionHelper db;
String tblName = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
}
public void refresh()
{
db = new MySQLiteDefinitionHelper(this);
final List<DefinitionObject> values = db.getAllWords(tblName);
ListView mylist = (ListView)findViewById(R.id.customListView);
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, R.id.customListView, values);
mylist.setAdapter(adapter);
}
}
You need to call refresh in onCreate since you are setting the adapter to list view there.
Make sure your values has some data
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
refresh();
}
Also you can move the inflater initialization to the constructor of adapter class. No need to initialize in getView
LayoutInflater vi;
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId,List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
vi =(LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Please call refresh method at least for first time to set the the adapter in your MainActivity' onCreate method..