with - how to set data in listview in android




Use array adapter with more views in row in listview (3)

I had to solve the same problem and tried to use the arrayadapter as answered here above, but it didn't work.

Later I succeeded to do it with baseadapter -- this is the adapter:

public class BusinessAdapter2 extends BaseAdapter {
    private final ArrayList<Business> myList;
    LayoutInflater inflater;
    Context context;


    public BusinessAdapter2(Context context, ArrayList<Business> myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }
    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) convertView = inflater.inflate(R.layout.business_list_item_2, parent, false);
        // assign the view we are converting to a local variable
        View v = convertView;
        Business b = myList.get(position);
        if (b != null) {
            TextView name = (TextView) v.findViewById(R.id.textview_name);
            TextView address = (TextView) v.findViewById(R.id.textview_address);
            TextView description = (TextView) v.findViewById(R.id.textview_description);
            TextView discount = (TextView) v.findViewById(R.id.textview_discount);

            // check to see if each individual textview is null.
            // if not, assign some text!
            if (name != null){
                name.setText(b.name);
            }
            if (address != null){
                address.setText(b.address);
            }
            if (description != null){
                description.setText(b.description);
            }
            if (discount != null){
                discount.setText(b.discountRate);
            }
        }

        // the view must be returned to our activity
        return v;
    }
}

this is the Object class I used (Business):

public class Business {
    String name,address,description,discountRate;
    public Business(){}
    public Business(String name,String address,String description,String discountRate){
        this.name=name;
        this.address=address;
        this.description=description;
        this.discountRate=discountRate;
    }
}

and this is how I populate the listview into the adapter:

ArrayList<Business> businesses2=new ArrayList<Business>(Arrays.asList(for_listview_objects));
    adapter_objects =new BusinessAdapter2(
            context, // The current context (this activity)
            businesses2);

    listView.setAdapter(adapter_objects);

I have stumbled upon a problem I can't quite get my head around, so I was hoping perhaps someone here have had the same problem or knew a good way of solving the problem.

I have created a view containing a ListView. This ListView contains two TextView. The problem is that I don't know where I send the values which are meant to go in the second text view using the ArrayAdapter. Is there a way to send with more information to the ArrayAdapter so that I can feed the "todaysmenu" TextView?

The ArrayAdapter method:

private void createList() {
    ListView lv = (ListView) findViewById(R.id.mylist);
    String[] values = new String[] { "Android", "Linux", "OSX", 
            "WebOS", "Windows7", "Ubuntu", "OS/2"
    };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.restaurantname, values);
    lv.setAdapter(adapter);
}

The row markup:

<?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/restaurantname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/restaurantname"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@+id/todaysmenu" />

</LinearLayout>

The activity layout:

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

    <ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </ListView>


</LinearLayout>

At the beginning I got everything to work, but when I added the second textfield problems arouse. In advance, thank you for your help!


I think that your problem is here:

Instead of this:

<?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/restaurantname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/restaurantname"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@+id/todaysmenu" />

</LinearLayout>

Try something like this:

<?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/restaurantname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test text"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test text" />

</LinearLayout>

If that works, then place your text in the /res/val/string folder like so:

<string name="testText">Put your text here...</string>

and then call like this:

  <?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="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/restaurantname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/testText"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/testText" />

</LinearLayout>

You would then set the dynamic values like this:

TextView tv = (TextView)findViewById(R.id.restaurantname);
tv.setText(values);







android-arrayadapter