1

我有一个带有惰性适配器的 xml 解析器,我将所有数据解析为列表视图。

现在我想从 listivew 中获取所有数据(每行有 1 个文本视图)并将其放入字符串数组中,但我不知道该怎么做。因为如果我谷歌我只能得到关于如何使用和字符串数组来填充列表的结果。

我的解析器:

TextView txt =(TextView)vi.findViewById(R.id.tv);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);     
txt.setText(song.get(MainActivity.KEY_TXT));

我使用(到目前为止)获取我的字符串数组的方法:

private String[] txt = {

}

提前致谢。

4

1 回答 1

1

您只需在 listView 适配器的 getView() 方法中使用字符串数组。像这样: -

给 strArray 的大小与 listView 的大小相同。

String strArray[] = new String[<size of Your ListView>]; 

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


    TextView textView;
    if (convertView == null) {

        convertView = inflater.inflate(R.layout.listitem, parent, false);
        textView = (TextView) convertView
                .findViewById(R.id.textView);
    }
    strArray[position] = textView.getText().toString();
    return convertView;
}

现在您想将此 strArray 用于其他类,那么您必须将其设为静态,或者您可以通过intent.like 将其传递给其他类:--

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

然后在其他中,activity class您应该通过输入以下代码来获取此字符串数组:-

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

如果你想要传递字符串数组(即 strArray)的类不是Activity那么你应该让你的 strArray 像这样静态:

public static strArray[];

现在我认为你的问题将得到解决..

于 2012-10-25T10:48:51.337 回答