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"/>
Related
Firstly I know there are hundreds of these questions but the answers have not solved my problem. I suspect it's something small I'm missing, basically I've added a RecyclerView and need to start a new activity when a card is clicked.
I've been following this tutorial and it's been smooth sailing up until this point.
https://guides.codepath.com/android/using-the-recyclerview
This is the Recycler
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bgImg"
android:scaleType="centerCrop"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/RV"
/>
</FrameLayout>
This is the card
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/vendor_image2"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:padding="5dp" />
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/vendor_image2"
>
<TextView
android:id="#+id/vendor_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="#+id/vendor_content2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/vendor_name2"
android:textColor="#color/text_col"
android:paddingBottom="5dp" />
<TextView
android:id="#+id/vendor_suburb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#color/text_col"
android:layout_below="#+id/vendor_name2"
android:layout_marginLeft="10dp" />
<RatingBar
android:id="#+id/MyRating2"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vendor_suburb2"
android:layout_marginLeft="10dp"
android:isIndicator="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:numStars="5"
android:textColor="#android:color/black"
android:rating="3.5"
android:stepSize="0.1" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And finally the Adapter and Handler
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
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.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.iconiccode.android.guestloc8tor.SQL.DatabaseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class VendorRecycleAdapter extends RecyclerView.Adapter<VendorRecycleAdapter.VendorHolder>
{
public List<Vendor> vendorList;
private DatabaseHandler dbHandler = new DatabaseHandler(MyApp.getContext());
private Vendor currentVendor;
int cate;
Activity thiscontext;
public VendorRecycleAdapter(Activity context, int Cate)
{
this.cate=Cate;
this.thiscontext=context;
}
#Override
public VendorHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(thiscontext);
//View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
View view = inflater.inflate(R.layout.vendor_list_cards,parent,false);
return new VendorHolder(view);
}
#Override
public void onBindViewHolder(VendorHolder holder, int position)
{
vendorList = dbHandler.getVendorData(cate);
currentVendor = vendorList.get(position);
// Get the image string from the Vendor object
String vendorImage = currentVendor.getImage();
Log.e("Vendor Adapter Image", vendorImage);
// Get the name string from the Vendor object
String vendorName = currentVendor.getCompanyName();
Log.e("Vendor Adapter Name", vendorName);
// Get the content string from the Vendor object
String vendorContent = currentVendor.getContent();
// Get the location string from the Vendor object
String vendorSuburb = currentVendor.getSuburb();
Log.e("Vendor Adapter Suburb", vendorSuburb);
// Find the TextView with view ID vendor_name
// Display the name of the current vendor in that TextView
holder.Vvendor_name.setText(vendorName);
holder.Vvendor_image.setImageResource(R.drawable.loading);
ImageLoader imageLoader;
DisplayImageOptions options;
imageLoader = ImageLoader.getInstance();
String url = vendorImage;
imageLoader.init(ImageLoaderConfiguration.createDefault(MyApp.getContext()));
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.b4f29385)
.resetViewBeforeLoading(true).cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
.displayer(new FadeInBitmapDisplayer(300)).build();
imageLoader.displayImage(url, holder.Vvendor_image);
// Find the TextView with view ID vendor_content
holder.Vvendor_content.setText(vendorContent);
holder.Vvendor_suburb.setText(vendorSuburb);
}
#Override
public int getItemCount()
{
vendorList = dbHandler.getVendorData(cate);
return vendorList.size();
}
public class VendorHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public ImageView Vvendor_image;
public TextView Vvendor_name,Vvendor_content,Vvendor_suburb;
public RatingBar Vrating;
public VendorHolder(View itemView)
{
//These pull as null
super(itemView);
Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2);
Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2);
Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2);
Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2);
Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2);
}
#Override
public void onClick(View v)
{
// int position = getAdapterPosition();
// currentVendor = vendorList.get(position);
Intent intent = new Intent(thiscontext,VendorDetailsActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
String Category = Integer.toString(cate);
JSONObject message = currentVendor.getAllData();
try {
intent.putExtra("IMAGE", message.getString("IMAGE"));
intent.putExtra("URL", message.getString("URL"));
intent.putExtra("CONTENT", message.getString("CONTENT"));
intent.putExtra("COMPANY_NAME", message.getString("COMPANY_NAME"));
intent.putExtra("TEL", message.getString("TEL"));
intent.putExtra("BOOKING_URL", message.getString("BOOKING_URL"));
intent.putExtra("LAT", message.getString("LAT"));
intent.putExtra("LON", message.getString("LON"));
intent.putExtra("STREET", message.getString("STREET"));
intent.putExtra("SUBURB", message.getString("SUBURB"));
intent.putExtra("PROVINCE", message.getString("PROVINCE"));
intent.putExtra("CITY", message.getString("CITY"));
intent.putExtra("CAT",Category);
} catch (JSONException json)
{
Log.e("ERROR", json.getMessage());
}
thiscontext.startActivity(intent);
}
}
}
Any help would be greatly appreciated.
Thanks
I think you just forgot to set the onClickListener in your ViewHolder constructor:
public VendorHolder(View itemView)
{
//These pull as null
super(itemView);
Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2);
Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2);
Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2);
Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2);
Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2);
// If it should be triggered only when clicking on a specific view, replace itemView with the view you want.
itemView.setOnClickListener(this);
}
I am using GridView first time. Everything is fine but the image height is less. I searched a lot but couldn't get the method of increasing the height of image through XML. Here is my code :
activity_select_images.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fillViewport="false">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectImagesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:text="Please Select the pictures you like"
android:id="#+id/selectTextview"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_below="#+id/selectTextview"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<Button
android:id="#+id/submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="#ffffff"
android:layout_below="#+id/frameLayout"
android:background="#drawable/my_button"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</RelativeLayout>
</ScrollView>
gridview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/thumbImage"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbImage"
android:layout_alignLeft="#+id/thumbImage"
android:layout_alignStart="#+id/thumbImage"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginBottom="61dp" />
</RelativeLayout>
SelectImagesActivity.java
package com.houssup.userapp;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class SelectImagesActivity extends AppCompatActivity {
private int count = 0;
Button btn;
private Drawable marker, marker1;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_images);
GridView imagegrid = (GridView) findViewById(R.id.gridView);
btn = (Button) findViewById(R.id.submit);
marker = getResources().getDrawable(R.drawable.pic_five);
marker1 = getResources().getDrawable(R.drawable.pic_two);
imagegrid.setAdapter(new ImageAdapter(this, marker, marker1));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (count < 5)
Toast.makeText(SelectImagesActivity.this, "Please " +
"select total 5 images", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private Drawable drawable1, drawable2;
private LayoutInflater mInflater;
int positionID[];
public ImageAdapter(Context context, Drawable drawable1,
Drawable drawable2) {
this.context = context;
this.drawable1 = drawable1;
this.drawable2 = drawable2;
}
public int getCount() {
return 6;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.gridview_layout, null);
ImageView imageView = (ImageView) gridView.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.itemCheckBox);
if (position < 5) {
imageView.setImageDrawable(marker);
} else {
imageView.setImageDrawable(marker1);
}
// set image based on selected text
} else {
gridView = (View) convertView;
}
return gridView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
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 have an issue with adapter adding data to listview on button click.
I have a activity called createteam that has name and email edittext and a createplayer button. On click createplayer button, the name and email should be added to list view.
I want to use an adapter for doing that(so that I can learn).
Update:
Onclicking the button the the first entry gets added but when I fill in the
edittexts again, the listview does get updated. I am notifying the adapter when I update
the data but only first entry gets and remains added to list view
create_team.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/teamname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Team Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<Button
android:id="#+id/addplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:text="Add Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<LinearLayout
android:id="#+id/view_addplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/createplayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/red"
android:text="Create Player"
android:layout_gravity="center"
android:textStyle="bold">
</Button>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Name"
android:gravity="center"
android:textStyle="bold">
</EditText>
<EditText
android:id="#+id/email"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/blue"
android:hint="Email "
android:textStyle="bold"
android:gravity="center">
</EditText>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>
add_player_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView android:id="#+id/name"
android:textSize="20sp"
android:textStyle="bold"
android:color="#color/blue"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<TextView android:id="#+id/email"
android:textSize="15sp"
android:textStyle="bold"
android:color="#color/mustard"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
CreateTeamActivity.java
package com.recscores.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.Toast;
import com.recscores.android.PlayerInfo;
public class CreateTeamActivity extends ListActivity {
Button createPlayer;
EditText Email;
EditText Name;
private ArrayList<PlayerInfo> newList;
private PlayerAddAdapter newAdpt;
private int i = 0;
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_team);
newList = new ArrayList<PlayerInfo>();
// CREATE TEAM
getWindow().setBackgroundDrawableResource(R.drawable.app_background2);
createPlayer = (Button)findViewById(R.id.createplayer);
Email = (EditText)findViewById(R.id.email);
Name = (EditText)findViewById(R.id.name);
lv = (ListView)findViewById(android.R.id.list);
newAdpt = new PlayerAddAdapter(CreateTeamActivity.this,android.R.id.list,newList);
lv.setAdapter(newAdpt);
createPlayer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// newList = new ArrayList<PlayerInfo>();
PlayerInfo info = new PlayerInfo();
info.SetName(Name.getText().toString());
info.SetEmail(Email.getText().toString());
newList.add(info);
newAdpt.notifyDataSetChanged();
//Thread.sleep(2000);
Name.setText("");
Email.setText("");
}
});
}
}
PlayerAddAdapter.java
package com.recscores.android;
import java.util.ArrayList;
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 PlayerAddAdapter extends ArrayAdapter<PlayerInfo> {
private Activity activity;
//private final Context mContext;
private ArrayList<PlayerInfo> items;
public PlayerAddAdapter(Activity a, int textViewResourceId, ArrayList<PlayerInfo> items){
super(a,textViewResourceId,items);
this.items=items;
this.activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView email;
}
#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);
vi.inflate(R.layout.add_player_listview, null);
holder = new ViewHolder();
holder.name = (TextView)v.findViewById(R.id.name);
holder.email = (TextView)v.findViewById(R.id.email);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final PlayerInfo custom = items.get(position);
if (custom != null) {
**holder.name.setText(custom.GetName());
holder.email.setText(custom.GetEmail());
}
return v;
}
}
PlayerInfo.java
package com.recscores.android;
public class PlayerInfo {
private String name="";
private String email="";
public void SetName(String name){
this.name = name;
}
public String GetName(){
return this.name;
}
public void SetEmail(String email){
this.email = email;
}
public String GetEmail(){
return this.email;
}
}
LayoutInflater vi =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.add_player_listview, null);
Change your code like this. You are not setting values to v variable.
onClick of your button call list.notifyDataSetChanged(). It will refresh your list.
Incase if doesn't work just check getCount() of your adapter what value it is returning. If it is returning 0 your list will not show any data even you call list.notifyDataSetChanged().
Make an arrayList in your adapter and add new items into that. and from getCount() return size of arrayList.
Hi i am developing android application. Now at this stage i am developing calendar which is in week view. For doing that i have use grid view.In grid view there is 7 columns per week days. Each column have one textview which display name of the week day.Now i want to display different week days in each column.But I don't know how to achieve this as i am new in this field. i am using custom adapter for this.Here is code Any help is appreciated.
Here on 6 in want to display Name of week days.
package com.gridlayoutdemo;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class GridLayoutDemo extends Activity {
/** Called when the activity is first created. */
public String[] weekdays={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
Context context;
public MyAdapter(Context context) {
this.context = context;
}
public int getCount() {
return 7;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater li = LayoutInflater.from(context);
convertView = li.inflate(R.layout.main1, null);
holder = new ViewHolder();
holder. tvWeekDay = (TextView)convertView.findViewById(R.id.tvWeekDayName);
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
holder.tvWeekDay.setText(weekdays[position]);
}
}
// holder.tvWeekDay.setText(Integer.toString(Calendar.MONTH));
TextView tvDate = (TextView)convertView.findViewById(R.id.tvDate);
TextView tvNotes = (TextView)convertView.findViewById(R.id.tvNotes1);
convertView.setTag(holder);
//tvWeekday.setText(c.get(Calendar.MONTH));
tvNotes.setText(" Click here for new Note");
tvNotes.setTextSize(10);
tvNotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" Click is successful");
}
});
TextView tv2 = (TextView)convertView.findViewById(R.id.tvNotes2);
tv2.setText(" Click here for new Note" );
tv2.setTextSize(10);
tv2.setLayoutParams(new LayoutParams(140, 200));
tv2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("1.Click is successful");
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
return convertView;
}
static class ViewHolder {
TextView tvWeekDay;
}
}
//main1.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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/LlHeader"
android:background="#drawable/calendar_top_header">
<TextView
android:id="#+id/tvWeekDayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="Friday"/>
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="25-NOV-2011"/>
</LinearLayout>
<TextView android:id="#+id/tvNotes1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:id="#+id/tvNotes2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#f5f5f5"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:background="#drawable/calendar_top_header">
<ImageView
android:id="#+id/imgleftarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src = "#drawable/cal_left_arrow_off">
</ImageView>
<TextView
android:id="#+id/tvMonthName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginLeft="80dp"
android:singleLine="true"
android:textColor="#FFFFFF"
android:text="November"/>
<ImageView
android:id="#+id/imgrightarrow"
android:layout_width="wrap_content"
android:layout_marginLeft="95dp"
android:layout_height="wrap_content"
android:src = "#drawable/cal_right_arrow_off">
</ImageView>
<!-- </LinearLayout> -->
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:numColumns="2"
android:layout_height="fill_parent"
android:background="#f5f5f5"
/>
</LinearLayout>