Android custom row layout not showing up in listview - android

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

Related

Add a image in Listview Android

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);
}
}

Custom listview doesnot show all data present in my List

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.

Android theme not affecting TextViews inside list item

I have a theme used throughout the app, in the theme the text color is simply declared like this:
android:textColor="#android:color/black"
For a TextView located inside of an activity the color is applied just fine. However when I create a TextView inside of a list item (inflated by an adapter) the text color is white instead.
Does anyone know what style needs to be declared in the theme for list items to be affected?
Full XML for the list item:
<CheckBox
android:id="#+id/lt_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<TextView
android:id="#+id/lt_length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/lt_checkbox"
android:textSize="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/lt_length"
android:layout_alignParentLeft="true"
android:orientation="vertical">
<TextView
android:id="#+id/lt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:textSize="18dp"/>
<TextView
android:id="#+id/lt_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"/>
</LinearLayout>
XML for the list:
<ListView
android:id="#+id/altl_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And the adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LocalTrack track = getItem(position);
ItemHolder holder;
if(convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.listem_track, parent, false);
holder = new ItemHolder();
holder.title = (TextView) convertView.findViewById(R.id.lt_title);
holder.length = (TextView) convertView.findViewById(R.id.lt_length);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.lt_checkbox);
convertView.setTag(holder);
}
else
holder = (ItemHolder) convertView.getTag();
holder.title.setText(track.getTitle());
holder.length.setText(Helpbot.convertMillisToTrackLength(track.getLength()));
holder.checkbox.setChecked(mSelectedTracks.contains(track));
return convertView;
}
This happens because you are inflating the row layout of your ListView with the Context of your Application (LayoutInflater.from(getApplicationContext()); or some other derivative), instead of using your Activity's Context.
The difference between the Context you will get from getApplicationContext() and Activity.this lays in the implementation of Context for either of them.
Both Context, the Application's Context and the Activity's gets wrapped inside a ContextWrapper and proxy their calls to an internal "Base" Context.
The difference here is that the Application class is a singleton and only has one global implementation for your whole Application, while each Activity - since inheriting from ContextWrapper as well - has it's own unique implementation for each Activity.
Now that's why getApplication() doesn't - and can't - obey the rules of the themes you defined in your manifest, but the Context provided by your Activity does/can.
I highly recommend you to read Dave Smith's article about Contexts.
item_list_rs.xml
This is use in ListAdapter for listview designe
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/image_rs"
android:layout_width="80dip"
android:layout_height="100dip"
android:background="#drawable/image_bg"
android:src="#drawable/ic_launcher" /> ///////
</LinearLayout>
<TextView
android:id="#+id/nama_rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Dilip Birajadar"
android:textColor="#3dd63d"
android:textSize="12dip"
android:textStyle="bold"
android:typeface="sans" /> /////////
<TextView
android:id="#+id/link_image_rs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/id_rs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/tv_seling_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_alignLeft="#+id/nama_rs"
android:text="Selling Price"
android:textSize="12dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/tv_measuring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nama_rs"
android:layout_centerVertical="true"
android:text="Additional-"
android:textColor="#ad30f6"
android:textSize="12dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/alamat_rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/telepon_rs"
android:layout_centerVertical="true"
android:text="Information:"
android:textColor="#000000"
android:textSize="12dip" />
<TextView
android:id="#+id/telepon_rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_seling_price"
android:layout_alignBottom="#+id/tv_seling_price"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tv_seling_price"
android:text="Selling_price"
android:textColor="#ff0000"
android:textSize="11dip"
android:textStyle="bold" />
</RelativeLayout>
ListAdapter.Java
import java.util.ArrayList;
import java.util.HashMap;
import android.annotation.SuppressLint;
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.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public int[] daftar_rs;
public ListAdapter(Pay_Per_View a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item_list_rs, null);
/*TextView id_rs = (TextView) vi.findViewById(R.id.id_rs);*/
TextView Image_name = (TextView) vi.findViewById(R.id.nama_rs);
TextView link_image_rs = (TextView) vi.findViewById(R.id.link_image_rs);
TextView movie_price = (TextView) vi.findViewById(R.id.telepon_rs);
/* TextView telepon_rs = (TextView) vi.findViewById(R.id.alamat_rs);*/
ImageView thumb_image = (ImageView) vi.findViewById(R.id.image_rs);
HashMap<String, String> daftar_rs = new HashMap<String, String>();
daftar_rs = data.get(position);
/*id_rs.setText(daftar_rs.get(Pay_Per_View.TAG_ID_RS));*/
Image_name.setText(daftar_rs.get(Pay_Per_View.TAG_MOVIE_NAME));
link_image_rs.setText(daftar_rs.get(Pay_Per_View.TAG_LINK_IMAGE_RS));
movie_price.setText(daftar_rs.get(Pay_Per_View.TAG_MOVIE_PRICE));
/*alamat_rs.setText(daftar_rs.get(MainActivity.TAG_MEASURING));
*/
imageLoader.DisplayImage(daftar_rs.get(Pay_Per_View.TAG_LINK_IMAGE_RS),
thumb_image);
return vi;
}
}
Actually, you can set a default style for TextViews like this for all the app or activity,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyTextViewStyle</item>
</style>
<style name="MyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
and set the theme as your activity's theme.

Custom list view is not diplaying properly

I have created an app which uses a custom listview Fragment. But the custom listview is not displaying properly.
below attached is my screen shot.
ANd here is my Xml file of individual row item
<?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:orientation="vertical" >
<TextView
android:id="#+id/Jobtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="2dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textStyle="bold" >
</TextView>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Jobtitle"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<TextView
android:id="#+id/publishdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PubDate:-"
android:textColor="#color/textgrey"
/>
<TextView
android:id="#+id/lefttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/publishdate"
android:layout_alignBottom="#+id/publishdate"
android:layout_alignParentRight="true"
android:text="Time Left:-"
android:textColor="#color/textgrey"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:background="#drawable/dividr" >
</LinearLayout>
and here is the list fragment layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No text" >
</TextView>
And here is my adapter
package my.newapp.freelanceeye;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class joblisted extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapt my=new adapt(getActivity(), R.layout.listrow,m);
setListAdapter(my);
}
Context ctx;
public class adapt extends ArrayAdapter<String>{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.listrow,parent,false);
return row;
//return super.getView(position, convertView, parent);
}
public adapt(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
ctx=context;
}
}
String[] m={"as","asa","sd","Ad","sad","asda"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.listfragmnetlayout,container,false);
return v;
}
}
You should set your fragment's layout something 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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</<LinearLayout>
I think the problem is with height of your Views, thats why try it with match_parent.
Your are building a custom array adapter but your are not setting the parameters in your row fields.
Take a look at this adapter implementation, there are comments explaining every step:
private class CustomArrayAdapter extends ArrayAdapter<RowData>
{
private ArrayList<RowData> list;
//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<RowData> rowDataList)
{
//populate the local list with data.
super(context, textViewResourceId, rowDataList);
this.list = new ArrayList<RowData>();
this.list.addAll(rowDataList);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//creating the ViewHolder we defined earlier.
ViewHolder holder = new ViewHolder();)
//creating LayoutInflator for inflating the row layout.
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflating the row layout we defined earlier.
convertView = inflator.inflate(R.layout.row_item_layout, null);
//setting the views into the ViewHolder.
holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
holder.changeRowStatus = (ImageView) convertView.findViewById(R.id.iStatus);
holder.changeRowStatus.setTag(position);
//define an onClickListener for the ImageView.
holder.changeRowStatus.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(activity, "Image from row " + position + " was pressed", Toast.LENGTH_LONG).show();
}
});
holder.checked = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
holder.checked.setTag(position);
//define an onClickListener for the CheckBox.
holder.checked.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//assign check-box state to the corresponding object in list.
CheckBox checkbox = (CheckBox) v;
rowDataList.get(position).setChecked(checkbox.isChecked());
Toast.makeText(activity, "CheckBox from row " + position + " was checked", Toast.LENGTH_LONG).show();
}
});
//setting data into the the ViewHolder.
holder.title.setText(RowData.getName());
holder.checked.setChecked(RowData.isChecked());
//return the row view.
return convertView;
}
}
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#339966"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PubDate:-"
android:textColor="#606060"
/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Left:-"
android:textColor="#606060"
android:paddingLeft="180dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
Change layout_height to fill_parent

Create and add listview to layout at runtime

I have design like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="horizontal" >
<Button
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<Button
android:id="#+id/nextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
<!-- the two columns part -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:id="#+id/submenue"
>
<!-- this will be the menue list -->
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contents"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.20"
android:id="#+id/mainLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second Name" />
</LinearLayout>
</LinearLayout>
I added the following code to the next button such that it display and fill the list
this.next = (Button)this.findViewById(R.id.nextbutton);
this.next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] MainMenue = new String[] { "aaaa", "bbb", "ccc"};
menueview = (ListView)findViewById(R.id.MyHiddenListView);
menueview.setVisibility(ListView.VISIBLE);
menueview.setAdapter(new submenueadapter(getApplicationContext(), MainMenue));
}
});
and the adaptor class is
package com.appnetics;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class submenueadapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public submenueadapter(Context context,String[] objects)
{
super(context, R.layout.main, objects);
this.context = context;
this.values = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.contents);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
return rowView;
}
}
the problem is that the list be populated with the array plus the other controls in the page (the 2 buttons )
the layout before clicking next
the layout after clicking
any idea to fix that please
Best regards
Best regards
The main problem is that you are including everything in one main.xml layout.
And due to this problem, you are getting back/next button for every listitem (Actually whole layout is being repeating as Listitem).
Soluition:
Instead, Try to define separate layout for the ListItem and inflate that layout inside the getView() method. so there must be 2 XML layouts file, one is having main design with ListView only, and another one with only views that you want for every list item.
for example, (1) main.xml (2) row_item.xml
Now, you just need to make correction in code of inflating layout:
View rowView = inflater.inflate(R.layout.row_item, parent, false); // row_item.xml is inflated here
ViewGroup class do offers various methods to add view anywhere and using LayoutParams classes you can pass the various parent container class specific attributes to applied over the child view to be added in the container.
That you can google and is easily doable.
I can also think of putting a ListView in the XML with the Visibility GONE.
And on specific action trigger make it visible. That will prevent me doing various programing stuff required to add view at a perticular position.
And i also don't feel any harm of this approach.
e.g
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
<ListView
android:id="#+id/MyHiddenListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</Linearlayout>
In the .java file
btnBack = (Button)findViewById(R.id.backbutton);
myListView = (ListView)findViewById(R.id.MyHiddenListView);
btnBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myListView.setVisibility(ListView.VISIBLE);
myListView.setAdaptor(new MyListViewAdaptor());
}
});
Hope it helps :)

Categories

Resources