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);
Related
hi I am trying to display data in a recyclerview in android by parsing a json from mysql database. But it keeps displaying just some of the data and I can't seem to find the problem.Any help will be greatly appreciated.
This is the adapter:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
List<Course> data;
private Context context;
public Adapter(Context context,List<Course> data){
this.data=data;
this.context=context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
DecimalFormat form = new DecimalFormat("0.00");
Course current=data.get(position);
holder.tCoursecode.setText(current.getmCoursecode());
holder.tCoursetitle.setText(current.getmCoursetitle());
holder.tCredit.setText(String.valueOf(current.getmCredit()));
holder.tMarks.setText(form.format (current.getmMarks()));
holder.tGrade.setText(current.getmGrade());
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tCoursecode;
TextView tCoursetitle;
TextView tCredit;
TextView tMarks;
TextView tGrade;
public MyViewHolder(View itemView) {
super(itemView);
tCoursecode=itemView.findViewById(R.id.tcc);
tCoursetitle=itemView.findViewById(R.id.tct);
tCredit=itemView.findViewById(R.id.tc);
tMarks=itemView.findViewById(R.id.tm);
tGrade=itemView.findViewById(R.id.tg);
}
}
this the layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/right">
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="CSC 460" />
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text= "Information Security"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:padding="25dp"
android:textSize="18sp"
tools:text="1"/>
<TextView
android:id="#+id/tm"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="78.4"/>
<TextView
android:id="#+id/tg"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="A"/>
</LinearLayout>
Screenshot:the grade "A" is not displaying
Screenshot
try this way
<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="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="16dp"
>
<LinearLayout
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="CSC 460"
/>
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Information Security"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp"
tools:text="1"
/>
<TextView
android:id="#+id/tm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="78.4"
/>
<TextView
android:id="#+id/tg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="A"
/>
</LinearLayout>
</LinearLayout>
hope this helps
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();
This is the getChildView inside the Adapter class...
public View getChildView(int parent, int child, boolean lastChild, View view, ViewGroup viewGroup) {
ArrayList<String> itemDetails= (ArrayList<String>)getChild(parent,child);
if(view==null)
{
LayoutInflater inflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.child_layout,viewGroup,false);
}
TextView textView =(TextView)view.findViewById(R.id.textView2);
textView.setText(itemDetails.get(0));
TextView textView1(TextView)view.findViewById(R.id.itemdescription);
textView1.setText(itemDetails.get(1));
TextView textView3= (TextView)view.findViewById(R.id.price);
textView3.setText(itemDetails.get(2));
final TextView textView2 = (TextView)view.findViewById(R.id.counterTextView);
ImageButton add = (ImageButton)view.findViewById(R.id.imageButton);
final ImageButton remove = (ImageButton)view.findViewById(R.id.imageButton1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString())+1));
remove.setVisibility(View.VISIBLE);
}
});
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Integer.parseInt(textView2.getText().toString())==0)
{
remove.setVisibility(View.INVISIBLE);
}
else {
textView2.setText(String.valueOf(Integer.parseInt(textView2.getText().toString()) - 1));
}
}
});
return view;
}
This is the ChildLayout:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#FFFFFF">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/k_meals_rs"
android:id="#+id/imageView6" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="10dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_remove_black"
android:id="#+id/imageButton1"
android:background="#drawable/round_button"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#000000"
android:id="#+id/counterTextView" />
<ImageButton
app:srcCompat="#drawable/ic_add_black"
android:id="#+id/imageButton"
android:background="#drawable/round_button"
android:layout_width="29dp"
android:layout_height="29dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp">
<TextView
android:id="#+id/itemdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:maxEms="15"
android:textColor="#000000"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="₹150"
android:textColor="#000000"/>
</RelativeLayout>
</LinearLayout>
i want to increment & decrements the value of counterTextView as the the + and - buttons are pressed.. but as i do so it increment and decrements the value of counterTextView in other child in other parents with same index value...any idea how to overcome this problem.. thnx
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 :)
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: