我是Android Studio中的新手,所以如果您对我有耐心和(我的noob问题),我将不胜感激。
我使用自定义适配器创建了ListView
,该适配器显示国家/地区和值的名称。我希望当你点击Country
时,它会启动一个新活动,并在其上显示国家名称,价值和人口。
我的问题是我怎么想从资源文件中的字符串数组中获取相应的数据? 例如,当我点击阿根廷时,它会在(不同的?)键中输入其名称,注释和金钱(未在列表视图中显示)和 将它发送到新活动,我会将其恢复并显示。
这是我的代码。
String.xml:
<string-array name="r_arrayPais">
<item>Argentina</item>
<item>Bolívia</item>
<item>Brasil</item>
<item>Chile</item>
<item>Colômbia</item>
<item>Equador</item>
<item>Guiana Francesa</item>
<item>Guiana</item>
<item>Paraguai</item>
<item>Peru</item>
<item>Surinami</item>
<item>Uruguai</item>
<item>Venezuela</item>
</string-array>
<string-array name="r_arrayNota">
<item>Pop 10kk</item>
<item>Pop 11kk</item>
<item>Pop 12kk</item>
<item>Pop 13kk</item>
<item>Pop 14kk</item>
<item>Pop 15kk</item>
<item>Pop 16kk</item>
<item>Pop 17kk</item>
<item>Pop 18kk</item>
<item>Pop 19kk</item>
<item>Pop 20kk</item>
<item>Pop 21kk</item>
<item>Pop 22kk</item>
</string-array>
<string-array name="r_arrayMoney">
<item>Money 10kk</item>
<item>Money 11kk</item>
<item>Money 12kk</item>
<item>Money 13kk</item>
<item>Money 14kk</item>
<item>Money 15kk</item>
<item>Money 16kk</item>
<item>Money 17kk</item>
<item>Money 18kk</item>
<item>Money 19kk</item>
<item>Money 20kk</item>
<item>Money 21kk</item>
<item>Money 22kk</item>
</string-array>
MainActivity.class
public class MainActivity extends ActionBarActivity {
public final static String DADOS = "patrick.flags.dados";
ListView j_listview;
String[] j_paises;
String[] j_nota;
String[] j_money
int[] j_flags =
{
R.drawable.flag_argentina,
R.drawable.flag_bolivia,
R.drawable.flag_brazil,
R.drawable.flag_chile,
R.drawable.flag_colombia,
R.drawable.flag_equador,
R.drawable.flag_france,
R.drawable.flag_guyana,
R.drawable.flag_paraguay,
R.drawable.flag_peru,
R.drawable.flag_suriname,
R.drawable.flag_uruguay,
R.drawable.flag_venezuela
};
FlagAdapter j_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
j_listview = (ListView)findViewById(R.id.l_lView);
j_paises = getResources().getStringArray(R.array.r_arrayPais);
j_nota = getResources().getStringArray(R.array.r_arrayNota);
j_money = getResources().getStringArray(R.array.r_arrayMoney);
j_adapter = new FlagAdapter(getApplicationContext(),R.layout.row);
j_listview.setAdapter(j_adapter);
j_listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
int i = 0;
for(String paises : j_paises)
{
FlagDataProvider dataProvider = new FlagDataProvider(j_flags[i], paises, j_nota[i]);
j_adapter.add(dataProvider);
i++;
}
j_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When I click ,for example, on Argentina, it will put its
//name,note and money(which is not showing in the listview) in (different?) key(s) and
//send it to the new activity where I will get it back and display.
Intent j_intent = new Intent (getApplicationContext(), ThirdActivity.class);
j_intent.putExtra(DADOS,j_dados);
startActivity(j_intent);
}
});
}
答案 0 :(得分:1)
直接从资源(即string.xml)访问字符串值,您可以通过调用其ID直接获取它:
String mess = getResources().getString(R.string.mess_1);
(从此前回答的问题中获得的示例)how to read value from string.xml in android?
如果您尝试使用索引以编程方式从列表视图中的特定字段获取特定值,则可能会有所帮助:
String selectedFromList = (lv.getItemAtPosition(position));
(从此前回答的问题中获得的示例)How to extract the text from the selected item on the listView
如果您尝试通过事件直接执行此操作,例如OnClick()事件:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
String data=(String)arg0.getItemAtPosition(arg2);
}});
(从此前回答的问题中获得的示例)How to retrieve the clicked string from a listview using OnItemClick?
换句话说,有很多方法可以获得数据,一些研究可以大有帮助,理解这些方法是Android编程的关键。
希望这有帮助!
答案 1 :(得分:0)
我这样做并为我工作,也许这不是最好的方式......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String j_dados = j_nota[position];
Intent j_intent = new Intent (getApplicationContext(), ThirdActivity.class);
j_intent.putExtra(DADOS,j_dados);
startActivity(j_intent);
}
});