I have an Android View Pager with five screens with images, autoload pager screen with timer.
When user swipes, it is jumping screens based on timer, and is not loading properly.
I have attached code below.
MainActivity.java
package com.sks.androidviewpager;
import java.util.Timer;
import java.util.TimerTask;
import com.androidsurya.androidviewpager.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends Activity {
int noofsize = 5;
ViewPager myPager = null;
int count = 0;
Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout which is containg viewPager Tag for image
setContentView(R.layout.activity_main);
//ViewPager Adapter to set image
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,noofsize);
myPager = (ViewPager) findViewById(R.id.reviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
// Timer for auto sliding
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(count<=5){
myPager.setCurrentItem(count);
count++;
}else{
count = 0;
myPager.setCurrentItem(count);
}
}
});
}
}, 500, 3000);
}
}
ViewPagerAdapter.java
package com.sks.androidviewpager;
import com.androidsurya.androidviewpager.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewPagerAdapter extends PagerAdapter {
int size;
Activity act;
View layout;
TextView pagenumber1,pagenumber2,pagenumber3,pagenumber4,pagenumber5;
ImageView pageImage;
Button click;
public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
// TODO Auto-generated constructor stub
size = noofsize;
act = mainActivity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return size;
}
#Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.pages, null);
pagenumber1 = (TextView)layout.findViewById(R.id.pagenumber1);
pagenumber2 = (TextView)layout.findViewById(R.id.pagenumber2);
pagenumber3 = (TextView)layout.findViewById(R.id.pagenumber3);
pagenumber4 = (TextView)layout.findViewById(R.id.pagenumber4);
pagenumber5 = (TextView)layout.findViewById(R.id.pagenumber5);
pageImage = (ImageView)layout.findViewById(R.id.imageView1);
int pagenumberTxt=position + 1;
//pagenumber1.setText("Now your in Page No " +pagenumberTxt );
try {
if(pagenumberTxt == 1){
pageImage.setBackgroundResource(R.drawable.android_1);
pagenumber1.setTextColor(Color.RED);
pagenumber2.setTextColor(Color.WHITE);
pagenumber3.setTextColor(Color.WHITE);
pagenumber4.setTextColor(Color.WHITE);
pagenumber5.setTextColor(Color.WHITE);
}
else if(pagenumberTxt == 2){
pageImage.setBackgroundResource(R.drawable.android_2);
pagenumber1.setTextColor(Color.WHITE);
pagenumber2.setTextColor(Color.RED);
pagenumber3.setTextColor(Color.WHITE);
pagenumber4.setTextColor(Color.WHITE);
pagenumber5.setTextColor(Color.WHITE);
}else if(pagenumberTxt == 3){
pageImage.setBackgroundResource(R.drawable.android_3);
pagenumber1.setTextColor(Color.WHITE);
pagenumber2.setTextColor(Color.WHITE);
pagenumber3.setTextColor(Color.RED);
pagenumber4.setTextColor(Color.WHITE);
pagenumber5.setTextColor(Color.WHITE);
}
else if(pagenumberTxt == 4){
pageImage.setBackgroundResource(R.drawable.android_4);
pagenumber1.setTextColor(Color.WHITE);
pagenumber2.setTextColor(Color.WHITE);
pagenumber3.setTextColor(Color.WHITE);
pagenumber4.setTextColor(Color.RED);
pagenumber5.setTextColor(Color.WHITE);
}
else if(pagenumberTxt == 5){
pageImage.setBackgroundResource(R.drawable.android_5);
pagenumber1.setTextColor(Color.WHITE);
pagenumber2.setTextColor(Color.WHITE);
pagenumber3.setTextColor(Color.WHITE);
pagenumber4.setTextColor(Color.WHITE);
pagenumber5.setTextColor(Color.RED);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((ViewPager) container).addView(layout, 0);
return layout;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
// }
}
activity_main.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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/relativeTextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header"
android:padding="5dp" >
<android.support.v4.view.ViewPager
android:id="#+id/reviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
pages.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/android_3"
android:contentDescription="#string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center" >
<TextView
android:id="#+id/pagenumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:onClick="pageOneClick"
android:text=" 1 "
android:textColor="#fff"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/pagenumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:textColor="#fff"
android:text=" 2 "
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/pagenumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:textColor="#fff"
android:text=" 3 "
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/pagenumber4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:textColor="#fff"
android:text=" 4 "
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/pagenumber5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#000"
android:textColor="#fff"
android:text=" 5 "
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
Use
myPager.setCurrentItem(myPager.getCurrentItem()+1);
instead of myPager.setCurrentItem(count);
And you should judge if current item is the last item of this view pager.
Related
This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is 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=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
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 com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I'm trying to display my ViewPager since few times without success.
In fact, my fragment layout (which contains the ViewPager) is cut in two parts with weight.
When I check the preview of the Adapter, I see what I want, which is not the case when the application is launched.
This is the code of my Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_search, container, false);
if (this.getArguments().getString("search") != null) {
final List<Produit> produitList =
HypredDbManager.getDbManager().getProduitDbManager()
.getProduitsByString(this.getArguments().getString("search"));
String product = Integer.toString(produitList.size()) + " ";
if (produitList.size() > 1) {
product += getResources().getString(R.string.products);
} else {
product += getResources().getString(R.string.product);
}
((TextView) rootView.findViewById(R.id.search_nombre_produits)).setText(product);
ArrayList<View> viewArrayList = new ArrayList<>();
for (int i = 0; i < produitList.size(); i++) {
ProduitItem produitItem = new ProduitItem(rootView.getContext(), produitList.get(i));
produitItem.setProduitItemListener(new ProduitItem.produitItemListener() {
#Override
public void onProduitSelected(Produit produit) {
mCallback.changeFragment(produit);
}
#Override
public void ajoutPanier(Produit produit) {
SelectionManager.getInstance().addProductToSelection(rootView.getContext(), ProduitSelectionne.fromProduitAndHistorique(produit, ""));
}
});
viewArrayList.add(produitItem);
}
HypredTableLayout hypredTableLayout = (HypredTableLayout) rootView.findViewById(R.id.search_tablelayout_produit);
hypredTableLayout.addChild(viewArrayList, 2, this);
final ArrayList<Protocole> protocoles = new ArrayList<>();
protocoles.addAll(HypredDbManager.getDbManager().getProtocoleDbManager()
.getProtocolesByString(this.getArguments().getString("search")));
String protocole = Integer.toString(protocoles.size()) + " ";
if (protocoles.size() > 1) {
protocole += getResources().getString(R.string.protocoles);
} else {
protocole += getResources().getString(R.string.protocole);
}
((TextView) rootView.findViewById(R.id.search_nombre_protocoles)).setText(protocole);
Log.d(TAG, "protocoles : " + protocoles.size());
if(protocoles.size()> 0) {
ViewPager viewPagerProtocole = (ViewPager) rootView.findViewById(R.id.search_viewpager_protocole);
ProtocoleAdapterViewPager protocoleAdapterViewPager = new ProtocoleAdapterViewPager(rootView.getContext(), protocoles);
protocoleAdapterViewPager.setClickOnProductListener(new ProtocoleAdapterViewPager.clickOnProductListener() {
#Override
public void appelProduit(long productId) {
mCallback.changeFragment(HypredDbManager.getDbManager().getProduitDbManager().getProduitById(productId));
}
#Override
public void ajouterAllProduit(ArrayList<Produit> produitArrayList) {
for (int i = 0; i < produitArrayList.size(); i++) {
SelectionManager.getInstance().addProductToSelection(getActivity(), ProduitSelectionne.fromProduitAndHistorique(produitArrayList.get(i), ""));
}
}
#Override
public void ajouterProduit(Produit produit) {
SelectionManager.getInstance().addProductToSelection(getActivity(), ProduitSelectionne.fromProduitAndHistorique(produit, ""));
}
});
viewPagerProtocole.setAdapter(protocoleAdapterViewPager);
viewPagerProtocole.setCurrentItem(0);
Log.d(TAG, "protocoleAdapterViewPager : " + protocoleAdapterViewPager.getCount());
Log.d(TAG, "getcurrentitem : " + viewPagerProtocole.getCurrentItem());
}
}
return rootView;
}
The getCount of my Adapter return the right size.
The xml of my Fragment :
<?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="horizontal"
android:baselineAligned="false"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:layout_weight="5"
android:background="#drawable/border_process_organe_right"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.ylly.hypred.custom.MyTextView
android:id="#+id/search_nombre_produits"
style="#style/HypredTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="10sp"
android:gravity="center_horizontal"
android:textColor="#color/hypred_rouge" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp">
<com.ylly.hypred.custom.HypredTableLayout
android:id="#+id/search_tablelayout_produit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#null" />
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="5"
android:background="#color/hypred_vert"
android:gravity="center_horizontal"
android:orientation="horizontal">
<com.ylly.hypred.custom.MyTextView
android:id="#+id/search_nombre_protocoles"
style="#style/HypredTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="10sp"
android:gravity="center_horizontal"
android:textColor="#color/hypred_rouge" />
<android.support.v4.view.ViewPager
android:id="#+id/search_viewpager_protocole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hypred_rouge" />
</LinearLayout>
</LinearLayout>
The code of my Adapter :
package com.ylly.hypred.search.adapters;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ylly.hypred.R;
import com.ylly.hypred.dao.Etape;
import com.ylly.hypred.dao.Produit;
import com.ylly.hypred.dao.Protocole;
import com.ylly.hypred.db.HypredDbManager;
import com.ylly.hypred.process.adapter.AdapterEtape;
import com.ylly.hypred.process.recyclerView.SpacesItemDecoration;
import org.solovyev.android.views.llm.LinearLayoutManager;
import java.util.ArrayList;
/**
* Created by YLLY on 06-10-2015.
*/
public class ProtocoleAdapterViewPager extends PagerAdapter {
private ArrayList<Protocole> protocoleArrayList;
private clickOnProductListener mCallback;
private Context context;
private LayoutInflater inflater;
public interface clickOnProductListener {
void appelProduit(long productId);
void ajouterAllProduit(ArrayList<Produit> produitArrayList);
void ajouterProduit(Produit produit);
}
public ProtocoleAdapterViewPager(Context context, ArrayList<Protocole> protocoles) {
this.protocoleArrayList = new ArrayList<>();
protocoleArrayList.addAll(protocoles);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return this.protocoleArrayList.size();
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
View root = inflater.inflate(R.layout.viewpager_search_container, null);
FrameLayout frameLayout = (FrameLayout) root.findViewById(R.id.viewpager_search_container_container);
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.adapter_view_pager_protocole, null);
TextView labelProtocoleTextView = (TextView) linearLayout.findViewById(R.id.adapter_view_pager_protocole_label_text_view);
RecyclerView produitsRecyclerView = (RecyclerView) linearLayout.findViewById(R.id.adapter_view_pager_protocole_recycler_view);
ImageView imageViewPanierSelectionAll = (ImageView) linearLayout.findViewById(R.id.adapter_view_pager_protocole_panier_rouge);
labelProtocoleTextView.setText(protocoleArrayList.get(position).getName());
produitsRecyclerView.addItemDecoration(new SpacesItemDecoration(0, 0, 0, 10));
produitsRecyclerView.setLayoutManager(new LinearLayoutManager(root.getContext(), LinearLayoutManager.VERTICAL, false));
final ArrayList<Etape> fEtapeArrayList = new ArrayList<>();
if(protocoleArrayList.get(position).getEtapeId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeId()));
}
}
if(protocoleArrayList.get(position).getEtapeTwoId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeTwoId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeTwoId()));
}
}
if(protocoleArrayList.get(position).getEtapeThreeId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeThreeId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeThreeId()));
}
}
if(protocoleArrayList.get(position).getEtapeFourId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeFourId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeFourId()));
}
}
if(protocoleArrayList.get(position).getEtapeFiveId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeFiveId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeFiveId()));
}
}
if(protocoleArrayList.get(position).getEtapeSixId()!= null) {
if (HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeSixId()) != null) {
fEtapeArrayList.add(HypredDbManager.getDbManager().getEtapeDbManager()
.getEtapeById(protocoleArrayList.get(position).getEtapeSixId()));
}
}
final AdapterEtape etapeAdapter = new AdapterEtape(fEtapeArrayList, root.getContext());
etapeAdapter.setClickOnProductListener(new AdapterEtape.clickOnProductListener() {
#Override
public void appelerProduit(long produitId) {
mCallback.appelProduit(produitId);
}
#Override
public void ajouterProduitSelection(Produit produit) {
mCallback.ajouterProduit(produit);
}
});
produitsRecyclerView.setAdapter(etapeAdapter);
imageViewPanierSelectionAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<Produit> produits = new ArrayList<>();
for (int i = 0; i < fEtapeArrayList.size(); i++) {
produits.add((fEtapeArrayList.get(i).getProduit()));
}
mCallback.ajouterAllProduit(produits);
}
});
frameLayout.addView(linearLayout);
collection.addView(root);
return root;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (View) object;
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
public void setClickOnProductListener(clickOnProductListener callback) {
mCallback = callback;
}
}
And his xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" android:orientation="vertical"
android:background="#color/hypred_rouge"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/border_process_protocole_title">
<com.ylly.hypred.custom.MyTextView
android:id="#+id/adapter_view_pager_protocole_label_text_view"
android:layout_width="wrap_content"
custom:font_name="Arial-Bold.ttf"
android:textSize="20sp"
android:textColor="#color/hypred_blanc"
android:gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_process_protocole_corps">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="15dp"
android:id="#+id/adapter_view_pager_protocole_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scrollbarStyle="insideOverlay"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:overScrollMode="never">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<com.ylly.hypred.custom.MyTextView
android:text="#string/selection_produits"
android:textColor="#color/hypred_noir"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<ImageView
android:id="#+id/adapter_view_pager_protocole_panier_rouge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_marginBottom="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/hypred_protocole_panier_rouge"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
I'm sure it's just a stupid mistake but I don't see it >:
Thanks in advance and have a good day !
Check this:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_weight="5"
android:background="#color/hypred_vert"
android:gravity="center_horizontal"
android:orientation="horizontal">
<com.ylly.hypred.custom.MyTextView
android:id="#+id/search_nombre_protocoles"
style="#style/HypredTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="10sp"
android:gravity="center_horizontal"
android:textColor="#color/hypred_rouge" />
<android.support.v4.view.ViewPager
android:id="#+id/search_viewpager_protocole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hypred_rouge" />
</LinearLayout>
In this LinearLayout you have android:orientation="horizontal", but you set to your TextView android:layout_width="match_parent". So, in this case your TextView fill out all LinearLayout and there no free space for your ViewPager.
It seems, that's the reason why ViewPager doesn't appear.
Maybe LinearLayout orientation should be vertical.
This is my actual code
I'm already defined setEmptyview on AsyncTask
DepartureFragmentInter.java
package com.trust.flightboard.fragment;
import java.util.ArrayList;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.trust.flightboard.R;
import com.trust.flightboard.adapter.DepartureAdapter;
import com.trust.flightboard.http.DepartureConnection;
import com.trust.flightboard.model.Departure;
import com.trust.flightboard.ui.DepartureDetailActivity;
public class DepartureFragmentInter extends Fragment implements
SwipeRefreshLayout.OnRefreshListener {
private DepartureAdapter mAdapter;
private ListView mListview;
private TextView mDate,flight,destination,time,gate;
ArrayList<Departure> dList = new ArrayList<Departure>();
private static int REFRESH_TIME_IN_SECONDS = 5;
public SwipeRefreshLayout swipeRefreshLayout;
DepartureConnection rest;
String airport;
public static DepartureFragmentInter newInstance() {
return new DepartureFragmentInter();
}
#Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {
View viewroot = inflater.inflate(R.layout.departure_activity,container, false);
mListview = (ListView) viewroot.findViewById(R.id.listview_maskapai);
mDate = (TextView) viewroot.findViewById(R.id.datetime);
swipeRefreshLayout = (SwipeRefreshLayout);
viewroot.findViewById(R.id.lySwipeRefresh);
flight = (TextView) viewroot.findViewById(R.id.codejudul);
destination = (TextView) viewroot.findViewById(R.id.Bandarajudul);
time = (TextView) viewroot.findViewById(R.id.Timejudul);
gate = (TextView) viewroot.findViewById(R.id.Gatejudul);
airport = getArguments().getString("airport");
Toast.makeText(getActivity(), airport, Toast.LENGTH_LONG).show();
Typeface typeFace =
Typeface.createFromAsset(getActivity().getAssets(),
"HELVETICANEUELTPRO-BD_0.OTF");
setTypeFace(typeFace);
mAdapter = new DepartureAdapter(getActivity());
rest = new DepartureConnection(getActivity());
mListview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int
scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
boolean enable = false;
if (mListview != null && mListview.getChildCount() > 0) {
// check if the first item of the list is visible
boolean firstItemVisible =
mListview.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible =
mListview.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
swipeRefreshLayout.setEnabled(enable);
}
});
initUI();
new setList().execute();
mListview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent in = new
Intent(getActivity(),DepartureDetailActivity.class);
in.putExtra("code", dList.get(position).code_departure);
in.putExtra("airport",dList.get(position).airport);
in.putExtra("time",dList.get(position).time);
in.putExtra("gate",dList.get(position).gate);
in.putExtra("city",dList.get(position).airportcity);
startActivityForResult(in, 100);
getActivity().overridePendingTransition(R.anim.left_in,
R.anim.left_out);
}
});
return viewroot;
}
class setList extends AsyncTask<String, String, String>{
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = "error";
try {
dList = rest.getAirport(airport,"Domestic");
result = "Ok";
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
mAdapter.setArray(dList);
mListview.setEmptyView(getActivity().
findViewById(R.id.emptyElement));
mListview.setAdapter(mAdapter);
}
}
private void initUI() {
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorScheme(android.R.color.black,
android.R.color.white,
android.R.color.black, android.R.color.white);
}
#Override
public void onRefresh() {
// TODO Auto-generated method stub
Log.d("cek", "onRefresh SwipeRefreshLayout");
new setList().execute();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
stopSwipeRefresh();
}
}, REFRESH_TIME_IN_SECONDS * 1000);
}
private void stopSwipeRefresh() {
swipeRefreshLayout.setRefreshing(false);
}
class mDateSetListener implements DatePickerDialog.OnDateSetListener {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
// getCalender();
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
}
}
private void setTypeFace(Typeface typeFace) {
flight.setTypeface(typeFace);
destination.setTypeface(typeFace);
time.setTypeface(typeFace);
gate.setTypeface(typeFace);
}
}
and this my departure_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/lySwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/datetime"
android:layout_below="#+id/rel1" >
<ListView
android:id="#+id/listview_maskapai"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:cacheColorHint="#android:color/transparent"
android:divider="#color/bg_separator"
android:dividerHeight="1dp"
android:paddingLeft="8dp"
android:paddingRight="8dp" />
</android.support.v4.widget.SwipeRefreshLayout>
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/text_color_white"
android:paddingBottom="#dimen/margin_medium" >
<LinearLayout
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_medium"
android:layout_marginRight="#dimen/margin_medium"
android:layout_marginTop="#dimen/margin_small"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/codejudul"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="15"
android:text="FLIGHT"
android:textColor="#59000000"
android:textSize="#dimen/text_size_medium" />
<TextView
android:id="#+id/Bandarajudul"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="DEST"
android:textColor="#59000000"
android:textSize="#dimen/text_size_medium" />
<TextView
android:id="#+id/Timejudul"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="13"
android:text="TIME"
android:textColor="#59000000"
android:textSize="#dimen/text_size_medium" />
<TextView
android:id="#+id/Gatejudul"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="GATE"
android:textColor="#59000000"
android:textSize="#dimen/text_size_medium" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel2"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_below="#+id/rel1"
android:background="#drawable/ic_shd_tab" >
</RelativeLayout>
<FrameLayout
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/emptytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/emptytext"
android:textColor="#color/text_color_white" />
<Button
android:id="#+id/emptybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/emptytext"
android:layout_centerHorizontal="true"
android:text="RELOAD" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
</FrameLayout>
when I load ListView contain zero record empty list view not showed
Any help will appreciate thanks
Well, i wrote simple it will help you
Override onContentChanged() method
Like below:
#Override
public void onContentChanged() {
super.onContentChanged();
// write you code here, when list view empty.
}
Change the following:
mListview.setEmptyView(getActivity().
findViewById(R.id.emptyElement));
mListview.setAdapter(mAdapter);
to
mListview.setAdapter(mAdapter);
mListview.setEmptyView(getActivity(). findViewById(R.id.emptyElement));
Bind the listview first and then set the empty view.
Place the
mListview.setEmptyView(getActivity(). findViewById(R.id.emptyElement));
below
mListview.setAdapter(mAdapter);
Went through many demos and was not able to figure out how to get the imageview work in list view.
Tried the code however the image is not visible in listview. Other details are working fine in the list view
Please help.
Following is the code
The mainactivity has tabhost. Under the second tab listview is present.
listdata is the data that i need in the list.
MyBaseAdapter is for the adapter needed to load data in listview.
MainActivity.java
package com.example.trial;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView list;
String[] dish={"Baby Corn Satay","Dum Aloo","Kadai Paneer","Methi Mutter Malai","Paneer Butter Masala","Saag Panner","Veg Kolhapuri"};
String[] about={"Baby Corn with marinade in a skewer","Baby Poatatoes in a spicy gravy","Kadhai paneer","Methi and peas in malai","Cottage cheese in butter masala","Cottage cheese in gravy","Spicy veg mix"};
String[] dprice={"250","300","350","250","350","250","300"};
Integer[] icon={R.drawable.baby_corn_satay,R.drawable.dum_aloo,R.drawable.kadai_paneer,R.drawable.methi_mutter_malai,R.drawable.paneer_butter_masaala,R.drawable.saag_paneer,R.drawable.veg_kolhapuri};
ArrayList<ListData> myList=new ArrayList<ListData>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=(TabHost) findViewById (R.id.tabhost);
th.setup();
TabSpec specs=th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Beverages");
th.addTab(specs);
list=(ListView)findViewById(R.id.list);
getInList();
list.setAdapter(new MyBaseAdapter(MainActivity.this,myList));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +dish[+ position], Toast.LENGTH_SHORT).show();
}
});
specs=th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Cuisines");
th.addTab(specs);
specs=th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Desserts");
th.addTab(specs);
specs=th.newTabSpec("tag4");
specs.setContent(R.id.tab4);
specs.setIndicator("Tri");
th.addTab(specs);
}
private void getInList(){
for(int i=0;i<7;i++)
{
ListData ld=new ListData();
ld.setAbout(about[i]);
ld.setDish(dish[i]);
ld.setDprice(dprice[i]);
ld.seticon(icon[i]);
myList.add(ld);
}
}
}
MainActivity.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"
tools:context=".MainActivity" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="Tab1"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="TooDeepLayout" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="Tab3"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="Orientation" >
<Button
android:id="#+id/button44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="529dp"
android:layout_marginTop="221dp"
android:text="tab4"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
MyBaseAdapter.java
package com.example.trial;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList=new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context,ArrayList<ListData> myList){
this.myList=myList;
this.context=context;
inflater=LayoutInflater.from(this.context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
#Override
public ListData getItem(int position) {
// TODO Auto-generated method stub
return myList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MyViewHolder mvh;
if(convertView==null)
{
convertView=inflater.inflate(R.layout.dishview,null);
mvh=new MyViewHolder();
convertView.setTag(mvh);
}
else
{
mvh=(MyViewHolder) convertView.getTag();
}
mvh.dish = detail (convertView,R.id.dish,myList.get(position).getDish());
mvh.dprice= detail (convertView,R.id.price,myList.get(position).getDprice());
mvh.about = detail (convertView,R.id.about,myList.get(position).getAbout());
mvh.icon= idetail(convertView,R.id.icon,myList.get(position).geticon());
NumberPicker np = (NumberPicker) convertView.findViewById(R.id.qty);
np.setMaxValue(9);
np.setMinValue(1);
np.setValue(1);
Button b = (Button) convertView.findViewById(R.id.b1);
b.setTag(convertView);
return convertView;
}
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(resId);
iv.setImageResource(resId);
return iv;
}
private TextView detail(View v,int resId,String text)
{
TextView tv=(TextView)v.findViewById(resId);
tv.setText(text);
return tv;
}
private static class MyViewHolder{
TextView about,dish,dprice;
ImageView icon;
}
}
ListData.java
package com.example.trial;
import android.widget.NumberPicker;
public class ListData {
String dish;
String about;
String dprice;
Integer icon;
public String getDish() {
return dish;
}
public void setDish(String dish) {
this.dish = dish;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String getDprice() {
return dprice;
}
public void setDprice(String dprice) {
this.dprice = dprice;
}
public Integer geticon() {
return icon;
}
public void seticon(Integer imageId) {
this.icon = imageId;
}
}
dishview.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" >
<TextView
android:id="#+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_alignLeft="#+id/dish"
android:text="Discription"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/dish"
android:layout_marginLeft="49dp"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:src="#drawable/ic_launcher"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/dish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:layout_marginTop="27dp"
android:layout_toRightOf="#+id/icon"
android:text="Dish name"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/about"
android:layout_marginRight="146dp"
android:layout_toLeftOf="#+id/qty"
android:text="Price"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<NumberPicker
android:id="#+id/qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:ignore="NewApi" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/price"
android:layout_marginLeft="41dp"
android:layout_toRightOf="#+id/qty"
android:text="Add"
tools:ignore="HardcodedText" />
</RelativeLayout>
Kindly help me out.
Thanks in anticipation.
Can you please paste your activity_main.xml? Moreso, it is preferable you use actionbar tabs.
Just changed position of layout and it stopped working
Clean and rebuild. Likely your resource ids in binary XML files and R.java are out of sync.
Now that you've included logcat, java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView in detail() are also indicative of this - the resource ids you're passing to the method are valid TextViews but at least in one case it is referring to an ImageView in binary XML.
the mistake is in MyBaseAdapter.java
simple mistake, parameters mismatched.
insted of
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(resId);
iv.setImageResource(resId);
return iv;
}
we need to change it to
private ImageView idetail(View v,int resId,int icon)
{
ImageView iv=(ImageView) v.findViewById(R.id.icon);
iv.setImageResource(icon);
return iv;
}
I need some help on this issue. When I added items to grid layout, some duplicate items added to container and when scrolling their positions are changing. Here is the XML and Java code.
Layout:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_width="wrap_content" android:paddingTop="20dp" android:layout_height="wrap_content" android:id="#+id/tableRow2" android:layout_centerHorizontal="true" android:background="#color/transparent" android:minHeight="110dp">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/school_logo_default" android:layout_gravity="bottom" android:id="#+id/school_logo"></ImageView>
</TableRow>
<ImageView
android:layout_width="fill_parent"
android:layout_height="90dip"
android:layout_alignParentTop="true"
android:src="#drawable/gradient_top"
android:scaleType="fitXY"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="90dip"
android:layout_alignParentBottom="true"
android:src="#drawable/gradient_bottom"
android:scaleType="fitXY"
/>
<LinearLayout
android:id="#+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="left|top">
<TextView android:layout_height="wrap_content" android:textColor="#color/white" android:layout_weight="1" android:textStyle="bold" android:text="#string/userName" android:layout_width="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="20dip" android:gravity="left|top" android:background="#color/transparent" android:paddingLeft="5dp" android:textSize="16px" android:paddingTop="5dp" android:id="#+id/username"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="20dip"
android:layout_alignParentBottom="true"
android:id="#+id/bottom">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/screen_icon" android:src="#drawable/sl_tools"></ImageView>
</LinearLayout>
<TableRow android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:id="#+id/tableRow1" android:layout_centerInParent="true" android:background="#color/transparent">
<ListView android:columnWidth="120dp" android:id="#+id/gridView1" android:numColumns="4" android:verticalSpacing="10dip" android:gravity="center" android:horizontalSpacing="10dip" android:scrollbars="vertical" android:layout_gravity="center" android:stretchMode="columnWidth" android:background="#color/transparent" android:paddingTop="140dip" android:paddingBottom="80dip"></ListView>
</TableRow>
</RelativeLayout>
</FrameLayout>
Class MyInternet:
package com.explore.home;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.AndroidException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.explore.objects.PInfoItem;
import com.explore.utils.DMS;
import com.explore.utils.Utils;
public class MyInternet extends Activity {
private static final String LOG = "My Internet";
public List<AppInfo> listApps = new ArrayList<AppInfo>();
LayoutInflater inflator;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
inflator = getLayoutInflater();
setContentView(R.layout.myinternet);
TextView tv = (TextView) findViewById(R.id.username);
tv.setText(DMS.userProfile.user_fname + " " + DMS.userProfile.user_lname);
FrameLayout lLayout = (FrameLayout) findViewById(R.id.myinternetlayout);
lLayout.setBackgroundColor(Color.parseColor(DMS.deviceSettings.Background_Color));
try
{
listApps = Utils.getAppsForScreen(getBaseContext(), "myinternet");
}
catch (IOException e)
{
Toast.makeText(getBaseContext(), "Error MyInternet - " +e.getMessage(), Toast.LENGTH_SHORT).show();
}
ImageView iv1 = (ImageView)findViewById(R.id.school_logo);
iv1.setImageBitmap(Utils.getBitmapByURL(DMS.deviceConfig.curriculum_loft_url + DMS.deviceSettings.School_Logo));
GridView gv = (GridView)findViewById(R.id.gridView1);
gv.setAdapter(new ButtonAdapter(this));
}
class MyOnClickListener implements OnClickListener {
private final int position;
public MyOnClickListener(int position) {
this.position = position;
}
public void onClick(View v) {
Log.i(LOG, "handler=" + listApps.get(position).appName);
try {
handler(listApps.get(position));
} catch (AndroidException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void handler(AppInfo appInfo) throws AndroidException, ClassNotFoundException
{
if(appInfo.type==0) // Shell Out APK
{
File realFilePath = new File("/data/data/com.explore.home/files/"+ appInfo.appFileName);
PackageManager pm = getBaseContext().getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo( realFilePath.getAbsolutePath(),0);
//PackageManager pm = getPackageManager();
//Intent il = pm.getLaunchIntentForPackage(appInfo.packageName);
Intent il = pm.getLaunchIntentForPackage(packageInfo.packageName);
//Intent i = new Intent();
il.setAction(Intent.ACTION_MAIN);
il.addCategory(Intent.CATEGORY_LAUNCHER);
il.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
ComponentName cn = new ComponentName(packageInfo.packageName, il.getComponent().getClassName());
il.setComponent(cn);
startActivity(il);
return;
}
if(appInfo.type==1) // Shell Out Hyper Link
{
Uri uriUrl = Uri.parse(appInfo.url);
Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(intent);
}
else {
Toast.makeText(this, "Not Yet Implemented", Toast.LENGTH_LONG).show();
}
}
class ButtonAdapter extends BaseAdapter {
private Context mContext;
// Gets the context so it can be used later
public ButtonAdapter(Context c) {
mContext = c;
}
// Total number of things contained within the adapter
public int getCount() {
return listApps.size();
}
// Require for structure, not really used in my code.
public Object getItem(int position) {
return null;
}
// Require for structure, not really used in my code. Can
// be used to get the id of an item in the adapter for
// manual control.
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TableLayout ll;
if (convertView == null) {
Log.i(LOG, "position=" + position);
Log.i(LOG, "count=" + listApps.size());
for(int i=0; i<listApps.size(); i++) {
Log.i(LOG, "apps=" + listApps.get(i).appName);
}
// if it's not recycled, initialize some attributes
AppInfo appInfo = listApps.get(position);
Log.i(LOG, "app name =" + appInfo.appName);
ll = (TableLayout)inflator.inflate(R.layout.app, null, true);
ImageView iv1 = (ImageView)ll.findViewById(R.id.app_icon);
TextView iv2 = (TextView)ll.findViewById(R.id.app_title);
iv2.setText(appInfo.appName);
if(appInfo.type==0)
{
iv1.setImageBitmap(Utils.getAPKIcon(getApplicationContext(), appInfo));
}else
{
if(appInfo.icon!=null)
{
iv1.setImageBitmap(appInfo.icon);
}
}
iv1.setOnClickListener(new MyOnClickListener(position));
iv2.setOnClickListener(new MyOnClickListener(position));
}
else {
ll = (TableLayout) convertView;
}
return ll;
}
}
}
Please let me know why some duplicate items added instead of actual items. While scrolling their positions have changed.
remove the condition and ready
if (convertView == null)