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);
}
}
Related
I am beginner in Android. My small app is working fine but its UI is looking ugly. Could you please help me make it better? For example: its image looks so big, could we make it a bit smaller.
Here is the code snippet.
MainActivity.java
package com.example.hacback17.listviewwithimagesadvanced;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] memeTitle;
String[] memeDescription;
// getting ids of images from drawable
int[] images = {R.drawable.number_one,R.drawable.number_two, R.drawable.number_three, R.drawable.number_four, R.drawable.number_five,
R.drawable.number_six, R.drawable.number_seven, R.drawable.number_eight, R.drawable.number_nine, R.drawable.number_ten};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources = getResources();
memeTitle = resources.getStringArray(R.array.titles);
memeDescription = resources.getStringArray(R.array.titles);
// Getting the reference of the ListView
list = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, memeTitle, images, memeDescription );
list.setAdapter(adapter);
}
}
// Custom ArrayAdapter
class MyAdapter extends ArrayAdapter<String>
{
Context context;
int[] images; // getting the reference of images.
String[] titleArray; // getting the reference of titles array
String[] descriptionArray; // getting the reference of description array
public MyAdapter(Context context, String[] titles, int[] img, String[] description) {
super(context, R.layout.single_row, R.id.textView, titles);
this.context = context;
this.images = img;
this.titleArray = titles;
this.descriptionArray = description;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Let's get the reference of LayoutInflater to get the XML into Java code.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
TextView myTitle = (TextView) row.findViewById(R.id.textView);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.hacback17.listviewwithimagesadvanced.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
single_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="match_parent">
<ImageView
android:src="#drawable/number_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignTop="#+id/imageView"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_alignEnd="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_below="#+id/textView" />
</RelativeLayout>
The UI looks like this:
In your ArrayAdapter use these funcitons:
myImage.setMaxWidth(MAX_WIDTH);
myImage.setMaxHeight(MAX_HEIGHT);
Use your own values in MAX_WIDTH & MAX_HEIGHT.
make your image layout widht and height wrap_content to 30dp.
<ImageView
android:src="#drawable/number_one"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
I have a DrawerLayout, my DrawerLayout currently display a listview that have an image and a text, and everything just works fine
Now, I decides to add an header in my DrawerLayout and add a special field for a few Rows in my listview
Thank you
this image tells everything about what i want to do:
http://8pic.ir/images/als4ye1elyo35tpju7hq.png
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/MainDrawerFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="#+id/MainDrawerListView"
android:layout_width="270dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:divider="#null"
android:background="#color/DrawerBackGround" />
</android.support.v4.widget.DrawerLayout>
drawer_listview.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="45dp">
<ImageView
android:id="#+id/DrawerIcon"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:contentDescription="#null"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/DrawerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_centerVertical="true"
android:textColor="#color/DrawerFontColor"
android:layout_toLeftOf="#+id/DrawerIcon"
android:layout_toStartOf="#+id/DrawerIcon" />
</RelativeLayout>
CustomListAdapter.java
import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String>
{
private final Activity ActivityContext;
private final String[] ItemName;
private final Integer[] ImageID;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid)
{
super(context, R.layout.drawer_listview, itemname);
this.ActivityContext = context;
this.ItemName = itemname;
this.ImageID = imgid;
}
#Override
public View getView(int position, View Unused1, ViewGroup Unused2)
{
LayoutInflater inflater = ActivityContext.getLayoutInflater();
View view = inflater.inflate(R.layout.drawer_listview, null);
ImageView DrawerIcon = (ImageView) view.findViewById(R.id.DrawerIcon);
TextView DrawerText = (TextView) view.findViewById(R.id.DrawerText);
Typeface FontFace = Typeface.createFromAsset(ActivityContext.getAssets(), "Yekan.ttf");
DrawerText.setTypeface(FontFace);
DrawerIcon.setImageResource(ImageID[position]);
DrawerText.setText(ItemName[position]);
return view;
};
}
MainActivity.java
http://paste.ubuntu.com/15237990/
Do you want your header to scroll off the top of the screen when the user scrolls the listview? If so, simply add a header to the listview using the addHeaderView API.
If you want your header to stay in place, add it in the XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header text"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
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.
I can't see the row that I've added to a list view and am wondering if it has something to do with my layout or ListViewAdapter. In debug I can see that my array is filled with the data passed to my ArrayList playerList, and I see the data in Toast but nothing shows up on screen.
MainActivity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/txtV_battingOrder_Title"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddButton_title" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.89" >
</ListView>
</LinearLayout>
New Row Layout:
<?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:layout_weight="1"
android:background="#android:color/transparent"
android:padding="5dp" >
<TextView
android:id="#+id/txtV_player_input_position_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="21dp"
android:width="180dp" />
<Button
android:id="#+id/btn_Edit_Title"
style="?android:attr/buttonStyleSmall"
android:layout_width="#dimen/btn_edit_player_width"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtV_player_input_position_title"
android:layout_alignBottom="#+id/txtV_player_input_position_title"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:padding="3dp"
android:text="#string/bnt_Edit_title" />
</RelativeLayout>
Custom ListViewAdapter
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<String> playerList;
public CustomListViewAdapter(Activity context, List<String> playerList) {
super();
this.playerList = playerList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return playerList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder {
public TextView text;
public Button edit;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String player = playerList.get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.new_player_view, null);
ViewHolder holder = new ViewHolder();
// assign components from new_player_view
// TextView playerInfo = (TextView)vi.findViewById(R.id.txtV_player_input_position_title);
holder.text = (TextView)vi.findViewById(R.id.txtV_player_input_position_title);
holder.edit = (Button)vi.findViewById(R.id.btn_Edit_Title);
// Button editButton = (Button) vi.findViewById(R.id.btn_Edit_Title);
holder.edit.setOnClickListener(MainActivity.editPlayerButtonListener);
holder.text.setText(player);
//playerInfo.setText("this is a test");
vi.setTag(holder);
return vi;
}
First of all, you are using too much the weight property in your xml.. you should use it wisely, I don't know why the RelativeLayout has the weight 1 but anyway, I think the issue you have might be one of the following:
make sure getCount() returns > 0
make sure your are using this method to do the inflate: inflater.inflate(R.layout.new_player_view, parent, false);
change the row layout to the follwing:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp" >
<TextView
android:id="#+id/txtV_player_input_position_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"/>
<Button
android:id="#+id/btn_Edit_Title"
style="?android:attr/buttonStyleSmall"
android:layout_width="#dimen/btn_edit_player_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:padding="3dp"
android:text="#string/bnt_Edit_title" />
And the main layout ListView to:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I hope something will help you, otherwise, please post the full code of the adapter.
EDIT:
Please try to look at this example of ViewHolder pattern implementation, that is working and it should help you implement yours better.
You have set layout_weight as 0.89 without setting the weightSum for the parent layout. Set android:weightSum = "1" for your parent LinearLayout
I'm trying to set a Listview under some other Widgets (Buttons, editText, etc). I don't want to use another activity for the listview. After reading some I found How can I implement a ListView without ListActivity? (use only Activity) and I tried to do it, ending up with:
Here is my main.xml:
<LinearLayout android:id="#+id/relativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView ...[some code].../>
<EditText ...[some code].../>
<ImageButton ...[some code].../>
<Chronometer ..[some code]..../>
<ListView android:id="#+id/listView1" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
here is my onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (ImageButton) findViewById(R.id.button1);
mChronometer = (Chronometer) findViewById(R.id.chronometer1);
editText1 = (EditText) findViewById(R.id.editText1);
ListView lv = (ListView) findViewById(R.id.listView1);
String[] listword = new String[] {"Hello","World","Foo","Bar"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listword));
}
and here is list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
When I debug in my physical device, the application simply stays all black. If I comment out the lines of onCreate() that involve the list, the application works (obviously without the listview).
Any ideas what might be wrong?
I actually have an app with a listview below some TextViews and above two buttons, let me grab my code!
Here's my activity with listview below the textviews and above the buttons (with quite a bit removed for brevity):
public class BNYDirectoryResults extends Activity{
public static String[] stuff;
ListView list;
BNYAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultscreen);
TextView headDisplay = (TextView)findViewById(R.id.results);
TextView headCount = (TextView)findViewById(R.id.resultsTotal);
TextView headPages = (TextView)findViewById(R.id.pages);
//Set up the results list, see BNYAdapter
list = (ListView)findViewById(R.id.list);
adapter = new BNYAdapter (this, BNYDirectory.ReturnResults);
list.setAdapter(adapter);
//Sets up header information (pages, total results)
//Just some stuff to change the TextViews
//Passes EmployeeID and Phone Number to AND opens the details page
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String EID = BNYDirectory.ReturnResults[position][0];
String phoneNumber = BNYDirectory.ReturnResults[position][2];
BNYDirectoryTransaction.doDetails(EID, phoneNumber);
Intent i = new Intent(view.getContext(), BNYDirectoryDetails.class);
startActivity(i);
}
});
}
}
Here's the XML file for it:
<TextView
android:id="#+id/results"
android:text="Name"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/resultsTotal"
android:text="Phone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="#+id/pages"
android:text="AIM"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:text="Name"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/phone"
android:text="Phone"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/aim"
android:text="AIM"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/dept"
android:text="Dept"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_weight="1"/>
<Button
android:id="#+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
and here's the custom adapter for the list:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BNYAdapter extends BaseAdapter {
private Activity activity;
private String[][] results;
private static LayoutInflater inflater=null;
public BNYAdapter(Activity a, String[][]info) {
activity = a;
results = info;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return results.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView name;
public TextView phone;
public TextView aim;
public TextView dept;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.nameItem);
holder.phone=(TextView)vi.findViewById(R.id.phoneItem);
holder.aim=(TextView)vi.findViewById(R.id.aimItem);
holder.dept=(TextView)vi.findViewById(R.id.deptItem);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.name.setText(BNYDirectory.ReturnResults[position][1]);
holder.phone.setText(BNYDirectory.ReturnResults[position][2]);
holder.aim.setText(BNYDirectory.ReturnResults[position][3]);
holder.dept.setText(BNYDirectory.ReturnResults[position][4]);
return vi;
}
}
and all together that makes a page like
You can use your own layout with a ListActivity. Make your activity extend ListActivity, create your own layout and make sure that the ListView has android:id="#android:id/list" (this is how the ListActivity links to the ListView in your own layout), then in onCreate set your layout with setContentView.