我有listview的片段
myfragment:
public class LightFragment extends Fragment {
String[] name;
String[] type;
String[] swim;
int[] flag;
ListView list;
ListViewAdapter adapter;
public LightFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_light, container, false);
name = getResources().getStringArray(R.array.sport);
type = new String[] { "China", "India", "United States",
"Indonesia", "Brazil" };
swim = new String[] { "1,354,040,000", "1,210,193,422",
"315,761,000", "237,641,326", "193,946,886" };
flag = new int[] { R.drawable.light11,
R.drawable.light12,
R.drawable.light21,
R.drawable.light22,
R.drawable.light23,
R.drawable.light24,
R.drawable.light25,
R.drawable.light26,
R.drawable.light27 };
// Locate the ListView in fragmenttab1.xml
list = (ListView) rootView.findViewById(R.id.listview);
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(getActivity(), name, type, swim,
flag);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
// Capture clicks on ListView items
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(getActivity(), SingleItemView.class);
// Pass all data rank
i.putExtra("rank", name);
// Pass all data country
i.putExtra("country", type);
// Pass all data population
i.putExtra("population", swim);
// Pass all data flag
i.putExtra("flag", flag);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
return rootView;
}
}
Listviewadapter:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] rank;
String[] country;
String[] population;
int[] flag;
LayoutInflater inflater;
public ListViewAdapter(Context context, String[] rank, String[] country,
String[] population, int[] flag) {
this.context = context;
this.rank = rank;
this.country = country;
this.population = population;
this.flag = flag;
}
public int getCount() {
return rank.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtrank;
TextView txtcountry;
TextView txtpopulation;
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Locate the TextViews in listview_item.xml
txtrank = (TextView) itemView.findViewById(R.id.name);
txtcountry = (TextView) itemView.findViewById(R.id.type);
txtpopulation = (TextView) itemView.findViewById(R.id.swim);
// Locate the ImageView in listview_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the TextViews
txtrank.setText(rank[position]);
txtcountry.setText(country[position]);
txtpopulation.setText(population[position]);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);
return itemView;
}
}
mysingleviewitem:
public class SingleItemView extends Activity {
TextView txtname;
TextView txttype;
TextView txtswim;
TextView txtfunny;
TextView txtwalking;
TextView txtangry;
TextView txtsad;
ImageView imgflag;
String[] name;
String[] type;
String[] swim;
String[] funny;
String[] walking;
String[] angry;
String[] sad;
int[] flag;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
Intent i = getIntent();
// Get a single position
position = i.getExtras().getInt("position");
// Get the list of rank
name = i.getStringArrayExtra("rank");
// Get the list of country
type = i.getStringArrayExtra("country");
// Get the list of population
swim = i.getStringArrayExtra("population");
funny = i.getStringArrayExtra("rank");
// Get the list of country
walking = i.getStringArrayExtra("country");
// Get the list of population
angry = i.getStringArrayExtra("population");
sad = i.getStringArrayExtra("rank");
// Get the list of flag
flag = i.getIntArrayExtra("flag");
// Locate the TextViews in singleitemview.xml
txtname = (TextView) findViewById(R.id.name);
txttype = (TextView) findViewById(R.id.type);
txtswim = (TextView) findViewById(R.id.swim);
txtfunny = (TextView) findViewById(R.id.funny);
txtwalking = (TextView) findViewById(R.id.walking);
txtangry = (TextView) findViewById(R.id.angry);
txtsad = (TextView) findViewById(R.id.sad);
// Locate the ImageView in singleitemview.xml
imgflag = (ImageView) findViewById(R.id.flag);
// Load the text into the TextViews followed by the position
txtname.setText(name[position]);
txttype.setText(type[position]);
txtswim.setText(swim[position]);
txtfunny.setText(funny[position]);
txtwalking.setText(walking[position]);
txtangry.setText(angry[position]);
txtsad.setText(sad[position]);
// Load the image into the ImageView followed by the position
imgflag.setImageResource(flag[position]);
}
}
myarray:
<string-array name="sport">
<item >Lari</item>
<item >Bola</item>
<item >Balap</item>
<item >tinju</item>
<item >renang</item>
</string-array>
<string-array name="Lari">
<item >100 meter</item>
<item >mulai</item>
<item >finish</item>
<item >piala</item>
</string-array>
细节: ListViewItem,其字符串数组为“sport”,并根据数组字符串的布局进行管理。
点击后会打开singleviewitem列表视图,但我希望根据<string-array name = "Lari">
<string-array name = "sport">
开始的字词等等
所以例如我单击listview,运行标题将是
<item >100 meter</item>
<item >mulai</item>
<item >finish</item>
<item >piala</item>
请帮忙,
抱歉,我的英语不好nb:我尝试过getstringarray这个位置,但只根据点击的列表视图的位置提升字符串
答案 0 :(得分:0)
由于声誉,我不能发表评论,但您可以简单地从所选数组中获取文本并将其与任何名称进行比较。 如果我理解正确你的问题是你无法从res /数组加载数组,其名称与listview中点击的项目相同。
我通过搜索数组点击的单词来解决它,如果匹配 - 接受它。
onItemClickListener
内的所有操作
例如:
String[] myString = null;
String text = parent.getItemAtPosition(position).toString();
if(text.equalsIgnoreCase("Lari")){
myString = getResources().getStringArray(R.array.lari);
}else if(text.equalsIgnoreCase("sport")){
myString = getResources().getStringArray(R.array.sport);
}
然后你可以通过意图传递这个数组并填充你的下一个listview
。
我希望这就是你想要的。
编辑:当您获得点击的listitem的名称时,您可能会在res / arrays中找到相同的数组。
之后,只需通过Intent
传递此数组,然后使用Adapter
填充此数组的下一个列表。
Intent intent = new Intent(this, YourNextActivity.class);
intent.putExtra("justNameForArray", mString);
在下一个活动中,您可以获得此数组:
Intent intent = getIntent();
String[] arrayFromPreviousClass = intent.getStringArrayExtra("justNameForArray");
之后你可以填写新名单。
如果你说你想要第三个列表项,你可以在onItemClickListener
内找到它:
switch(position){
case 0:
//do smthg;
break;
case 1:
//do smthg;
break;
case 2:
//here you may get your string array and pass it through intent to another activity with another view
break;
}