0

I'm having trouble setting my listView up to work with two textViews per item in the list. This is my code. I know there is something fundamentally wrong with the way that I try to implement two different arrays, but I haven't been able to figure this out. Not sure if hashMaps would be the way to go.

private String[] nums= { "One", "Two", "Three" };
private String[] names= { "HoneyComb", "JellyBean", "ICS" };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int[] ids = {android.R.id.text1, android.R.id.text2};
    SimpleAdapter<String> adapter = new SimpleAdapter(this, names,
            android.R.layout.simple_list_item_2, nums, ids);
    setListAdapter(adapter);

}

I would really like to stick with SimpleAdapter if possible.

EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Its was simpler if you use [custom adapter..](http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – amalBit Jul 29 '13 at 06:26
  • int[] ids = {android.R.id.text1, android.R.id.text2}; is that correct ? android.R.... – Tugrul Jul 29 '13 at 06:27

3 Answers3

1

Use custom listview as shown below:

Check here

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

Tyr this one

private String[] nums= { "One", "Two", "Three" };
private String[] names= { "HoneyComb", "JellyBean", "ICS" };


List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<nums.lenght();i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt1",  nums[i]);
            hm.put("txt2", names[i]);
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "txt1","txt2" };

        // Ids of views in listview_layout
        int[] to = {R.id.txt1,R.id.txt2};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);

        setListAdapter(adapter);

}

listview_layout.xml is

<?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" >




        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp" />



</LinearLayout>
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

Well you have to use BaseAdapter for that, Here is your code

public class MainActivity extends Activity 
{
private String[] nums= { "One", "Two", "Three" };
private String[] names= { "HoneyComb", "JellyBean", "ICS" };

@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView listView= (ListView) findViewById(R.id.lv_list);

    count = this.names.length();
    listView.setAdapter(new CustomAdapter(MainActivity.this));
}

public class CustomAdapter extends BaseAdapter 
{
    /*
     * Variables Declaration section
     */
    private Context mContext;

    public CustomAdapter(Context context) 
    {
        mContext = context;
    }//End of CustomAdapter constructor

    public int getCount() 
    {
        return count;
    }//End of getCount method

    public Object getItem(int position) 
    {
        return position;
    }//End of getItem method

    public long getItemId(int position) 
    {
        return position;
    }//End of getItemId method

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;

        if (convertView == null) 
        {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.text_view, null);
            holder.textviewName = (TextView) convertView.findViewById(R.id.name);
            holder.textviewNumber = (TextView) convertView.findViewById(R.id.number);

            convertView.setTag(holder);
        }//End of if condition
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }//End of else

        holder.textviewName.setText(names[position]);
        holder.textviewNumber.setText(nums[position]);

        return convertView;
    }//End of getView method
}//End of CustomAdapter instance inner class

class ViewHolder 
{
    TextView textviewName;
    TextView textviewNumber;
}//End of ViewHolder instance inner class
}

your main.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:background="#FFFFFF"
android:orientation="vertical" >
<ListView
        android:id="@+id/lv_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

create a xml under res/layout in the name text_view.xml

<?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="fill_parent" >

<TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#00ccFF"
        android:textSize="18sp"
        android:paddingTop="10sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_alignRight="@+id/name"
        android:layout_toRightOf="@+id/icon"
        android:layout_below="@+id/name"
        android:text="TextView"
        android:textColor="#00ccFF"
        android:textSize="16sp"
        android:paddingTop="30sp"
        android:textStyle="normal" />
</RelativeLayout>

This will do the job for you.Tweak the code for your purpose. Thanks

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45