Android theme not affecting TextViews inside list item - android

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.

Related

Get "No results" Message Custom Adapter ListView Android

I am having a problem to set a "No results" message when getCount() returns 0.
I have a custom layout for the listview, and I simply want to get a "No Results" textview when there is no data on the listview.
Any idea how to achieve this?
I saw something with a empty id, but that needs to be on a listview, since this is a custom layout, it doesn't have that "tag".
Custom Adapter Code:
public class ListScheduleAdapter extends BaseAdapter {
Context context;
protected List<Schedule> listSchedules;
LayoutInflater inflater;
public ListScheduleAdapter(Context context, List<Schedule> listSchedules) {
this.listSchedules = listSchedules;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return listSchedules.size();
}
public Schedule getItem(int position) {
return listSchedules.get(position);
}
public long getItemId(int position) {
return listSchedules.get(position).getCod_Horario();
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.layout_list_item,
parent, false);
holder.txtTeacher = (TextView) convertView
.findViewById(R.id.txt_teacher);
holder.txtDayofWeek = (TextView) convertView
.findViewById(R.id.txt_dayofweek);
holder.txtSubject = (TextView) convertView
.findViewById(R.id.txt_subject);
holder.txtTime = (TextView) convertView
.findViewById(R.id.txt_time);
holder.txtRoom = (TextView) convertView
.findViewById(R.id.txt_room);
holder.txtClass = (TextView) convertView
.findViewById(R.id.txt_class);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Schedule sche = listSchedules.get(position);
String fullTeacher = sche.getProfessor();
String[] names = fullTeacher.split(" ");
holder.txtTeacher.setText(names[0] + " " + names[names.length-1]);
holder.txtDayofWeek.setText(sche.getDia_Semana());
holder.txtSubject.setText(sche.getDisciplina());
holder.txtTime.setText(sche.getT_Entrada() + "h-" + sche.getT_Saida()+"h");
holder.txtRoom.setText(sche.getSala());
holder.txtClass.setText(sche.getTurma());
return convertView;
}
private class ViewHolder {
TextView txtTeacher;
TextView txtDayofWeek;
TextView txtSubject;
TextView txtTime;
TextView txtRoom;
TextView txtClass;
}
}
Fragment Code (method called on a async OnPostExecute method):
private void populateListView() {
ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList);
listviewHorarios.setAdapter(adapter);
}
Layout code:
<?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="horizontal"
android:padding="8dp" >
<LinearLayout
android:id="#+id/layout_row1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_teacher"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_dayofweek"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row1"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_subject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layout_row3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_row2"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_room"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
<TextView
android:id="#+id/txt_class"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
</RelativeLayout>
add
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="5dp"
android:gravity="center"
android:text="#string/no_data_available"
android:textColor="#android:color/black"
android:visibility="gone" />
to the same layout where you declared your ListView (wrap both in a FrameLayout)
on the ListView's instance call
listView.setEmptyView(findViewById(R.id.empty));
when the adapter is empty, you will see the TextView apper
add TextView to your listview layout and setVisibility of it to GONE and check the adapter:
if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
}
hope this will help.

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.

Set different icons for each row

Above is the main screen of my app with a custom adapter for listview. How do I put a different icon for each row in the listview? I tried putting an ImageView in the xml but it overwrites my whole screen only showing an icon.
MainAcitivty 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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="28sp" />
<ImageView
android:id="#+id/condIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/cityText"
android:contentDescription="weather icon" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/condIcon"
android:textSize="20sp" />
<ListView
android:id="#+id/mainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="#+id/btnPicturePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="picturePage"
android:src="#drawable/photo" />
Custom adapter:
public class WeatherAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> weatherData;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public WeatherAdapter(Activity context, ArrayList<String> weatherData) {
super(context, R.layout.list, weatherData);
this.context = context;
this.weatherData = weatherData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
ArrayList<String> s = new ArrayList<String>(weatherData);
String []sa = new String [s.size()];
sa = s.toArray(sa);
holder.text.setText(sa[position]);
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
}
return rowView;
}
List xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/white" >
</TextView>
Your list.xml should be something similar to this (taken from one my files)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal"
android:background="#drawable/xml_pressed_bg" android:clickable="true" android:layout_width="fill_parent"
android:layout_height="54dip"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ttshow="http://schemas.android.com/apk/res-auto">
<ImageView android:id="#+id/imgThumb" android:layout_width="42.0dip" android:layout_height="42.0dip" android:scaleType="fitXY"
android:src="#drawable/sample_dj" />
<TextView android:textSize="14.0sp" android:textColor="#ff666666" android:id="#+id/txtName"
android:layout_width="180dip" android:layout_height="wrap_content"
android:singleLine="true" android:shadowColor="#99ffffff" style="#style/TextShadowLight"
android:text="Hello" android:paddingLeft="10dip"/>
</LinearLayout>
Every row item has an image (imgThumb). This needs to be changed or set to a static image, depending on your needs. This is done in the adapters' getView() method.
imgThumb.setImageResource(R.drawable.my_image);
Private TypedArray img;
img=getResource.obtainTypedArray(R.array.imgIcon)
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
holder.image.setImageResource(img.gerResourceId[0]);
}else if (sa[position].startsWith("Air Condition")) {
holder.image.setImageResource(img.gerResourceId[1]);
}

Android custom row layout not showing up in 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

ListView without ListActivity

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.

Categories

Resources