如何设置两条线使用simple_list_item_2一个ListView的?两条线、如何设置、ListView

2023-09-06 00:26:54 作者:风扬长发

所以下面将创建一个ListView其中的行都有自己的主的TextView填充值数组。

  ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
    android.R.layout.simple_list_item_2,android.R.id.text1,价值观);
 

更改第三个参数android.R.id.text2设置辅助的TextView。有没有什么简单的方法可以同时设置?

解决方案

AFAIK simple_list_item_2包含包含两个TextViews一个TwoLineListItem。 ArrayAdapter是不会在这里工作,你要么必须创建一个自定义的适配器或使用一个支持它像SimpleCursorAdapter。

  ListAdapter适配器=新SimpleCursorAdapter(
                 本,
                 android.R.layout.simple_list_item_2,
                 mCursor,//通行证光标结合。
                 新的String [] {People.NAME,People.COMPANY} //光标柱阵列结合。
                 新的INT [] {android.R.id.text1,android.R.id.text2}); //该模板对象的并行阵列绑定到这些列。

         //绑定到我们新的适配器。
         setListAdapter(适配器);
 

  

或者,如果你不想SimpleCursorAdapter您必须创建一个自定义ArrayAdapter或BaseAdapter

创建自定义ArrayAdapter,应用对象(有两个项目)阵列的自定义适配器,并将其输送到getListView.setAdapter。

重写一个ArrayAdapter的getView方法,你的名字字符串适用于TextViews在自定义列表行视图。

下面的代码片段会帮助你。

SampleActivity.java

 包org.sample;

进口的java.util.ArrayList;

进口android.app.ListActivity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;
进口android.widget.TwoLineListItem;

公共类SampleActivity扩展ListActivity {

    @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);

        人的人;

        ArrayList的<人>人=新的ArrayList<人>();

        人=新的Person();
        person.setName(VIPUL);
        person.setAge(20);
        persons.add(人);

        人=新的Person();
        person.setName(安妮);
        person.setAge(22);
        persons.add(人);

        setListAdapter(新MyAdapter(这一点,人));
    }

}
 
VB问题 运行上面的程序,然后单击窗体,列表框中的项目是什么

Person.java

 类Person {
    字符串名称;
    INT年龄;

    公共字符串的getName(){
        返回名称;
    }

    公共无效setname可以(字符串名称){
        this.name =名称;
    }

    公众诠释getAge(){
        返回年龄;
    }

    公共无效setAge(INT岁){
        this.age =年龄;
    }

}
 

MyAdapter.java

 类MyAdapter扩展了BaseAdapter {

    私人上下文的背景下;
    私人的ArrayList<人>人;

    公共MyAdapter(上下文的背景下,ArrayList的<人>人){
        this.context =背景;
        this.persons =者;
    }

    @覆盖
    公众诠释getCount将(){
        返回persons.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回persons.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回0;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        TwoLineListItem twoLineListItem;

        如果(convertView == NULL){
            LayoutInflater充气=(LayoutInflater)上下文
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            twoLineListItem =(TwoLineListItem)inflater.inflate(
                    android.R.layout.simple_list_item_2,NULL);
        } 其他 {
            twoLineListItem =(TwoLineListItem)convertView;
        }

        TextView的文本1 = twoLineListItem.getText1();
        TextView的文本2 = twoLineListItem.getText2();

        text1.setText(persons.get(位置).getName());
        text2.setText(+ persons.get(位置).getAge());

        返回twoLineListItem;
    }
}
 

So the following will create a ListView where the rows have their "primary" textview filled by the values array.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_2, android.R.id.text1, values);

Changing the third parameter to android.R.id.text2 sets the "secondary" textview. Is there any simple way to set both?

解决方案

AFAIK simple_list_item_2 contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here,you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter.

ListAdapter adapter = new SimpleCursorAdapter(
                 this,
                 android.R.layout.simple_list_item_2,
                 mCursor,     // Pass in the cursor to bind to.
                 new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
                 new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.

         // Bind to our new adapter.
         setListAdapter(adapter);

Or if you dont want SimpleCursorAdapter You will have to create Custom ArrayAdapter or BaseAdapter

Create a custom ArrayAdapter,apply the object(Having two items) array to the custom adapter, and feed it to getListView.setAdapter.

Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.

Following Snippet will help you.

SampleActivity.java

package org.sample;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;

public class SampleActivity extends ListActivity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Person person;

        ArrayList<Person> persons = new ArrayList<Person>();

        person = new Person();
        person.setName("Vipul");
        person.setAge(20);
        persons.add(person);

        person = new Person();
        person.setName("Anil");
        person.setAge(22);
        persons.add(person);

        setListAdapter(new MyAdapter(this, persons));
    }

}

Person.java

class Person {
    String name;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

MyAdapter.java

class MyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Person> persons;

    public MyAdapter(Context context, ArrayList<Person> persons) {
        this.context = context;
        this.persons = persons;
    }

    @Override
    public int getCount() {
        return persons.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TwoLineListItem twoLineListItem;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            twoLineListItem = (TwoLineListItem) inflater.inflate(
                    android.R.layout.simple_list_item_2, null);
        } else {
            twoLineListItem = (TwoLineListItem) convertView;
        }

        TextView text1 = twoLineListItem.getText1();
        TextView text2 = twoLineListItem.getText2();

        text1.setText(persons.get(position).getName());
        text2.setText("" + persons.get(position).getAge());

        return twoLineListItem;
    }
}