嗨,我是新生的,所以我需要你的帮助。 每次我点击listView位置我想要膨胀活动 并且根据其可能性,我想用不同的图像和文本填充布局。这是代码..感谢您的帮助
public class WalkFragment extends Fragment {
public WalkFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.word_list, container, false);
final ArrayList<word> walk = new ArrayList<>();
walk.add(new word("Παλιά Πόλη", "Περπάτημα", R.drawable.old_town));
walk.add(new word("Φορτέτζα", "Περπάτημα", R.drawable.forteza));
walk.add(new word("Παραλία", "Περπάτημα", R.drawable.paralia));
walk.add(new word("Δημοτικός Κήπος", "Περπάτημα", R.drawable.khpos));
walk.add(new word("Περιφεριακό", "Περπάτημα", R.drawable.periferiakos));
final WordAdapter adapter =
new WordAdapter(getActivity(), walk);
final ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
// Set a click listener to inflate and populate the place_layout and InfoActivity
// with a string from strings.xml and an image from drawable
// when the list item is clicked on
// the imageView is mImageView and textView is mTextView that i need to
// populate each time i click on a different position in the listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (position == 0) {
Intent infoActivity = new Intent(getContext(), InfoActivity.class);
startActivity(infoActivity);
} else if (position == 1) {
Intent infoActivity = new Intent(getContext(), InfoActivity.class);
startActivity(infoActivity);
}
}
});
return rootView;
}
}
答案 0 :(得分:0)
使用ImageView和TextView为您的活动创建一个.xml文件。
使用以下内容替换onItemClick中的所有内容:
Intent infoActivity = new Intent(getContext(), InfoActivity.class);
intent.putExtra("position", position);
startActivity(infoActivity);
在InfoActivity的onCreate()内添加:
int position = getIntent().getIntExtra("position", -1);
switch (position){
case 0:
//set resources for imageview & textview
break;
case 1:
//set resources for imageview & textview
break;
.
.
.
.
default:
//set resources for imageview & textview
break;
}
答案 1 :(得分:0)
请参考这个答案
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
switch (position)
{
case 0:
Intent infoActivity = new Intent(getContext(), InfoActivity.class);
intent.putExtra("position", position);
startActivity(infoActivity);
break;
case 1:
Intent infoActivity = new Intent(getContext(), SecondActivity.class);
intent.putExtra("position", position);
startActivity(infoActivity);
break;
case 2:
Intent infoActivity = new Intent(getContext(), ThirdActivity.class);
intent.putExtra("position", position);
startActivity(infoActivity);
break;
case 3:
Intent infoActivity = new Intent(getContext(), FourthActivity.class);
intent.putExtra("position", position);
startActivity(infoActivity);
break;
default:
//set resources for imageview & textview
break;
}
}
});