Displaying data in listview - android

I am trying to design a listview wherein i have to display two to three data for rows of each column,but i am not able to do so,i am getting the data has in image a but i want to display has in image b,i can display only single data in each row of each column.For example the column states to display the value of item number and style number ,i am able to display only item number but not style number.
**DataDisplay 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" >
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:text="TextView" />
</LinearLayout>
**Listview xml**
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#00000000"
android:dividerHeight="5dp"
/>
</LinearLayout>
adapter
public class CustomAdapter extends BaseAdapter{
String [] result;
String [] result1;
String [] result2;
String [] result3;
String [] result4;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(ItemActivity mainActivity, String[] prgmNameList,String[] prgmNameList1,String[] prgmNameList2,String[] prgmNameList3,String[] prgmNameList4) {
// TODO Auto-generated constructor stub
result=prgmNameList;
result1=prgmNameList1;
result2=prgmNameList2;
result3=prgmNameList3;
result4=prgmNameList4;
context=mainActivity;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.tabitem, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.tv1=(TextView)rowView.findViewById(R.id.textView2);
holder.tv2=(TextView) rowView.findViewById(R.id.textView3);
holder.tv3=(TextView) rowView.findViewById(R.id.textView4);
holder.tv4=(TextView) rowView.findViewById(R.id.textView5);
holder.tv.setText(result[position]);
holder.tv1.setText(result1[position]);
holder.tv2.setText(result2[position]);
holder.tv3.setText(result3[position]);
holder.tv4.setText(result4[position]);
rowView.setBackgroundColor(Color.parseColor("#F1F1FF"));
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
called from activity
public static String [] prgmNameList={"123456","456665","123445","5675343"};
public static String [] prgmNameList1={"15.56","15.562","15.67","15.455"};
public static String [] prgmNameList2={"9999","9999","9999","9999"};
public static String [] prgmNameList3={"99%","99%","99%","99%"};
public static String [] prgmNameList4={"9999","9999","9999","9999"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.itemlist);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmNameList1,prgmNameList2,prgmNameList3,prgmNameList4));
}

You need to create a custom layout for your row item where you can place two textviews one below the other to show your data as you want. Then override the getView() method of your adapter and inflate this custom layout and populate it with data from an array / list.
See the getView() method in the link mentioned in giacavicchioli's answer.

As user87049 has said this will solve your problem sarah:
**DataDisplay 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" >
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:textSize="25dp"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:textSize="25dp"
android:text="TextView" />
</LinearLayout>
tell me if this this is not what you want :)
Ah and since I'm a bit busy these days and I may not come to this account on stackoverflow, please send me email to get my attention please [erfan.molaei#gmail.com]

Here is what i did. Make a xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF" />
</LinearLayout>
</LinearLayout>
This is your custom listview xml file. Now in getView method setText to this all textview as you did for your xml file.
Check out below screen:

Related

How to set Visibility of any view inside listview row from button outside the listview

I have edit button on toolbar on click this button i want to show new view that is cross button inside every row of listview. is this possible and if yes how ? Thanks in advance. Below is layout files:
main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/TabColor"
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_weight="1"
android:onClick="back2"
android:layout_gravity="center"
app:srcCompat="#drawable/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="7"
android:paddingLeft="#dimen/margin_small"
android:text="List OF Hotels"
android:textSize="#dimen/text_title"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="2"
android:background="#drawable/transparent_bg"
android:layout_marginRight="#dimen/margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/edit_jobs"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Edit"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="invisible"
android:id="#+id/done_editing"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Done"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/listview1"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
listview row layout where i have to show cross image:
<?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_marginTop="#dimen/margin_large"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
android:background="#drawable/border"
android:padding="#dimen/margin_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_gravity="center"
android:layout_weight="9"
android:textColor="#color/TabColor"
android:singleLine="true"
android:textSize="#dimen/text_large"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:gravity="right"
android:id="#+id/arrow"
android:src="#drawable/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:id="#+id/cross"
android:gravity="right"
android:src="#drawable/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_weight="5"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="5"
android:text=""
android:gravity="right"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> titlelist,datalist;
public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
super(context, R.layout.listview_row, title);
titlelist= new ArrayList<>();
datalist= new ArrayList<>();
this.context=context;
this.titlelist=title;
this.datalist=data;
}
private static class ViewHolder {
TextView t1,t2;
}
#Override
public int getCount() {
return titlelist.size();
}
#Override
public String getItem(int position) {
return titlelist.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.title);
holder.t2 = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(titlelist.get(position));
holder.t2.setText(datalist.get(position));
return convertView;
}
}
you have to add this functionality in your adapter,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ur other codes.....
//
//
if(crossButtonPress){
holder.imageView.setImageResource(R.drawable.cross);
}else{
holder.imageView.setImageResource(R.drawable.default_ic);
}
}
In addition, in you actual button's onClick,
crossButtonPress = true;
adapter.notifyDatasetChanged();

ListView Items changed automatically in Fragment android

when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.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:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);

Listview displays only one row from the list

I am new to android listviews and I am trying to populate a listview in android by a list of items coming from a webservice. I know that the list contains more than one record coming from the webservice but my custom listview displays only the first record from the list. I checked the size of the list and there is always more than one records but the listview is showing only one of them. My custom adapter is like following:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<LItem> lstItems;
ListAdapter(Context context, ArrayList<LItem> objects) {
ctx = context;
lstItems = objects;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return lstItems.size();
}
#Override
public Object getItem(int position) {
return lstItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
LItem p = getProduct(position);
((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
((TextView) view.findViewById(R.id.tvBQuantity )).setText(p.getBQ() );
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
LItem getProduct(int position)
{
return ((LItem) getItem(position));
}
ArrayList<LItem> getBox() {
ArrayList<LItem> box = new ArrayList<LItem>();
for (LItem p : lstItems) {
// if (p.selected)
// box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//getProduct((Integer) buttonView.getTag())= isChecked;
}
};
}
I am binding the listview as:
protected void onPostExecute(List<LItem> li ) {
super.onPostExecute(lstItm);
if(lstItm.size()>0) {
Adp=new ListAdapter(myactivity,lstItm);
lvTasks.setAdapter(Adp);
Log.d("Size---------",Integer.toString(lstItm.size()) );//here it writes more than one as size of the list.
}
}
My xml file for displaying lists is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#FFF"
android:button="#drawable/custom_checkbox_design"
android:focusableInTouchMode="false"
android:text="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Task:"
android:id="#+id/textView2"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="task"
android:id="#+id/tvTask"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material:"
android:id="#+id/textView1"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="material"
android:id="#+id/tvMaterial"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balanced Quantity:"
android:id="#+id/textView14"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bquantity"
android:id="#+id/tvBQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Balanced Quantity:"
android:id="#+id/textView25"
android:layout_weight="1"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/tvAQuantity"
android:layout_weight="1"
android:textColor="#FFF"
/>
</LinearLayout>
</LinearLayout>
My xml for listview is like this:
<LinearLayout 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="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Issue was with the scrollview which was createing the issue. I just removed
<ScrollView android:id="#+id/ScrlView" android:layout_width="fill_parent" android:layout_height="fill_parent" >
</ScrollView>
and it worked for me :)

Create a layout with two layout for right and left side .having a Listview to scroll it

I have created this layout and want to be divided in two parts same of left and right side of the page. That have a scroll in that for scroll I want to use ListView Please help me on this I want left side independent scorll and the right side independent scroll
<?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="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/if"
android:layout_width="50dip"
android:layout_height="42dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/fc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:text=""
android:textStyle="bold" />
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:text=""
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/ty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text=""
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/dd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/flightTime"
android:textColor="#343434"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="" />
<TextView
android:id="#+id/cc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/flightDuration"
android:textColor="#343434"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="" />
</RelativeLayout>
This is my listView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
This is how i am trying to create the layout with the listview
http://s17.postimg.org/x1fcutey7/sample.png
Use the android:numColumns="2" in your GridView widget
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical">
</GridView>
Use a gridview with two columns. You will need to ad dividers and customize according to your needs.
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
GridView gv = (GridView) findViewById(R.id.grid);
CustomAdapter adapter = new CustomAdapter(MainActivity.this);
gv.setAdapter(adapter);
}
class CustomAdapter extends BaseAdapter
{
LayoutInflater mInflater;
public CustomAdapter(Context mainActivity) {
// TODO Auto-generated constructor stub
mInflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 20;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub ViewHolder holder;
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row,
parent, false);
holder = new ViewHolder();
holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
holder.tv3 = (TextView) convertView.findViewById(R.id.textView3);
holder.tv4 = (TextView) convertView.findViewById(R.id.textView4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// set data here
return convertView;
}
}
static class ViewHolder
{
ImageView iv;
TextView tv1,tv2,tv3,tv4;
}
}
test.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical">
</GridView>
row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="14dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="43dp"
android:layout_toRightOf="#+id/imageView1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="24dp"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/imageView1"
android:text="TextView" />
</RelativeLayout>
snap shot
Edit:
If you want the left side and right side to scroll individually you will need two listviews. One for the left and one for the right. Have custom adapters for both the listviews.

How to show Toast in a class extended by BaseAdapter Get View Method

I am having problem of showing Toast Message when i click a button within a list View.
The problem is that i a custom class extended by BaseAdapter i have a method named GetView
I wanna show a toast Message from that particular message
Following is my Listview Row Xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:context=".HomeActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_Header_laugh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#818085" >
<RelativeLayout
android:id="#+id/relative_header"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_Vote_laugh"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_number_of_vote_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Vote_laugh"
android:layout_alignBottom="#+id/btn_Vote_laugh"
android:layout_toRightOf="#+id/UserIconPic_laugh"
android:layout_weight=".4"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_MainText_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txt_MainText_laugh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/whatweare_about_main"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="HardcodedText" />
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_laugh"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relative_footer"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/btn_Share_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_laugh"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_laugh"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_header_light"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_Vote_light"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_vote_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Vote_light"
android:layout_alignBottom="#+id/btn_Vote_light"
android:layout_toRightOf="#+id/UserIconPic_light"
android:layout_weight=".4"
android:textAppearance="?android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_content_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:maxLength="300"
android:text="#string/Main_laugh"
android:textAppearance="?android:attr/textAppearanceSmall"
app:ignore="HardcodedText" />
<ImageView
android:id="#+id/light_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:ignore="ContentDescription" />
</FrameLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_light"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/Btn_Share_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_light"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_light"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ignore="UselessParent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="#+id/tableRow_header_fun"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout04"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#818085" >
<ImageView
android:id="#+id/UserIconPic_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.4"
android:src="#drawable/ic_launcher"
app:ignore="ObsoleteLayoutParam,ContentDescription" />
<Button
android:id="#+id/btn_vote_fun"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight=".4"
android:text="VOTE"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
<TextView
android:id="#+id/txt_vote_number_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_vote_fun"
android:layout_alignBottom="#+id/btn_vote_fun"
android:layout_toRightOf="#+id/UserIconPic_fun"
android:layout_weight=".4"
android:textAppearance="? android:attr/textAppearanceMedium"
app:ignore="ObsoleteLayoutParam,HardcodedText" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Content_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image_fun"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:baselineAlignBottom="true"
android:cropToPadding="true"
app:ignore="ContentDescription" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_fun"
android:layout_alignLeft="#+id/image_fun"
android:layout_alignRight="#+id/image_fun"
android:layout_alignTop="#+id/image_fun"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/main_light"
android:textColor="#000000" />
</RelativeLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow_Footer_fun"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#bababa" >
<Button
android:id="#+id/Btn_share_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
app:ignore="HardcodedText" />
<ImageButton
android:id="#+id/btn_facebook_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btn_twitter_fun"
android:src="#drawable/pic2"
app:ignore="ContentDescription" />
<ImageButton
android:id="#+id/btn_twitter_fun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_facebook_fun"
android:src="#drawable/pic3"
app:ignore="ContentDescription" />
</RelativeLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</TableRow>
</TableLayout>
</ScrollView>
My CustomeAdapter Class code
package com.example.laysapp.ContentListView;
import java.util.ArrayList;
import java.util.List;
import com.example.laysapp.R;
import com.example.laysapp.RegisteredUser.LighterShare;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ContentItemAdapter extends BaseAdapter {
private final List<Content_Items> items;
public ContentItemAdapter(final Context context, final int itemResId,
final ArrayList<Content_Items> items2) {
this.items = items2;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#SuppressWarnings("null")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Content_Items item = this.items.get(position);
View itemView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.contentlistrow, null);
} else {
itemView = convertView;
}
// Set the text of the Laugh Row Start
TextView txtLaugh_Vote = (TextView) itemView
.findViewById(R.id.txt_number_of_vote_laugh);
txtLaugh_Vote.setText(item.getLaughNo_Vote());
TextView txtlaugh_Content = (TextView) itemView
.findViewById(R.id.txt_MainText_laugh);
txtlaugh_Content.setText(item.getLaughContent());
ImageView imgView_Laugh_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_laugh);
imgView_Laugh_UserPic.setImageBitmap(item.getLaughtUserPic());
// Set the text of the Laugh Row End
// Setting of Light Row Start
TextView txtLight_Vote = (TextView) itemView
.findViewById(R.id.txt_vote_light);
txtLight_Vote.setText(item.getLaughNo_Vote());
TextView txtlight_Content = (TextView) itemView
.findViewById(R.id.textView1);
txtlight_Content.setText(item.getLaughContent());
ImageView imgView_Light_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_light);
imgView_Light_UserPic.setImageBitmap((item.getLightStoriesUserPic()));
ImageView imgView_LightContent_Image = null;
// Check weather ContentImage is Available For Light
String Check = item.getIsLightContentAvaiable();
if (Check == "yes") {
imgView_LightContent_Image = (ImageView) imgView_LightContent_Image
.findViewById(R.id.light_image);
imgView_LightContent_Image.setImageBitmap(item
.getLightStoriesContentImagePic());
}
// Setting of Light Row End
// Set the text of the Funto Row Start
TextView txtfunto_Vote = (TextView) itemView
.findViewById(R.id.txt_vote_number_fun);
txtfunto_Vote.setText(item.getLaughNo_Vote());
TextView txtfunto_Content = (TextView) itemView
.findViewById(R.id.myImageViewText);
txtfunto_Content.setText(item.getLaughContent());
// Set the text of the Funto Row End
ImageView imgView_funto_UserPic = (ImageView) itemView
.findViewById(R.id.UserIconPic_fun);
imgView_funto_UserPic.setImageBitmap(item.getfuntoUserPic());
ImageView imgView_funto_ContentPic = (ImageView) itemView
.findViewById(R.id.image_fun);
imgView_funto_ContentPic.setImageBitmap(item.getfuntoContentImagePic());
Button MainVote_Laugh = null;
MainVote_Laugh = (Button) MainVote_Laugh
.findViewById(R.id.btn_Vote_laugh);
MainVote_Laugh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show();
}
});
return itemView;
}
}
Replace this line:
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
with this line:
Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
You have got a context right in the constructor. create a class level Context variable say ctx. inside constructor write
this.ctx = context;
finally replace getApplication() in
Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show();
with ctx.
You can also use parent.getContext(), full code of toast is like below
Toast.makeText(parent.getContext(), "view clicked", Toast.LENGTH_SHORT).show();
As you switch from your MainActivity context changes, you have to use your AdapterClass > ViewHolderClass > ViewHolder's constructor argument to get context for adapter.
Here is an example.
public MyViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(),"Hi",Toast.LENGTH_LONG);
}
});
}
otherwise you can also pass context to a constructor argument for any other class not having activity.
example.
public class MyClass {
Context c;
public MyClass(Context context) {
c = context;
}}

Categories

Resources