带有Subtext的Android ListView

时间:2015-04-08 14:08:08

标签: android android-studio

我是初学者,我正在创建的第一个Android应用程序应该跟踪不同人的罢工。

我希望得到他们的名字,然后在其下面,一个潜台词,显示他们有多少次罢工。我不知道该如何解决这个问题。

这是我到目前为止所拥有的:

private void populateListView() {
    String[] myItems = {"Alex", "Laura", "John", "Tom"};
    ArrayAdapter<String> myAdapter=new
            ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            myItems);

    ListView list = (ListView) findViewById(R.id.people);
    list.setAdapter(myAdapter);
}

此外,当按下列表中的项目时,它会使用toast来显示其位置和名称:

private void registerClickCallback() {
    ListView list = (ListView) findViewById(R.id.people);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
            TextView textView = (TextView) viewClicked;
            String message = "You clicked" + position + "which is string" + textView.getText().toString();
            Toast.makeText(Hello.this, message, Toast.LENGTH_LONG).show();
        }
    });
}

如何创建显示一个人有多少次打击的子文本以及如何制作它以便在按下时增加一个?

1 个答案:

答案 0 :(得分:1)

您必须为您要显示的列表中的每个项目执行自定义布局的自定义适配器,以便每个项目都是这样的:

 <LinearLayout
        android:id="@+id/layout_list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txt_num_strikes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="normal" />
    </LinearLayout>

然后将该视图扩展到cutom适配器, 检查一下:http://www.android-ios-tutorials.com/android/android-custom-listview-example/

编辑:

创建一个类:

public class Person
{
 private String name;
 private int strikes;

public Person(){}

public Person( String _name, int _strikes)
{
  this.name = _name;
  this.strikes = _strikes;
}

public String getName()
{
  return name;
}

public int getStrikes()
{
  return strikes;
}

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

public void setStrikes (int strikes)
{
 this.strikes = strikes;
}
}

创建自定义适配器:

public class ListPeopleAdapter extends BaseAdapter {

    Context context;

    protected List<Person> listPeople;
    LayoutInflater inflater;

    public ListCarsAdapter(Context context, List<Person> listPeople) {
        this.listPeople= listPeople;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public int getCount() {
        return listPeople.size();
    }

    public Person getItem(int position) {
        return listPeople.get(position);
    }

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

            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.layout_list_item,
                    parent, false);

            holder.txtName = (TextView) convertView
                    .findViewById(R.id.txt_name);
            holder.txtStrikes = (TextView) convertView
                    .findViewById(R.id.txt_num_strikes);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Person person = listPeople.get(position);
        holder.txtName.setText(person.getName());
        holder.txtStrikes.setText(""+person.getStrikes()); //using ("" +) instead of .toString()

        return convertView;
    }

    private class ViewHolder {
        TextView txtName;
        TextView txtStrikes;
    }

}

关于你的活动:

    private void populateListView() {

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

Person person1 = new Person("Alex", 1);
Person person2 = new Person("Laura", 3);
Person person3 = new Person("John", 2);
Person person4 = new Person("Tom", 5);

arrayPeople.add(person1);
arrayPeople.add(person2);
arrayPeople.add(person3);
arrayPeople.add(person4);

// Get the ListView by Id and instantiate the adapter with
        // cars data and then set it the ListView
        ListView listV = (ListView) findViewById(R.id.people);
        ListPeopleAdapter adapter = new ListPeopleAdapter(context, arrayPeople);
        listV .setAdapter(adapter);


}

PS:我在这里直接编码,因此可能出现任何语法错误。