Different Column name in grid view in android - android

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>

Related

Android ImageButton and Spinner not working

When user clicks on icon adjacent to the item in spinner, some action has to be performed. i have not added functionality for button click handler. Before that i am facing below issue. For button with icon, i have used ImageButton in Spinner.
But when item in spinner is selected, all the items are shown and list down is not closing.
I am using Android api 28 to test.
MainActivity.java
package com.example.ravispinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<FileItem> mList;
private FileAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
Spinner spinnerCountries = findViewById(R.id.spinner_countries);
mAdapter = new FileAdapter(this, mList);
spinnerCountries.setAdapter(mAdapter);
spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FileItem clickedItem = (FileItem) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getName();
Toast.makeText(MainActivity.this, clickedCountryName + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void initList() {
mList = new ArrayList<>();
mList.add(new FileItem("Edit", R.drawable.ic_action_edit));
mList.add(new FileItem("New", R.drawable.ic_action_new));
mList.add(new FileItem("Remove", R.drawable.ic_action_remove));
}
}
FileAdapter
package com.example.ravispinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
public class FileAdapter extends ArrayAdapter<FileItem> {
public FileAdapter(Context context, ArrayList<FileItem> countryList) {
super(context, 0, countryList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.file_spinner_row, parent, false
);
}
ImageButton imageViewFlag = convertView.findViewById(R.id.image_view_flag);
TextView textViewName = convertView.findViewById(R.id.text_view_name);
FileItem currentItem = getItem(position);
if (currentItem != null) {
//imageViewFlag.setImageResource(currentItem.getFlagImage());
textViewName.setText(currentItem.getName());
}
return convertView;
}
}
FileItem
package com.example.ravispinner;
public class FileItem {
private String mName;
private int mFlagImage;
public FileItem(String name, int flagImage) {
mName = name;
mFlagImage = flagImage;
}
public String getName() {
return mName;
}
public int getFlagImage() {
return mFlagImage;
}
}
file_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_view_flag"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toEndOf="#+id/image_view_flag"
android:gravity="center"
android:text="India"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageButton
android:id="#+id/icon"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_remove"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ravispinner.MainActivity">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow android:layout_margin="2dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="2dp"
android:padding="3dp"
android:text="Test Scenario\t: "
android:textSize="20dp" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp" />
</TableRow>
</TableLayout>
</RelativeLayout>
This is one more solution to achieve UI try once may it helps you.
change your file_spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center|start"
android:text="India"
android:textColor="#android:color/black"
android:textSize="16sp" />
<ImageView
android:id="#+id/image_view_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:scaleType="center"
android:src="#drawable/ic_baseline_remove_circle_24" />
</LinearLayout>
and activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight="0.5"
android:text="Test Scenario:"
android:textSize="20sp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_countries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:spinnerMode="dropdown" />
</LinearLayout>

How to update Custom ListView by button click

I am try to show Custom List View using Custom Adapter. that is showing data nicely but when i try to add new Item form EditTxt on click of Button then getting error.
Could not execute method for android:onClick
i am not able to add new Item in this list. here name will come form EditText and other item will constant. i try many method but not getting my solution. Another issue is , Activity is opening little late. can you tell me issue behind this.. please check Code...
Activity_phone_book.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center|center_horizontal|center_vertical"
android:text="Type Your Name"
android:textSize="14sp"
android:id="#+id/addNewName"/>
<Button
android:layout_width="100dp"
android:id="#+id/addNew"
android:onClick="addNewData"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:text="Add New" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_array.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/userImage"
android:layout_width="109dp"
android:layout_height="90dp"
android:padding="5dp" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"/>
<TextView
android:id="#+id/profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4d4d4d"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
PhoneBook Activity
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class PhoneBook extends AppCompatActivity {
// Declare variable types
int[] images={R.drawable.pankaj,R.drawable.logo_red,R.drawable.about_us,R.drawable.buddy_1,
R.drawable.buddy_2,R.drawable.contact_us,R.drawable.head_659651_640,R.drawable.login1,
R.drawable.login2,R.drawable.login3};
String[] names={"Pankaj Kumar","Rahul","Prabhash","Rima","Kum","Rupali","Amrawati","Nabhya",
"Nidhi","Sonu"};
String[] profiles={ "Web Developer","Software Developer","Web Developer","Software Developer",
"Web Developer","Software Developer","Web Developer","Software Developer",
"Web Developer","Software Developer" };
Button addNew ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_book);
ListView listview = (ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listview.setAdapter(customAdapter);
}
// here i want to add new Item using this function
public void addNewData( View v)
{
Toast.makeText(this, "length " + images.length, Toast.LENGTH_SHORT).show();
# here i am try to add new item in Array. but getting error. many method applied here but now worked.please tell me how can i add new item form Edit Text...
images = new int[R.drawable.login3];
}
class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.activity_array ,null);
ImageView imageView = (ImageView)convertView.findViewById(R.id.userImage);
TextView textView = (TextView)convertView.findViewById(R.id.userName);
TextView textView1 = (TextView)convertView.findViewById(R.id.profile);
imageView.setImageResource(images[position]);
textView.setText(names[position]);
textView1.setText(profiles[position]);
return convertView;
}
}
class CustomAdapter1 extends BaseAdapter
{
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.activity_array ,null);
ImageView imageView = (ImageView)convertView.findViewById(R.id.userImage);
TextView textView = (TextView)convertView.findViewById(R.id.userName);
TextView textView1 = (TextView)convertView.findViewById(R.id.profile);
imageView.setImageResource(images[position]);
textView.setText(names[position]);
textView1.setText(profiles[position]);
return convertView;
}
}
}

How to fix OnItemClickListener on ListView

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"/>

ListView Adapter executing onclick methods on multiple rows simultaneously for a single click event

In my app I have a listview in a tab fragment. Each row of listview is a cardview , which has multiple views in it.(Buttons, textviews etc.) I have created a custom adapter class for this listview.
This adapter class takes the data from local DB and inserts it in different views in cardview, creating a list of cards. I have a like button in cardview.
Now, when user presses the like button, I'm changing the background of button with a onClickListener on button in adapter class.
The problem is, if I press like button on one card, it will change the background of like button on that card as well as like button on forth card down in the list. I think this has to do something with ViewHolder pattern I'm using, but not sure how to fix it.
Here is the Custom Adapter class
package com.example.nxtstepz.nxtstepzone.Adpters;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.example.nxtstepz.nxtstepzone.Communicater.Communicater;
import com.example.nxtstepz.nxtstepzone.PostDetailActivity;
import com.example.nxtstepz.nxtstepzone.PostInfo;
import com.example.nxtstepz.nxtstepzone.R;
import java.util.List;
import de.greenrobot.dao.query.QueryBuilder;
import nxtstepz.DaoMaster;
import nxtstepz.DaoSession;
import nxtstepz.Post;
import nxtstepz.PostDao;
import nxtstepz.SavedPost;
import nxtstepz.SavedPostDao;
/**
* Created by Prathya on 6/8/2015.
*/
public class HomeScreenNewsFeedAdapter extends ArrayAdapter<PostInfo>{
List<PostInfo> data;
Context context;
int layoutid;
public HomeScreenNewsFeedAdapter(Context context, int layoutid, List<PostInfo> data) {
super(context, layoutid);
this.context=context;
this.data=data;
this.layoutid=layoutid;
}
#Override
public int getCount() {
return data.size();
}
#Override
public long getItemId(int position) {
return 0;
}
private class PostHolder{
TextView postusername,autherinstitute,postheading,postdescription,likecount,comments,date,category;
View save,like;
LinearLayout postdetail;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"nxtstepz",null);
SQLiteDatabase db =helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();
final PostDao postDao = session.getPostDao();
final PostHolder holder;
View v= convertView;
if(v==null){
LayoutInflater li = LayoutInflater.from(context);
v= li.inflate(layoutid,parent,false);
holder = new PostHolder();
holder.postusername = (TextView)v.findViewById(R.id.postusername);
holder.autherinstitute= (TextView)v.findViewById(R.id.autherinstitute);
holder.postheading = (TextView)v.findViewById(R.id.post_title);
holder.postdescription = (TextView)v.findViewById(R.id.post_description);
holder.likecount = (TextView)v.findViewById(R.id.like_count);
holder.comments = (TextView)v.findViewById(R.id.comment_count);
holder.date = (TextView)v.findViewById(R.id.post_datetime);
holder.category = (TextView)v.findViewById(R.id.post_category);
holder.save=v.findViewById(R.id.save_button);
holder.like= v.findViewById(R.id.like_button);
holder.postdetail=(LinearLayout)v.findViewById(R.id.ll3);
v.setTag(holder);
}
else {
holder= (PostHolder)v.getTag();
}
final PostInfo post;
post = data.get(position);
Log.d("post id", String.valueOf(post.posttypeid));
holder.postusername.setText(post.postusername);
holder.autherinstitute.setText(post.autherinstitutename);
holder.postheading.setText(post.postheading);
holder.postdescription.setText(post.postdescription);
holder.likecount.setText(""+post.likecount);
holder.comments.setText("" + post.comments);
holder.category.setText(post.categoryname);
holder.date.setText(post.date);
holder.like.setOnClickListener(new View.OnClickListener() { //this method has the problem
#Override
public void onClick(View v) {
if (post.postlikeflag == 0) {
holder.like.setBackgroundResource(R.drawable.liked);
post.postlikeflag =1;
} else {
holder.like.setBackgroundResource(R.drawable.like);
post.postlikeflag = 0;
}
}
});
holder.postdetail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent eventDetail = new Intent(context, PostDetailActivity.class);
eventDetail.putExtra("EventDetail", post);
context.startActivity(eventDetail);
}
});
return v;
}
}
Here is the single_card_view.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/homecardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/img8" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:gravity="left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/postusername"
android:gravity="left"
android:text="Jessica Alba"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/autherinstitute"
android:text="Mahatma audkjfsdk dkbldvb"
android:singleLine="true"
android:ellipsize="end"
android:textSize="13sp"
android:textStyle="italic"
android:scrollHorizontally="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="10dp"
android:text="38 min"
android:id="#+id/post_datetime"/>
<TextView
android:id="#+id/post_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginTop="10dp"
android:background="#9C27B0"
android:padding="5dp"
android:text=""
android:textColor="#ffffff"
android:textStyle="bold|italic" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00b5ad"
android:gravity="center|left"
android:padding="5dp"
android:text="Sri-Ram breakup party"
android:textColor="#ffffff"
android:textSize="15dp"
/>
<TextView
android:id="#+id/post_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="1dp"
android:background="#ffffff"
android:gravity="top|left"
android:padding="5dp"
android:text="Monica is an Italian actress and model who recently broke up with Sriram started her modelling career at the age of 13 by posing for a local photo enthusiast. Android:The Best OS.Android powers hundreds of millions of mobile devices in more than 190 countries around the world.Android's openness has made it a favorite for consumers"
android:textSize="10dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="2dp"
android:background="#000000"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<View
android:id="#+id/like_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#drawable/like"
android:focusable="false"
android:focusableInTouchMode="false">
</View>
<View
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/comment"
android:id="#+id/comment_button"></View>
<View
android:layout_width="45dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/view"
android:id="#+id/save_button"
android:background="#drawable/save"></View>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="6dp"
android:text="0"
android:gravity="center"
android:id="#+id/like_count"
android:background="#FFECB3"
android:layout_alignTop="#+id/like_button"
android:layout_toRightOf="#+id/like_button"
android:layout_toEndOf="#+id/like_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="6dp"
android:text="0"
android:gravity="center"
android:id="#+id/comment_count"
android:background="#FFECB3"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignTop="#+id/comment_button"
android:layout_toRightOf="#+id/comment_button"
android:layout_toEndOf="#+id/comment_button" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
i have modifiedyour code paste it and check wether it is working or not
#Override
public View getView(int position, View convertView, ViewGroup parent) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"nxtstepz",null);
HashMap<Integer, String> map = new HashMap<Integer, String>();
SQLiteDatabase db =helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();
final PostDao postDao = session.getPostDao();
final PostHolder holder;
View v= convertView;
if(v==null){
LayoutInflater li = LayoutInflater.from(context);
v= li.inflate(layoutid,parent,false);
holder = new PostHolder();
holder.postusername = (TextView)v.findViewById(R.id.postusername);
holder.autherinstitute= (TextView)v.findViewById(R.id.autherinstitute);
holder.postheading = (TextView)v.findViewById(R.id.post_title);
holder.postdescription = (TextView)v.findViewById(R.id.post_description);
holder.likecount = (TextView)v.findViewById(R.id.like_count);
holder.comments = (TextView)v.findViewById(R.id.comment_count);
holder.date = (TextView)v.findViewById(R.id.post_datetime);
holder.category = (TextView)v.findViewById(R.id.post_category);
holder.save=v.findViewById(R.id.save_button);
holder.like= v.findViewById(R.id.like_button);
holder.postdetail=(LinearLayout)v.findViewById(R.id.ll3);
v.setTag(holder);
}
else {
holder= (PostHolder)v.getTag();
}
final PostInfo post;
post = data.get(position);
Log.d("post id", String.valueOf(post.posttypeid));
holder.postusername.setText(post.postusername);
holder.autherinstitute.setText(post.autherinstitutename);
holder.postheading.setText(post.postheading);
holder.postdescription.setText(post.postdescription);
holder.likecount.setText(""+post.likecount);
holder.comments.setText("" + post.comments);
holder.category.setText(post.categoryname);
holder.date.setText(post.date);
map.put(position,"like");
holder.like.setOnClickListener(new View.OnClickListener() { //this method has the problem
#Override
public void onClick(View v) {
if (post.postlikeflag == 0 && map.get(position).equals("like")) {
holder.like.setBackgroundResource(R.drawable.liked);
map.put(position,"liked");
post.postlikeflag =1;
} else {
holder.like.setBackgroundResource(R.drawable.like);
post.postlikeflag = 0;
}
}
});
holder.postdetail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent eventDetail = new Intent(context, PostDetailActivity.class);
eventDetail.putExtra("EventDetail", post);
context.startActivity(eventDetail);
}
});
In your button and other view where onClick listener is there add these two line in xml
android:focusable="false"
android:focusableInTouchMode="false"
declare final variable index as below and initialize it with position at very
start of getView method as below
final int index = position;
Also on click instead of this
post.postlikeflag =1;
try this
data.get(index).postlikeflag = 1;
also you will have to call notifyDataSetChanged()
I got it working by using HashMap as SAM suggested.
Here is what I did.
in Adapter class
HashMap<Long, String> map = new HashMap<Long, String>();
in getView()method of adapter
if(post.postlikeflag==0){
map.put(post.postid, "like");
}
else{
map.put(post.postid,"liked");
}
if(map.get(post.postid).equals("like")) {
holder.like.setBackgroundResource(R.drawable.like);
}
else {
holder.like.setBackgroundResource(R.drawable.liked);
}
And in onclickListener of like button
if (post.postlikeflag == 0 && map.get(post.postid).equals("like")) {
holder.like.setBackgroundResource(R.drawable.liked);
map.put(post.postid, "liked");
post.postlikeflag = 1;
} else {
holder.like.setBackgroundResource(R.drawable.like);
post.postlikeflag = 0;
map.put(post.postid,"like");
}
I know there can be some redundancy in the code. Point it out if you find.

Image not displayed in listview

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;
}

Categories

Resources