【问题标题】:How to setup multi-line listview using separate string-arrays (Android)如何使用单独的字符串数组(Android)设置多行列表视图
【发布时间】:2012-08-02 12:46:10
【问题描述】:

我正在开发一个适用于 2 个屏幕的 Android 应用程序。第一个屏幕很简单,它有一个初始化为“未设置”的 TextView 和一个按钮。当用户点击按钮时,他/她被带到第二个屏幕,这是一个大的 ListView。 ListView 是有史以来所有票房最高的电影的列表。如果它只包含标题会很简单,但我需要创建列表视图以使其具有多行。结构是,标题向左,总收入向右对齐,发布日期位于第二行。

重要的花絮是我需要使用在 strings.xml 中声明的字符串数组。我已经声明了所有这些,但是我的问题是我被困在如何制作 Java 部分。到目前为止,这是我提出的。

package com.android.rondrich;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Lab5_082588part2 extends ListActivity {

    public static final String KEY = "KEY";
public static final String RETURN = "RETURN";

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

    String[] titleList = getResources().getStringArray(R.array.title_array);
    String[] grossList = getResources().getStringArray(R.array.gross_array);

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.lab5_082588part2, grossList)); //not sure if     correct to have 2 setListAdapters

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.lab5_082588part2, titleList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                Intent intent = getIntent();
                String title = ((TextView) view).getText().toString();
                intent.putExtra(Lab5_082588part2.RETURN, title);
                setResult(RESULT_OK, intent);
                finish();

            }
        });
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }

}

我研究过的一些sn-ps正在使用这种方法(示例代码)

private ArrayList<SearchResults> GetSearchResults(){
 ArrayList<SearchResults> results = new ArrayList<SearchResults>();

 SearchResults sr = new SearchResults();
 sr.setName("Justin Schultz");
 sr.setCityState("San Francisco, CA");
 sr.setPhone("415-555-1234");
 results.add(sr);

但是,对于长列表,这种方法会非常乏味。问题是:

如何使用在strings.xml 中声明的字符串数组来实现多行列表视图,而无需像上面那样进行硬编码?

【问题讨论】:

    标签: android arrays listview android-arrayadapter


    【解决方案1】:

    您不能将适配器设置两次以显示您想要的行,而是您必须实现自定义适配器(那里有成千上万的教程可供查看,所以我将省略这部分)。

    要从字符串数组中提取数据,您将(几乎)使用您发布的 sn-p:

    String[] titleList = getResources().getStringArray(R.array.title_array);
    String[] grossList = getResources().getStringArray(R.array.gross_array);
    // ...
    
    private ArrayList<SearchResults> GetSearchResults(){
        ArrayList<SearchResults> results = new ArrayList<SearchResults>();
        // make sure the arrays have the same length
        for (int i = 0; i < titleList.length; i++) {
            SearchResults sr = new SearchResults();
            sr.setTitle(titleList[i]);
            sr.setGross(grossList[i]);
            results.add(sr);
       }
       return results;
    }
    

    然后您将使用此方法返回的列表来填充您的自定义适配器。

    布局示例:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="title" />
    
        <TextView
            android:id="@+id/gross"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/title"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Gross" />
    
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/title"
            android:text="Date" />
    
    </RelativeLayout>
    

    【讨论】:

    • 我现在明白了,但我唯一不明白的是如何格式化结果(对齐),因为仅使用 sn-p 只会将它们垂直堆叠在一起。
    • @Erasmus 对齐是在布局文件中完成的,而不是在代码中。请参阅我编辑的答案。
    • 这很有意义。前段时间我在编码时很迷茫,因为我从来不知道如何不对整个列表进行硬编码。非常感谢您,非常感谢您的帮助!
    【解决方案2】:

    如何制作可以在本教程中阅读的自定义 ListView 项目:http://www.ezzylearning.com/tutorial.aspx?tid=1763429

    如果你想从你的 strings.xml 中获取 String,请在你的 ativity 类中使​​用它:

    getContext().getResources.getString(R.string.your_text);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多