使listActivity可以使用每行2个项目

时间:2013-07-29 06:23:34

标签: java android android-listview

我无法将listView设置为在列表中为每个项目使用两个textView。这是我的代码。我知道我尝试实现两个不同数组的方式存在根本性的错误,但我无法弄清楚这一点。不确定hashMaps是否可行。

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);

}

如果可能,我真的很想坚持使用SimpleAdapter。

3 个答案:

答案 0 :(得分:1)

使用自定义列表视图,如下所示:

检查here

答案 1 :(得分:1)

Tyr这个

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是

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

答案 2 :(得分:0)

你必须使用BaseAdapter, 这是你的代码

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
}

您的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>

在名称res/layout

中的text_view.xml下创建一个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>

这将为您完成工作。Tweak the code for your purpose.谢谢