how to scroll table horizontally in android - android

I am trying to implement table layout in android.I have table Header and row designed in one xml file and am passing data using adapter.So What is happening is I got scrolling horizontally but with multiple table header repeating on each row.
What I want to achieve is table header should be seen only once and all the rows below it should scroll along with table header.
Here's my xml file
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
And here's the image what it looks likescrolling table
UPDATE (2) HERE's Adapter Code
public class Flight_cancel_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Flight_cancel_Details> rowItems;
public static int available_seats;
public static PolicyAdapter adapter;
int TOTAL;
public static ArrayList<String> arraySeat=new ArrayList<String>();
Flight_cancel_Adapter(Context context, ArrayList<Flight_cancel_Details> rowItems) {
this.context = context;
this.rowItems = rowItems;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView type;
TextView fare;
TextView tax;
TextView air_pnr;
TextView ticket;
TextView namee;
CheckBox box;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.test, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.namee = (TextView) convertView.findViewById(R.id.name);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.fare = (TextView) convertView.findViewById(R.id.fare);
holder.tax = (TextView) convertView.findViewById(R.id.tax);
holder.air_pnr = (TextView) convertView.findViewById(R.id.air_pnr);
holder.ticket = (TextView) convertView.findViewById(R.id.ticket);
holder.box = (CheckBox) convertView.findViewById(R.id.check_bx);
holder.box.setTag(position);
try
{
final Flight_cancel_Details row_pos = rowItems.get(position);
String name=String.valueOf(row_pos.getName());
String typ=String.valueOf(row_pos.gettypes());
String far=String.valueOf(row_pos.getfares());
String tax=String.valueOf(row_pos.gettax());
String pnr=String.valueOf(row_pos.getair_pnr());
String ticket=String.valueOf(row_pos.getticket());
System.out.print("ROW POS-"+row_pos.getStatus());
if(row_pos.getStatus().equals("CANCELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(false);
holder.box.setText("CANCELLED");
}else {
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
holder.box.setEnabled(true);
holder.box.setText("CANCEL");
}
if(row_pos.getTarvel_Status().equals("TRAVELLED"))
{
holder.namee.setText(name);
holder.type.setText(typ);
holder.fare.setText(far);
holder.tax.setText(tax);
holder.air_pnr.setText(pnr);
holder.ticket.setText(ticket);
}
}
catch (Exception e)
{
Log.e("BUS_CANCEL_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
holder.box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
int position = (int) buttonView.getTag();
System.out.println("--CLICKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--VALUE--" + contactId);
arraySeat.add(contactId);
}
else
{
int position = (int) buttonView.getTag();
System.out.println("--UNCHEKED--" + position);
String contactId = (holder.air_pnr.getText().toString());
System.out.println("--UNCHEKED VALUE--" + contactId);
arraySeat.remove(contactId);
}
}
});
return convertView;
}
}

Use Recyclerview for horizontal scrollview for a better performance
test.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="vertical">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
</LinearLayout>
recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:listitem="#layout/layout">
</android.support.v7.widget.RecyclerView>

Here my XML file, you should put title for all row TableView (TextView) on LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:id="#+id/horizontalView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:scrollbars="horizontal|vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--Title for all row, once for all row -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Name"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Fare"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Tax"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Airline Pnr"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Ticekt Numner"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</LinearLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:stretchColumns="*"
android:divider="#color/black"
android:showDividers="middle"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="My Name"
android:textColor="#color/black"
android:textSize="18sp"
android:id="#+id/name"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/type"
android:text="My Type"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/fare"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/tax"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/air_pnr"
android:textColor="#color/black"
android:textSize="18sp" />
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/ticket"
android:textColor="#color/black"
android:textSize="18sp" />
<CheckBox
android:id="#+id/check_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>

Related

How to click button inside listview

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?

Android: Custom List View is not working properly

My apologies for not providing a proper source code.
basically what I have in my list view xml layout is:
listleave.xml
leaveSummary.xml
leavedB
Custom Adapter
ApplyLeave
I'm creating an app where I have a list view and when I'm running my app I'm getting an error in Locgat.
Caused by:java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null
object reference
at com.example.huzai.civilsoft1.ApplyLeave.loadDataInListView(ApplyLeave.java:258 )
at com.example.huzai.civilsoft1.ApplyLeave.onCreate(ApplyLeave.java:84)
In the line loadDataInListView when I'm not commenting l1.setAdapter(customAdapter); the app crashes but when I'm commenting it works properly.
Result of not commenting: l1.setAdapter(customAdapter);
ApplyLeave Activity
public class ApplyLeave extends AppCompatActivity {
LeaveService myDb;
//Declaration EditTexts
EditText editstartDate;
EditText editendDate;
DatePickerDialog datePickerDialog;
CheckBox checkgoingAbroad;
Spinner spinnertypess;
Spinner editleavess;
EditText editremarks;
EditText editnoOfDays1;
TextView noOfDays;
TextView endd;
Button btnApply;
Button addfield;
Button deletefield;
LinearLayout parentlinearlayout;
ListView l1;
ArrayList<Leave> arrayList;
CustomAdapter customAdapter;
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_leave);
myDb = new LeaveService (this);
checkgoingAbroad =(CheckBox) findViewById(R.id.GoingAbroad);
spinnertypess = (Spinner)findViewById(R.id.types);
editleavess = (Spinner)findViewById(R.id.Leaves);
editremarks = (EditText)findViewById(R.id.reason);
editnoOfDays1 = (EditText)findViewById(R.id.noOfDays);
parentlinearlayout =
(LinearLayout)findViewById(R.id.parent_linear_layout);
addfield = (Button)findViewById(R.id.addField);
deletefield = (Button)findViewById(R.id.removeField);
endd = (TextView)findViewById(R.id.date);
l1 = (ListView)findViewById(R.id.leaveApplicationSummary);
btnApply = (Button)findViewById(R.id.apply);
AddData();
arrayList = new ArrayList<>();
loadDataInListView();
//Created a method for loadDataInListView
private void loadDataInListView() {
arrayList = myDb.getAllData();
customAdapter = new CustomAdapter(this,arrayList);
l1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
private void AddData() {
btnApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData("", checkgoingAbroad.toString(), spinnertypess.toString(), editleavess.toString(), editnoOfDays1.getText().toString(), editstartDate.getText().toString(), editendDate.getText().toString(), editremarks.getText().toString());
if (isInserted == true){
Toast.makeText(ApplyLeave.this, "Data Inserted Successfully",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ApplyLeave.this, "Data Not Inserted",Toast.LENGTH_LONG).show();
}
}
});
}
Result of commenting: //l1.setAdapter(customAdapter);
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<Leave> arrayList;
public CustomAdapter(Context context, ArrayList<Leave>arraylist) {
this.context = context;
this.arrayList = arraylist;
}
#Override
public int getCount() {
return this.arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.leavesummary, null);
TextView types = (TextView)convertView.findViewById(R.id.textViewtypes);
TextView startDate = (TextView)convertView.findViewById(R.id.textViewstartDate);
TextView endDate = (TextView)convertView.findViewById(R.id.textViewendDate);
TextView leavetype = (TextView)convertView.findViewById(R.id.textViewleaveType);
TextView noOfDays = (TextView)convertView.findViewById(R.id.textViewnoOfDays);
TextView reason = (TextView)convertView.findViewById(R.id.textViewreason);
Leave leave = arrayList.get(position);
types.setText(leave.getTypes());
startDate.setText(leave.getStartDate());
endDate.setText(leave.getEndDate());
leavetype.setText(leave.getLeaves());
noOfDays.setText(leave.getNoOfDays());
reason.setText(leave.getReason());
return convertView;
}
}
listleave.xml Over here I'm using ConstraintLayout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveApplicationSummary">
</ListView>
</android.support.constraint.ConstraintLayout>
leaveSummary.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Application Summary"
android:textColor="#080808"
android:textSize="20dp"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="72dp"
android:text="Applicant Name :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewApplicantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="72dp"
android:text="Huzaifa [001]"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="116dp"
android:text="Type of Leave :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewtypes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="116dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="164dp"
android:text="Leave Start Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewstartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="164dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="208dp"
android:text="Rejoining Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewendDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="208dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="248dp"
android:text="Leave Type :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewleaveType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="248dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="288dp"
android:text="Total Days Applied :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewnoOfDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="216dp"
android:layout_marginLeft="216dp"
android:layout_marginTop="288dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="332dp"
android:text="Remarks :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewreason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginLeft="144dp"
android:layout_marginTop="332dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Back" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
apply_leave.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".ApplyLeave">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_linear_layout"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/GoingAbroad"
android:hint="Going Abroad"
android:inputType="text"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Need Advance Payment?"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/types"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/types"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Leave Type"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/Leaves"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/leaves"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/noOfDays"
android:hint="No. of Days"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/addField"
android:text="+"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/removeField"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Leave starts on" />
<EditText
android:id="#+id/startDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rejoining will be on" />
<EditText
android:id="#+id/endDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<EditText
android:id="#+id/reason"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Remarks" />
<TextView
android:id="#+id/days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total leave days = 0"/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Leave ends on = "/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:gravity="bottom">
<Button
android:id="#+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Apply" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please tell me where I'm going wrong.
You are getting null l1.
Confirm your id of ListView in your activity_apply_leave.xml. It should be leaveApplicationSummary.
EDIT:
Your ListView is in listleave.xml but you are using activity_apply_leave.xml in your Activity.
Move your ListView from listleave.xml to activity_apply_leave.xml.

How to limit ListView to particular number of list items?

I am working on a functionality in which I have a ListView and I want listView to show only 3 items. I have set ListView height to "wrap_content" and in adapter I have set get count to 3 so that it will show only 3 items but it is showing only 1 item. I am not able to figure out what is happening.My API is returing only 3 items but not able to show all the items in ListView.
Adapter Code:
public class Adapter3Testimonials extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private Context context;
public Adapter3Testimonials(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
this.data = d;
context = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(R.layout.item_3_testimonials, parent, false);
TextView txtTestimonial = (TextView) itemView.findViewById(R.id.txtAdvisorTestimonails);
Log.e("TAG", "item_3_testimonials: " + position);
HashMap<String, String> testimonialsList;
testimonialsList = new HashMap<String, String>();
testimonialsList = data.get(position);
txtTestimonial.setText(testimonialsList.get("description"));
return itemView;
}
}
Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5">
<FrameLayout
android:id="#+id/layImageProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dp5"
android:background="#drawable/half_blue_circle">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="#dimen/dp80"
android:layout_height="#dimen/dp80"
android:padding="#dimen/dp2"
android:src="#drawable/blank_profile" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_background"
android:visibility="gone"
app:civ_border_width="2dp" />
<ImageView
android:id="#+id/imgBlockUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/profile_image2"
android:layout_gravity="right"
android:src="#drawable/phone" />
</FrameLayout>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/dp13"
android:layout_toRightOf="#+id/layImageProfile"
android:text="John doe"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<TextView
android:id="#+id/txtFirmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtName"
android:layout_alignStart="#id/txtName"
android:layout_below="#id/txtName"
android:text="Demo Corp. Inc."
android:textColor="#android:color/black" />
<TextView
android:id="#+id/txtRnf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtFirmName"
android:layout_alignStart="#id/txtFirmName"
android:layout_below="#id/txtFirmName"
android:text="Rnf No #12234"
android:textColor="#android:color/black" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgReviewStarts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/txtRnf"
android:layout_below="#id/txtRnf"
android:layout_marginTop="#dimen/dp5"
android:layout_toRightOf="#id/layImageProfile"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
<ImageView
android:id="#+id/imgClickToConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/dp25"
android:src="#drawable/click" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp10"
android:background="#color/colorPrimary" />
<TextView
android:id="#+id/txtStatement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary"
android:paddingBottom="#dimen/dp5"
android:paddingLeft="#dimen/dp5"
android:paddingTop="#dimen/dp3"
android:text="Mission statement Lorum ipsum dolar sit or elit lamet,consector cillium"
android:textColor="#android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:paddingLeft="#dimen/dp10"
android:text="#string/customer_review_score"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp10"
android:progress="100"
android:progressTint="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buying_exp" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgBuyingExp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtRnf"
android:layout_marginLeft="#dimen/dp20"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/knowledge_comp" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/imgKnowledgeComp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtRnf"
android:layout_marginLeft="#dimen/dp20"
app:srb_borderColor="#color/colorPrimary"
app:srb_fillColor="#color/colorPrimary"
app:srb_isIndicator="true"
app:srb_numberOfStars="5"
app:srb_starBorderWidth="1.0"
app:srb_starCornerRadius="2.5"
app:srb_starSize="#dimen/dp15"
app:srb_stepSize="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:text="#string/percentage_of_customer" />
<TextView
android:id="#+id/txtPercentageRecommended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dp20"
android:layout_toRightOf="#id/txt"
android:text="100%" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp10"
android:background="#color/colorPrimary" />
<LinearLayout
android:id="#+id/layTestimonials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp10"
android:paddingTop="#dimen/dp10"
android:text="#string/customer_testimonials"
android:textColor="#android:color/white"
android:textSize="#dimen/dp15" />
<TextView
android:id="#+id/btnTestimonialNumber"
android:layout_width="#dimen/dp35"
android:layout_height="#dimen/dp35"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp5"
android:layout_toRightOf="#id/textView"
android:background="#drawable/round_button"
android:padding="#dimen/dp5"
android:text=""
android:textAlignment="center"
android:textColor="#color/colorPrimary" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:paddingLeft="#dimen/dp10"
android:progress="100"
android:progressTint="#android:color/white" />
</RelativeLayout>
<TextView
android:id="#+id/noTestimonial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/dp20"
android:layout_marginTop="#dimen/dp20"
android:gravity="center"
android:text="#string/no_testimonials"
android:textColor="#android:color/white"
android:visibility="gone" />
<ListView
android:id="#+id/listCustomer_testimonial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp10"
android:divider="#android:color/white">
</ListView>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_marginTop="#dimen/dp5"
android:background="#color/colorPrimary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp10"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp5"
android:text="Years of experience:"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/txtAdExperience"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp8"
android:text="2" />
</LinearLayout>
<TextView
android:id="#+id/txtExpertise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/dp5"
android:text="#string/expertise"
android:textColor="#android:color/black"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/dp5"
android:orientation="horizontal">
<TextView
android:id="#+id/btnProtect_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/protect_money"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:visibility="gone" />
<TextView
android:id="#+id/btnGrow_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/grow_money"
android:textColor="#android:color/white"
android:visibility="gone" />
<TextView
android:id="#+id/btnMedical_Needs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp5"
android:background="#drawable/bg_skyblue"
android:padding="#dimen/dp5"
android:text="#string/medical_need"
android:textColor="#android:color/white"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/dp15"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp10">
<TextView
android:id="#+id/txtSocialMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/find_me_on_social_media"
android:textColor="#android:color/black"
android:textSize="#dimen/dp15" />
<ImageView
android:id="#+id/imgFB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/fb_icon" />
<ImageView
android:id="#+id/imgYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgFB"
android:src="#drawable/youtube_icon" />
<ImageView
android:id="#+id/imgLinkden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgYoutube"
android:src="#drawable/linkedin_icon" />
<ImageView
android:id="#+id/imgRSS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtSocialMedia"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp10"
android:layout_toRightOf="#id/imgLinkden"
android:src="#drawable/rss_icon" />
<ImageView
android:id="#+id/imgCartoon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/txtSocialMedia"
android:src="#drawable/man_after_feedback" />
<ImageView
android:id="#+id/imgRead_my_articles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/imgFB"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/read_my_articles" />
<ImageView
android:id="#+id/imgClick_for_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/imgFB"
android:layout_marginRight="#dimen/dp10"
android:layout_marginTop="#dimen/dp10"
android:src="#drawable/click_for_feedback" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="#+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
Inside this code I have ListView with id "listCustomer_testimonial".
Never use ListView inside Scrollview. But if you must use ListView inside ScrollView create a scrollhandler.
public class scrollhandler {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
int rowheight=56;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
rowheight=listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight)+","+myListAdapter.getCount());
}
}
Then lastly bind the ListView with the Handler
lv.setAdapter(adapter);
scrollhandler.getListViewSize(lv);
Your problem is not with the getCount() method or list items count.
You have used listview inside scrollview, so only one item will be visible and others can be seen on scroll.
You can use custom Listview to avoid issue in height of listview,
public class NonScrollableListview extends ListView {
public NonScrollableListview(Context context) {
super(context);
}
public NonScrollableListview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollableListview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
In your xml ,
<com.yourpack.NonScrollableListview
android:id="#+id/list_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Or use NestedScrollView with RecyclerView instead of listview.
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:fillViewport="true">
<!--otherviews-->
<android.support.v7.widget.RecyclerView
android:id="#+id/list_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--otherviews-->
</android.support.v4.widget.NestedScrollView>

Remove one layout from listview dynamically

I am working on an application in which I am using custom listview and for my listview I have define a row layout for customisation.
In my row layout I have two Relative layouts.
I want to dynamically remove that one Relative layout from my listview and then add it again.
Please help me how can I do that. I have added a screen shot of my layout you can see here
Below is my Row layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/r2_imageslayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_persontext"
android:text="Per Person"
android:textSize="15sp"
android:textColor="#color/toolbar_color"
android:layout_marginLeft="150sp"
android:layout_marginTop="#dimen/size_8"
/>
<TextView
android:layout_width="100sp"
android:layout_height="wrap_content"
android:id="#+id/result2_price"
android:text="$568"
android:textSize="20sp"
android:textStyle="bold"
android:background="#color/yellow"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/size_10"
android:layout_marginTop="#dimen/size_5"
android:paddingLeft="#dimen/size_15"
/>
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:contentDescription="#null"
android:layout_marginLeft="#dimen/size_10"
android:layout_marginTop="#dimen/size_30"
android:id="#+id/r2_departairlineimage"
android:src="#drawable/dl"
/>
<TextView
android:layout_width="40sp"
android:layout_height="20sp"
android:id="#+id/result2_flightcode"
android:text="SG250"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_below="#+id/r2_departairlineimage"
/>
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:id="#+id/r2_depart_image"
android:layout_toRightOf="#+id/r2_departairlineimage"
android:layout_marginLeft="#dimen/size_15"
android:layout_marginTop="35sp"
android:src="#drawable/depart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_departtime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_depart_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="36sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_dep_citycode"
android:text="Bos"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_departtime"
android:layout_marginTop="38sp"
android:layout_marginLeft="#dimen/size_3"
/>
<ImageView
android:layout_width="80sp"
android:layout_height="wrap_content"
android:id="#+id/r2_image"
android:contentDescription="#null"
android:src="#drawable/onestop_line"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_marginTop="42sp"
android:layout_marginLeft="#dimen/size_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_1stoptext"
android:text="1 stop"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_marginTop="32sp"
android:layout_marginLeft="#dimen/size_30"
/>
<ImageView
android:layout_width="10sp "
android:layout_height="10sp"
android:id="#+id/r2_departclock"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_below="#+id/r2_1stoptext"
android:layout_marginTop="5sp"
android:src="#drawable/time_image"
android:layout_marginLeft="#dimen/size_30"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15sp"
android:id="#+id/r2_dep_flightduration"
android:text="5h 60m"
android:textSize="12sp"
android:layout_below="#+id/r2_1stoptext"
android:layout_marginTop="#dimen/size_1"
android:layout_toRightOf="#+id/r2_departclock"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_arrivetime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="36sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_arrive_citycode"
android:hint="Was"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_arrivetime"
android:layout_marginTop="38sp"
android:layout_marginLeft="#dimen/size_3"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/result2_returnlayout" >
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:contentDescription="#null"
android:layout_marginLeft="#dimen/size_10"
android:layout_marginTop="#dimen/size_80"
android:id="#+id/r2_return_airlineimage"
android:src="#drawable/dl"
/>
<TextView
android:layout_width="40sp"
android:layout_height="20sp"
android:id="#+id/result2_returnflightcode"
android:text="SG250"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_below="#+id/r2_return_airlineimage" />
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:id="#+id/r2_return_image"
android:layout_toRightOf="#+id/r2_return_airlineimage"
android:layout_marginLeft="#dimen/size_15"
android:layout_marginTop="80sp"
android:src="#drawable/return_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_deptime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_return_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="80sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_depcitycode"
android:text="Bos"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_return_deptime"
android:layout_marginTop="82sp"
android:layout_marginLeft="#dimen/size_3"/>
<ImageView
android:layout_width="80sp"
android:layout_height="10sp"
android:id="#+id/r2_ret_image"
android:contentDescription="#null"
android:src="#drawable/nonstop"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_marginTop="85sp"
android:layout_marginLeft="#dimen/size_10"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_nonstoptext"
android:text="Non stop"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_marginTop="75sp"
android:layout_marginLeft="#dimen/size_25" />
<ImageView
android:layout_width="10sp "
android:layout_height="10sp"
android:id="#+id/r2_ret_clock"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_below="#+id/r2_return_nonstoptext"
android:layout_marginTop="5sp"
android:src="#drawable/time_image"
android:layout_marginLeft="#dimen/size_30"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15sp"
android:id="#+id/r2_ret_flightduration"
android:text="5h 60m"
android:textSize="12sp"
android:layout_marginTop="#dimen/size_1"
android:layout_below="#+id/r2_return_nonstoptext"
android:layout_toRightOf="#+id/r2_ret_clock"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_ret_arrivetime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_ret_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="80sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_ret_arrivecitycode"
android:text="Was"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_ret_arrivetime"
android:layout_marginTop="82sp"
android:layout_marginLeft="#dimen/size_3"/>
</RelativeLayout>
</RelativeLayout>
That's my adapter code:
Result2Adapter(Context context, List list_row)
{
// super(context, resourceID, list_row);
this.context=context;
this.list_row=list_row;
}
#Override
public int getCount() {
return list_row.size();
}
#Override
public Custom_Result getItem(int position) {
return list_row.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder
{
ImageView DepAirline_logo, Ret_Airlinelogo;
TextView Dep_Airline_code,dep_time,dep_arr_time,dep_stops,dep_duration,Dep_dep_citycode,Dep_arrivecitycode,Ret_dep_citycode,Ret_arrivecitycode,Ret_Airline_code,Ret_time,Ret_arr_time,
Ret_stops,Ret_duration,price;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView=inflater.inflate(R.layout.result2_rowlayout,null);
holder=new ViewHolder();
holder. DepAirline_logo=(ImageView) convertView.findViewById(R.id.r2_departairlineimage);
holder.Dep_Airline_code=(TextView) convertView.findViewById(R.id.result2_flightcode);
holder.dep_time=(TextView) convertView.findViewById(R.id.r2_departtime);
holder.dep_arr_time=(TextView) convertView.findViewById(R.id.r2_arrivetime);
holder.Dep_arrivecitycode=(TextView) convertView.findViewById(R.id.r2_arrive_citycode);
holder.dep_stops=(TextView) convertView.findViewById(R.id.r2_1stoptext);
holder.dep_duration=(TextView) convertView.findViewById(R.id.r2_dep_flightduration);
holder.price=(TextView) convertView.findViewById(R.id.result2_price);
holder.Dep_dep_citycode=(TextView) convertView.findViewById(R.id.r2_dep_citycode);
holder.Ret_Airlinelogo = (ImageView) convertView.findViewById(R.id.r2_return_airlineimage);
holder.Ret_Airline_code = (TextView) convertView.findViewById(R.id.result2_returnflightcode);
holder.Ret_time = (TextView) convertView.findViewById(R.id.r2_return_deptime);
holder.Ret_arr_time = (TextView) convertView.findViewById(R.id.r2_ret_arrivetime);
holder.Ret_arrivecitycode = (TextView) convertView.findViewById(R.id.r2_ret_arrivecitycode);
holder.Ret_dep_citycode = (TextView) convertView.findViewById(R.id.r2_return_depcitycode);
holder.Ret_stops = (TextView) convertView.findViewById(R.id.r2_return_nonstoptext);
holder.Ret_duration = (TextView) convertView.findViewById(R.id.r2_ret_flightduration);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
String retimagename=list_row.get(position).getDep_Airline_code().toLowerCase();
String retpath="drawable/"+retimagename;
int retimageresource=context.getResources().getIdentifier(retpath, null, context.getPackageName());
String imagename=list_row.get(position).getRet_Airline_code().toLowerCase();
String path="drawable/"+imagename;
int imageresource=context.getResources().getIdentifier(path, null, context.getPackageName());
if(imageresource==0)
{
imageresource=R.drawable.flight_icon;
}
if(retimageresource==0)
{
imageresource=R.drawable.flight_icon;
}
Drawable image=context.getResources().getDrawable(imageresource);
Drawable retimage=context.getResources().getDrawable(imageresource);
holder.DepAirline_logo.setImageDrawable(image);
holder.Ret_Airlinelogo.setImageDrawable(retimage);
holder.Dep_Airline_code.setText(list_row.get(position).getDep_Airline_name());
holder.Ret_Airline_code.setText(list_row.get(position).getRet_Airline_name());
holder.dep_time.setText(list_row.get(position).getDepart_time());
holder.Ret_time.setText(list_row.get(position).getRet_dep_time());
holder.dep_arr_time.setText(list_row.get(position).getArrive_time());
holder.Ret_arr_time.setText(list_row.get(position).getRet_arr_time());
holder.dep_stops.setText(list_row.get(position).getDep_stops());
holder.Ret_stops.setText(list_row.get(position).getRet_stops());
holder.dep_duration.setText(list_row.get(position).getDep_duration());
holder.Ret_duration.setText(list_row.get(position).getRet_duration());
holder.Dep_arrivecitycode.setText(list_row.get(position).getDep_CityCode());
holder.Ret_arrivecitycode.setText(list_row.get(position).getArr_citycode());
holder.Ret_dep_citycode.setText(list_row.get(position).getRet_dep_citycode());
holder.Ret_arrivecitycode.setText(list_row.get(position).getRet_arr_citycoode());
holder.price.setText(list_row.get(position).getPrice());
return convertView;
}
You could do that in the adapter class's getView method.
For a particular row by inflating the view and getting the relative layout by using findViewById().
e.g.,
RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayoutId);
You can hide the layout by using
relativeLayout1.setVisibility(View.GONE);
and unhide using
relativeLayout1.setVisibility(View.VISIBLE);

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 :)

Categories

Resources