Here is the Adapter class that doesn't work properly.
It always shows me 3 rows even if I'm passing 5 values in List as an parameter.
Can anyone tell me where is the problem in my adapter class.
Any help will be greatly appreciated.
Thanks:)
import java.util.ArrayList;
import java.util.List;
import com.example.mis.R;
import com.mis.adapter.Chk_Model;
import com.mis.database.DatabaseHandler;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class ThreeTextViewAdapter extends ArrayAdapter<String> {
Context context;
List<String> orderNum=new ArrayList<String>();
TextView txtOrd1,txtShip1,txtStatus;
DatabaseHandler handler;
public ThreeTextViewAdapter(Context context, List<String> ordNo) {
// TODO Auto-generated constructor stub
super(context, R.layout.threetextview,ordNo);
this.context=context;
this.orderNum=ordNo;
handler=new DatabaseHandler(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
final int pos = position;
if (convertView == null) {
viewHolder=new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.threetextview, parent, false);
viewHolder.txtOrd1=(TextView)convertView.findViewById(R.id.txtfirst_detail_expo);
viewHolder.txtShip1=(TextView)convertView.findViewById(R.id.txtsec_detail_expo);
viewHolder.txtStatus=(TextView)convertView.findViewById(R.id.txtthird_detail_expo);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView
.getTag();
}
viewHolder.txtOrd1.setText(orderNum.get(pos));
handler.getReadableDatabase();
Cursor cursor=handler.getShipNo(orderNum.get(pos));
handler.closeDatabase();
String shipNo=cursor.getString(0);
if(shipNo.equals(null))
{
viewHolder.txtShip1.setText("Not Exported");
viewHolder.txtStatus.setText("Failed");
}
else
{
viewHolder.txtShip1.setText(shipNo);
System.out.println("y"+shipNo);
viewHolder.txtStatus.setText("Passed");
System.out.println("z"+"Passed");
}
return convertView;
}
private static class ViewHolder
{
TextView txtOrd1,txtShip1,txtStatus;
}
}
Here, I have attached my code where I'm initializing my adapter.
I have converted Set to list.
Set<String> keySet = ordShipNo.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String key = (String) it.next();
ordExported.add(ordShipNo.get(key));
}
adapter = new ThreeTextViewAdapter(this, ordExported);
lststatus.setAdapter(adapter);
adapter.notifyDataSetChanged();
Here is my Xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blue_2"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/scroll_full_mpr"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="189dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mpr_mpr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#267ad4"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="#+id/txt_OrderLstOrdNo_mpr"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:text="#string/pn"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_ShipNo_mpr"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/receipt_no"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_status_mpr"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/st"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefullresult_mpr"
android:layout_width="fill_parent"
android:layout_height="400dp" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/btn_mseOkResult_mpr"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/ok" />
</RelativeLayout>
And this the custom layout of 3 textviews.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="175dp"
>
<TextView
android:id="#+id/txtfirst_detail_expo"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:text="" />
<TextView
android:id="#+id/txtsec_detail_expo"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/txtfirst_detail_expo"
android:textSize="20dp"
android:text="" />
<TextView
android:id="#+id/txtthird_detail_expo"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:text="" />
</RelativeLayout>
As Jorge suggest, overRide getCount of your adapter like this and return the size of the list.
#Override
public int getCount() {
return orderNum.size();
}
and if it failed to solve the problem, then give me your code where you setting the adapter to the listView.
Related
I have a list view that for each item needs to display an image, a book title, the author and the price. Under normal circumstances my output is fine and looks like the following
My client has a large portion of customers that need to set the font size to huge. When this is done and the authors name is very long my output looks like below
This is not right and I would like it to look like this instead
In other words the height of list view and elements inside grow to fit larger amounts of text.
Here is the code which replicates my problem.
The book class
public class Book {
public String name;
public String author;
public String price;
public Book(String name, String author, String price){
this.name = name;
this.author = author;
this.price = price;
}
}
The main activity code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
ArrayList<Book> bookList = new ArrayList<Book>();
bookList.add(new Book("Book 1", "By Christopher Christopheson", "£0.50"));
bookList.add(new Book("Book 2", "Author 2", "£0.75"));
BookAdapter adapter = new BookAdapter(this, R.layout.row, bookList);
listView.setAdapter(adapter);
}
}
The book list view adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import android.widget.TextView;
public class BookAdapter extends ArrayAdapter<Book> {
private ArrayList<Book> items;
private LayoutInflater lInflater;
public BookAdapter(Context context, int textViewResourceId, ArrayList<Book> items){
super(context, textViewResourceId, items);
lInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
v = lInflater.inflate(R.layout.row, null);
}
Book b = items.get(position);
if(b!= null){
TextView bookName = (TextView)v.findViewById(R.id.book_name);
TextView bookPrice = (TextView)v.findViewById(R.id.book_price);
TextView bookAuthor = (TextView)v.findViewById(R.id.book_author);
if(bookName != null){
bookName.setText(b.name);
}
if(bookPrice != null){
bookPrice.setText(b.price);
}
if(bookAuthor != null){
bookAuthor.setText(b.author);
}
}
return v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="barnetttabs.com.basiclistview.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
>
<TextView
android:id="#+id/price_background"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#0000ff"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:minHeight="150dp"
android:layout_toLeftOf="#id/price_background"
android:layout_alignParentLeft="true"
android:gravity="top"
android:background="#d3d3d3"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:paddingLeft="110dp"
android:paddingRight="4dp"
/>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/lvplaceholderimage"
/>
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/book_image"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/book_price"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
I've tried fixing the layout_height of the relative layout in row.xml. This keeps the height consistent but when the author is too long some of the text is hidden. When I've searched for solutions on this most things say the solution is to set height as wrap_content which I have done but it doesn't the problem.
Is there a way to fix this through either changing the xml layout or the java code? Maybe the relative layout I've used here in row.xml is not the right approach?
Chetan's suggestion was the way to go. The basic idea is to change from a relative layout to a horizontal linear layout. The middle portion is given weight 0 and the 2 end bits have their lengths set as before. The only drawback is the method needs to have a nested layout to get the blue bit to work but hopefully not too much of a performance hit.
In activity_main.xml change the listview code to
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="5.0sp"
android:layout_width="match_parent">
</ListView>
Change row.xml to
<?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:minHeight="150dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:background="#d3d3d3"
>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lvplaceholderimage"
android:layout_gravity="top"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_gravity="top"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
/>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:background="#0000ff">
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignParentTop="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_below="#id/book_price"
android:layout_alignParentBottom="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
</LinearLayout>
Then it looks like this
i am developing application for home security. One of the feature, that must be implemented is the ability to see connected devices (for sending notifications, blocking access, etc). So far, i've been able to create RecyclerView list of the devices, everything is perfect (for me), except that cards in this list have no spacing between them.
Screenshot of how it looks now
device_data_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="#+id/deviceIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="8dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignBottom="#+id/deviceIcon"
android:layout_toEndOf="#+id/deviceIcon">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="#+id/deviceUsername"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Model"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:id="#+id/deviceModel"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Version"
android:gravity="center_vertical|right"
android:id="#+id/deviceVersion"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
devices_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_vertical_margin">
<view
android:id="#+id/connectedDevicesList"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
View Holder Code
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.exampleapp.R;
public class DeviceViewHolder extends RecyclerView.ViewHolder {
protected ImageView deviceIcon;
protected TextView deviceUsername;
protected TextView deviceModel;
protected TextView deviceVersion;
public DeviceViewHolder(View view) {
super(view);
this.deviceIcon = (ImageView) view.findViewById(R.id.deviceIcon);
this.deviceUsername = (TextView) view.findViewById(R.id.deviceUsername);
this.deviceModel = (TextView) view.findViewById(R.id.deviceModel);
this.deviceVersion = (TextView) view.findViewById(R.id.deviceVersion);
}
}
RecyclerView Adapter
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import com.exampleapp.API.AuthorizedDevice;
import com.exampleapp.R;
public class ConnectedDeviceAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
private List<AuthorizedDevice> devices;
private Context mContext;
public ConnectedDeviceAdapter(Context context, List<AuthorizedDevice> devices) {
this.devices = devices;
this.mContext = context;
}
#Override
public DeviceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
DeviceViewHolder viewHolder = new DeviceViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(DeviceViewHolder deviceViewHolder, int i) {
AuthorizedDevice device = devices.get(i);
if (device.getDeviceId().equals("0")) {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.android_icon));
} else {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.apple_icon));
}
deviceViewHolder.deviceUsername.setText(device.getUsername());
deviceViewHolder.deviceModel.setText(device.getDeviceName());
deviceViewHolder.deviceVersion.setText(device.getDeviceVersion());
}
#Override
public int getItemCount() {
return (null != devices ? devices.size() : 0);
}
}
Can anyone please help me solve this problem? Thank you very much in advance!
In your RecyclerView Adapter onCreateViewHolder:
replace:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
with:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, viewGroup, false);
Or you can use RecyclerView.addItemDecoration(ItemDecoration decor).
I am creating a student details application in android.I need to display student's photo at the top and display name,roll number,address etc as a table.I added a imageview at the top and listview just below the image view.Can i add the image inside the list view ( Now image is not scrollable ).
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/def"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="#id/list"
android:layout_above="#+id/ad_view"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Activity.java
list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> productsList;
productsList = new ArrayList<HashMap<String, String>>();
/*
adding data from database to productsList
*/
ListAdapter adapter =
new SimpleAdapter(this, productsList, R.layout.full, new String[]{"left", "right"},
new int[]{R.id.left, R.id.right});
list.setAdapter(adapter);
full.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="#drawable/background_border"
android:padding="15dp"
>
<TextView
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="cnsdsdsf:"
android:layout_weight="1"
android:gravity="center"
/>
<TextView
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1"
android:gravity="center"
/>
</LinearLayout>
you can use
ImageView imageView = new ImageView(this);
listView.addHeaderView(imageView);
to add a imageView on the head of listview
You can treat the top row or any other row as special for the image. in getView() inflate with separate layout for that image according to position.
You can also modify your item list layout file (full.xml) and create a custom listview adapter. This method will give you more freedom in terms of design. You can play around with this full.xml
full.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="64dp"
android:paddingRight="32dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
tools:background="#ffaa00">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/studentPicture"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/student_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/studentPicture"
android:layout_toEndOf="#+id/studentPicture"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/left"
android:layout_toEndOf="#+id/left"
android:textColor="#ffffffff"
android:textSize="20sp"
android:paddingLeft="10dp" />
</RelativeLayout>
Then you create a adapter that will use this list item layout file:
StudentsAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class StudentsAdapter extends BaseAdapter{
private Context mContext;
private String[] mNames;
private String[] mOtherInfo;
public StudentsAdapter (Context context, String[] mNames, String[] mOtherInfo) {
mContext = context;
mNames = students;
mOtherInfo = mOtherInfo;
}
#Override
public int getCount() {
return mNames.length;
}
#Override
public Object getItem(int position) {
return mNames[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
//brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.full, null);
holder = new ViewHolder();
holder.studentImageView = (ImageView) convertView.findViewById(R.id.studentPicture);
holder.left= (TextView) convertView.findViewById(R.id.left);
holder.right= (TextView) convertView.findViewById(R.id.right);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//here you set the picture, name, etc.
holder.studentImageView.setImageResource(/* your method to find specific image view */);
holder.left.setText(mNames[position]);
holder.right.setText(mOtherInfo[position]);
return convertView;
}
private static class ViewHolder {
ImageView studentImageView;
TextView left;
TextView right;
}
}
Your activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="No data to display"
android:textColor="#ffffffff"/>
</RelativeLayout>
And now implement this adapter in your Activity.java
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
public class Activity extends Activity {
//you will need to set this data
private String[] mNames;
private String[] mOtherInfo;
ListView mListView;
TextView mEmptyTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mListView = (ListView) findViewById(android.R.id.list);
mEmptyTextView = (TextView) findViewById(android.R.id.empty);
DayAdapter adapter = new DayAdapter(this, mNames, mOtherInfo);
mListView.setAdapter(adapter);
mListView.setEmptyView(mEmptyTextView);
}
}
I'm using recycler view to show items with multiple views. whenever I launch the app some white space is being showed. which is exactly equal to my list items.
I provided the screenshot please go through it.
I think there is no problem with my code.
following is my code
recyclerview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".activities.GroupView">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_group_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
adapter code
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Objects;
import pdfshare.hemanthreddy.com.pdfshare.R;
import pdfshare.hemanthreddy.com.pdfshare.pojo.PdfItemHolder;
import pdfshare.hemanthreddy.com.pdfshare.pojo.RequestPojo;
public class GroupContentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int PDF = 0,REQUEST = 1;
List<Object> list;
OnClickListener onClickListener;
Context context;
public GroupContentAdapter(List<Object> list, OnClickListener onClickListener, Context context) {
this.list = list;
this.onClickListener = onClickListener;
this.context = context;
}
public class PdfHolder extends RecyclerView.ViewHolder {
TextView pdfName,pdfDescription,Helped,uploadedBy;
Button downloadPdf,viewPdf;
View container;
CardView cardView;
public PdfHolder(View itemView) {
super(itemView);
container = itemView;
uploadedBy = (TextView) itemView.findViewById(R.id.pdf_info_uplodedby);
pdfName = (TextView) itemView.findViewById(R.id.pdf_name);
pdfDescription = (TextView) itemView.findViewById(R.id.pdf_description);
Helped = (TextView) itemView.findViewById(R.id.helped);
downloadPdf = (Button) itemView.findViewById(R.id.download_pdf);
viewPdf = (Button) itemView.findViewById(R.id.view_pdf);
cardView = (CardView) itemView.findViewById(R.id.card_view_pdf_row);
}
}
public class RequestHolder extends RecyclerView.ViewHolder {
View container;
TextView name,request;
public RequestHolder(View itemView) {
super(itemView);
container = itemView;
name = (TextView) itemView.findViewById(R.id.requested_name);
request = (TextView) itemView.findViewById(R.id.request);
}
}
public interface OnClickListener{
public void OnItemClick(View view,int position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case PDF:
View v1 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v1);
break;
case REQUEST:
View v2 = inflater.inflate(R.layout.group_request_row_view,parent,false);
viewHolder = new RequestHolder(v2);
break;
default:
View v3 = inflater.inflate(R.layout.pdf_row_view,parent,false);
viewHolder = new PdfHolder(v3);
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder.getItemViewType() == PDF)
{
PdfItemHolder pdfItemHolder = (PdfItemHolder) list.get(position);
PdfHolder pdfHolder = (PdfHolder) holder;
if(!TextUtils.isEmpty(pdfItemHolder.getPdfName())) {
pdfHolder.pdfName.setText(pdfItemHolder.getPdfName());
pdfHolder.pdfDescription.setText(pdfItemHolder.getPdfDescription());
pdfHolder.Helped.setText(pdfItemHolder.getHelped());
pdfHolder.uploadedBy.setText(pdfItemHolder.getUplodedBy());
pdfHolder.downloadPdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "downloading pdf bitch", Toast.LENGTH_LONG).show();
}
});
pdfHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickListener.OnItemClick(v, position);
}
});
}
else
{
Toast.makeText(context,"empty",Toast.LENGTH_LONG).show();
Log.e("empty","empty");
pdfHolder.container.setVisibility(View.GONE);
pdfHolder.pdfName.setVisibility(View.GONE);
pdfHolder.pdfDescription.setVisibility(View.GONE);
pdfHolder.Helped.setVisibility(View.GONE);
pdfHolder.downloadPdf.setVisibility(View.GONE);
pdfHolder.uploadedBy.setVisibility(View.GONE);
pdfHolder.viewPdf.setVisibility(View.GONE);
}
}
else
{
RequestHolder requestHolder = (RequestHolder) holder;
RequestPojo requestPojo = (RequestPojo) list.get(position);
requestHolder.name.setText(requestPojo.getName());
requestHolder.request.setText(requestPojo.getRequest());
}
}
#Override
public int getItemCount() {
return list.size();
}
#Override
public int getItemViewType(int position)
{
if(list.get(position) instanceof PdfItemHolder)
return PDF;
else if(list.get(position) instanceof RequestPojo)
return REQUEST
;
return -1;
}
}
also I checked stack overflow to remove spaces and I added some code to adapter to check wheather the data is empty or not, it didn't worked well.
following Is code of row views`group_row_view code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Requested By : "
android:id="#+id/textView7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/requested_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/request"
android:lines="2"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
code of Pdf_row_view
</LinearLayout>`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="pdfName"
android:id="#+id/pdf_name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp"
android:lines="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="pdf descriptionl"
android:id="#+id/pdf_description"
android:layout_below="#+id/pdf_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:lines="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Pdf"
android:id="#+id/view_pdf"
android:layout_alignTop="#+id/download_pdf"
android:layout_toRightOf="#+id/pdf_info_uplodedby"
android:layout_toEndOf="#+id/pdf_info_uplodedby" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download Pdf"
android:id="#+id/download_pdf"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/pdf_description" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="helped"
android:id="#+id/helped"
android:textSize="15dp"
android:layout_alignBottom="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:layout_alignTop="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"
android:id="#+id/view1"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Upladed by : "
android:id="#+id/pdf_info_uplodedby"
android:layout_below="#+id/view_pdf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
please help me.. thank you
In all of your list item layout files:
pdf_row_view.xml, group_request_row_view.xml, pdf_row_view.xml
set layout_height="wrap_content" of their root layout
Remove the match_parent and set wrap_content as the layout_height instead in the root of your layout of your list items.
For example your pdf_row_view.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view_pdf_row"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="3dp"
card_view:contentPadding="2dp">
<!-- Other elements in your layout -->
</android.support.v7.widget.CardView>
</RelativeLayout>
See, I've set the android:layout_height="wrap_content" in the root RelativeLayout of your pdf_row_view list item.
Do the same for the other list item layouts.
Just set the height of the rootView of specific viewHolder to 0 like this -
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
rootView.setLayoutParams(layoutParams);
where rootView is same as this -
ViewHolder(View view) {
super(view);
rootView = view;
}
Let me know if you want more clarity on this.
I'm trying to implement a ListView with an embedded Button. I've made my list item view separately and followed this tutorial.
When I try running it, I get an NPE on the button's setOnClickListener because it says it's a null reference. Kindly point out where I've gone wrong or if I need to do anything else.
Here's my code:
Main activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Data">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="319dp"
android:layout_height="232dp"
android:id="#+id/imageView"
android:src="#drawable/image" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:id="#+id/editText"
android:autoText="false"
android:inputType="number"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter value"
android:id="#+id/textView"
android:textSize="24dp"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/button"
android:layout_alignBottom="#+id/editText"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:layout_marginLeft="37dp"
android:layout_marginStart="37dp"
android:onClick="bringList"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select reason:"
android:id="#+id/textView2"
android:textSize="24dp"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
List Item:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listitem"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:id="#+id/listbutton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/listitem"
android:textSize="15dp" />
Java code:
package com.example.abhinav.data;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Data extends ActionBarActivity {
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
ListView lv = (ListView)findViewById(R.id.listView);
makeList();
lv.setAdapter(new MyListAdapter(this, R.layout.list, data));
}
private void makeList() {
for(int i = 1; i<6; i++) {
data.add("Theorem"+i);
}
}
public void bringList() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_data, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String> {
private int listlayout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
listlayout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vw = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listlayout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pop up dialog with theorem info
Toast.makeText(getContext(), "Button presss", Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
vw = (ViewHolder) convertView.getTag();
vw.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView title;
Button button;
}
}
Change this Lines
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
to
viewHolder.title = (TextView) convertView.findViewById(R.id.listitem);
viewHolder.button = (Button) convertView.findViewById(R.id.listbutton);
NPE is because you reading view incorrectly.
It seems to me that your List Item might only inflate the TextView element, because it's the actuall root element for the XML.
I recommend wrapping both the TextView and the Button in your list.xml file with a LinearLayout so the actual root view will wrap all your views.
Here's an example of what stacking both views vertically would look like with a LinearLayout for a ListView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="New Text"
android:textSize="24dp" />
<Button
android:id="#+id/listbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/listitem"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Info"
android:textSize="15dp" />
</LinearLayout>
I don't know if this is the case, but you are creating two different ViewHolders. Use vw = new ViewHolder();
And maybe you need to use convertView.findViewById(R.id.listbutton);