[英]Creating new activity when clicking on listView Item
I am new to android developing and i got a few troubles there.I made a list of items in ListView, so i want to generate a new activity based on which item was clicked. 我是Android开发的新手,我在这里遇到了一些麻烦。我在ListView中列出了项目列表,因此我想根据单击的项目生成一个新的活动。 In the first class as you can see i want to open an activity with imageView with AKM texture which is already in drawable. 如您所见,在第一堂课中,我想使用带有可绘制的AKM纹理的imageView打开一个活动。 When click on m416, i want to generetare an activity with imageView and m416 texture which is also in drawable and same with other items. 当单击m416时,我想生成一个具有imageView和m416纹理的活动,该活动也是可绘制的,并且与其他项目相同。 I look for any help there, i will be greatful for anything. 我在那里寻求任何帮助,我将竭尽所能。 PS sorry for english. PS对不起英语。
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Weaponry extends AppCompatActivity {
public static final int EXTRA_KEY_COUNT = R.drawable.akm;
ListView listView;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weaponry);
listView = (ListView) findViewById(R.id.ivMain);
final String[] values = new String[] {"AKM", "M416", "Scar-L", "GROZA", "UMP", "UZI",
"AWM", "M-14", "M24", "Colt 1911", "Glock-19"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent intent = new Intent(Weaponry.this, WeaponDetails.class);
intent.putExtra(String.valueOf(EXTRA_KEY_COUNT), values[position]);
startActivity(intent);
} else if (position == 1) {
Intent intent1 = new Intent(Weaponry.this, WeaponDetails.class);
intent1.putExtra("R.drawable.m416", values[position]);
startActivity(intent1);
}
}
});
}
}</i>
"Future generated activity" “未来产生的活动”
public class WeaponDetails extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weapon_details);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource();
}
}
and if needed xml code for second acitivity 以及是否需要xml代码进行二次激活
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.drews.projnumba.WeaponDetails">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_marginStart="4dp"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
in case of something there is xml code for the first activity 在某些情况下,第一个活动有xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weapon_types">
</TextView>
<ListView
android:id="@+id/ivMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Replace this : 替换为:
Intent intent = new Intent(Weaponry.this, WeaponDetails.class);
With: 带有:
Intent intent = new Intent(getContext(), WeaponDetails.class);
and: 和:
startActivity(intent);
with: 与:
getContext().startActivity(intent);
You have tu put the value inside the extras 您已经将价值放在附加项目中
intent1.putExtra("name", values[position]);
intent1.putExtra("image_resource",
R.drawable.m416);
Then use this on your new activity: 然后将其用于您的新活动:
int imageResource = getIntent().getIntExtra("image_resource");
You just Copy And Paste this code.. 您只需复制并粘贴此代码即可。
final ListView listView = (ListView) findViewById(R.id.ivMain);
final String[] values = new String[] {"AKM", "M416", "Scar-L", "GROZA", "UMP", "UZI",
"AWM", "M-14", "M24", "Colt 1911", "Glock-19"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromList = (String) listView.getItemAtPosition(position);
switch (position){
case 0:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("name",selectedFromList);
Log.d("name",selectedFromList);
intent.putExtra("pos",position);
intent.putExtra("image_resource",EXTRA_KEY_COUNT);
startActivity(intent);
break;
//
default:break;
}
}
});
First of all you can store the image drawable like 首先,您可以像
final int[] imageResources = new int[] {R.drawable.AKM, R.drawable.M416, R.drawable.UMP};
then you can pass the image resource id with the intent 那么您可以通过意图传递图像资源ID
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(Weaponry.this, WeaponDetails.class).putExtra("image", imageResources[position]));
}
});
Then in the WeaponDetails.class you can fetch the image resource and set the imageView using 然后在WeaponDetails.class中,您可以获取图像资源并使用以下命令设置imageView
int image = getIntent().getExtras().getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.