blackwolf
blackwolf

Reputation: 927

Android List with two elements per row

I want to make a list in an Android application that looks like this:

param1_name value1

param2_name value2

param2_name value2

...

where param_names are strings only, but every value come from one different LinkedList.

Can anyone give me any suggestions? This is what I've done until now, so not very much:

import android.app.Activity;
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView;

public class MyListActivity extends Activity {

    private ArrayAdapter<String> paramArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.param_list);

        String paramNames[] = { "param1", "param2", "param3" };

        paramArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, paramNames);

        ListView paramListView = (ListView) findViewById(R.id.params);
        paramListView.setAdapter(paramArrayAdapter);
    }
}

I forgot to tell you that the values from the list must be changed from time to time. The LinkedLists have data that come from a device via bluetooth.

Upvotes: 1

Views: 4802

Answers (4)

Arpit Patel
Arpit Patel

Reputation: 1571

You need to create One new adapter with the another Linear list with the 2 text views and inflate that to the original list view and get the values according to the inflated listviews.

here id the example of that,

http://www.vogella.de/articles/AndroidListView/article.html#adapterperformance

Hope you will get your answer

Upvotes: 0

Aerrow
Aerrow

Reputation: 12134

In this code i used 3 columns, you will changed as per yours..

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#bfffdf"
    android:drawSelectorOnTop="false">
</ListView>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
     <TextView android:id="@+id/text1"
        android:textColor="#00ff00" 
        android:textSize="18sp"
        android:layout_width="30dip"
        android:layout_height="wrap_content"/>
     <TextView android:id="@+id/text2"
        android:textColor="#ff0000"
        android:textSize="18sp"
        android:layout_width="110dip"
        android:layout_height="wrap_content" 
        android:layout_weight="1"/>
     <TextView android:id="@+id/text3"
        android:textColor="#0000ff"
        android:textSize="14sp"
        android:layout_width="40dip"
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>
</LinearLayout>

MainActivity.java

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

    static final ArrayList<HashMap<String,String>> list = 
        new ArrayList<HashMap<String,String>>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                list,
                R.layout.main,
                new String[] {"rank","model","company"},
                new int[] {R.id.text1,R.id.text2, R.id.text3}
        );     
        populateList();
        setListAdapter(adapter);    
    }

       private void populateList() {
        HashMap map = new HashMap();
        map.put("rank", "1");
        map.put("model", "Samsung Galaxy Nexus");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "2");
        map.put("model", "Samsung Epic Touch 4G");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "3");
        map.put("model", "HTC Evo 3D");
        map.put("company", "HTC");
        list.add(map);
        map = new HashMap();
        map.put("rank", "4");
        map.put("model", "T-Mobile MyTouch 4G Slide");
        map.put("company", "HTC");
        list.add(map);
        map = new HashMap();
        map.put("rank", "5");
        map.put("model", "Samsung Galaxy S II");
        map.put("company", "Samsung");
        list.add(map);
        map = new HashMap();
        map.put("rank", "6");
        map.put("model", "Apple iPhone 4S 16GB");
        map.put("company", "Apple");
        list.add(map);
        map = new HashMap();
        map.put("rank", "7");
        map.put("model", "Motorola Droid Razr");
        map.put("company", "Motorola");
        list.add(map);
        map = new HashMap();
        map.put("rank", "8");
        map.put("model", "Motorola Droid Bionic");
        map.put("company", "Motorola");
        list.add(map);
        map = new HashMap();
        map.put("rank", "9");
        map.put("model", "Samsung Focus S");
        map.put("company", "Samsung ");
        list.add(map);
        map = new HashMap();
        map.put("rank", "10");
        map.put("model", "Samsung Focus Flash");
        map.put("company", "Samsung");
        list.add(map);
    }
}

Upvotes: 0

PKeidel
PKeidel

Reputation: 2589

You can use your own Layout in a ListActivity. Then you use something like "CustomAdapter adapter = new CustomAdapter(this, R.layout.event_row, R.id.event_row_lbl_title, allEvents);" instead of your ArrayAdapter.

I think http://www.codeproject.com/Articles/183608/Android-Lists-ListActivity-and-ListView-II-Custom would help.

Upvotes: 1

Aleks G
Aleks G

Reputation: 57346

I don't think you can do this with a standard ArrayAdapter. You need to create your own adapter class and in its getView class create a simple LinearLayout with two TextView's each displaying a corresponding piece of your row - then return that linear layout. It's a rather simple exercise.

Upvotes: 1

Related Questions