如何使用单独的字符串数组(Android)设置多行列表视图

时间:2012-08-02 12:46:10

标签: android arrays listview android-arrayadapter

我正在开发一款适用于2个屏幕的Android应用程序。第一个creen很简单,它有一个TextView初始化为“Not Set”和一个Button。当用户点击按钮时,他/她被带到第二个屏幕,这是一个大的ListView。 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();
    }

}

我研究的一些片段正在使用这种方法(示例代码)

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中声明的字符串数组合并多行listview,而无需像上面那样使用硬编码?

2 个答案:

答案 0 :(得分:1)

您无法将适配器设置为两次以显示您想要的行,而是必须实现自定义适配器(有数千个教程可供查看,因此我将保留此部分)。

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

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>

答案 1 :(得分:0)

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

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

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