0

I'm trying to create an android list using a custom adapter and the following XML row layout. I have implemented the following classes however when I run it onto my emulator the "android:id="@+id/firstLine" TextView does not appear to be setting the text! The "id/secondLine" Text View does receive the correct text!

List Row Item XML Layout:

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="@string/TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/purchase_date"
        android:textSize="12sp" />

///  THIS IS THE TextView that's not working
    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="@string/home_depot"
        android:textSize="16sp" />

</RelativeLayout> 

Adapter Class:

    public class SimpleAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    //public ImageLoader imageLoader;

    public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d){
        activity = a;
        data = d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_item, null);

        TextView purchaseDate = (TextView)vi.findViewById(R.id.secondLine);
        TextView seller = (TextView)vi.findViewById(R.id.firstLine);


        HashMap<String, String> employee = new HashMap<String, String>();
        employee = data.get(position);

  //  Should this not be setting the text for the "seller" TextView?
        purchaseDate.setText(employee.get(HistoryListView.KEY_PURCHASE_DATE));
        seller.setText(employee.get(HistoryListView.KEY_SELLER));

        return vi;
    }
}

List Activity:

public class HistoryListView extends Activity {

    static final String KEY_SELLER="seller";
    static final String KEY_PURCHASE_DATE="purchase_date";
    static final String KEY_SALE_AMMOUNT="sale_ammount";
    static final String PURCHASE_DATE="Purchase Date: ";
    static final String USD="$ ";

    ListView list;
    SimpleAdapter adapter;
    public String[] sellerList = {"Home Depot","7 Eleven","Costco","Outback Steak House","Philz Coffee"};
    public String[] purchaseDateList={"1/12/13", "1/23/13", "2/16/13", "2/17/13", "2/18/13"};
    public String[] saleAmmountList={"1023.10","243.98","107.54","45.88","21.99"};

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_list);
        ArrayList<HashMap<String,String>> expenseList=new ArrayList<HashMap<String, String>>();
        Intent intent = getIntent();

        for(int i=0; i<5; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_SELLER, sellerList[i]);
            map.put(KEY_PURCHASE_DATE, PURCHASE_DATE+purchaseDateList[i]);
            //map.put(KEY_SALE_AMMOUNT, saleAmmountList[i]);

            expenseList.add(map);
        }

        ListView list=(ListView)findViewById(R.id.list);

        adapter=new SimpleAdapter(this, expenseList);
        list.setAdapter(adapter);
    }//END onCreate
}//END HistoryListView
Matt
  • 3,592
  • 5
  • 21
  • 26
  • Means your textview is invisible while running app?? – Piyush Mar 24 '14 at 06:37
  • 1
    add a log inside getView and print the second textview value. If the value is printing then the problem is with the alignment in layouts. – Shriram Mar 24 '14 at 06:40
  • why are you extending a BaseAdapter? why not using a specialized adapter? – pskink Mar 24 '14 at 07:03
  • pskink I'm sort of modifying existing code. No idea what the difference between BaseAdapter and a specialized adapter would be, but I got it working. It was in the XML! – Matt Mar 25 '14 at 00:45

2 Answers2

0

You have problem in List Row Item try this one tested and it show icon and two lines

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/icon"
        android:text="TextView" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/firstLine"
        android:layout_below="@+id/firstLine"
        android:text="TextView" />

</RelativeLayout>
0
try this one..

<TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
android:maxLines="5"// to specify how many lines its occupied or else reduce your size of textsize 
    android:singleLine="false"
android:ellipsize="end"
        android:text="@string/home_depot"
        android:textSize="16sp" />

thank you..

Sethu
  • 430
  • 2
  • 13