我是Android新手,我在那里遇到了一些麻烦。我在ListView中创建了一个项目列表,所以我想根据点击的项目生成一个新的活动。在第一堂课中你可以看到我想用带有AKM纹理的imageView打开一个活动,该纹理已经在drawable中。当点击m416时,我想使用imageView和m416纹理生成一个活动,该活动也是可绘制的,与其他项目相同。我在那里寻求任何帮助,我会为任何事情感到高兴。 附:对不起英语。
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>
“未来生成的活动”
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();
}
}
如果需要第二个活动的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>
如果有第一个活动的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>
答案 0 :(得分:0)
替换它:
Intent intent = new Intent(Weaponry.this, WeaponDetails.class);
使用:
Intent intent = new Intent(getContext(), WeaponDetails.class);
和
startActivity(intent);
使用:
getContext().startActivity(intent);
答案 1 :(得分:0)
你把这个值放在额外内容
intent1.putExtra("name", values[position]);
intent1.putExtra("image_resource",
R.drawable.m416);
然后在新活动中使用此功能:
int imageResource = getIntent().getIntExtra("image_resource");
答案 2 :(得分:0)
您只需复制并粘贴此代码..
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;
}
}
});
答案 3 :(得分:0)
首先,您可以将图像存储为
final int[] imageResources = new int[] {R.drawable.AKM, R.drawable.M416, R.drawable.UMP};
然后您可以使用intent
传递图像资源IDlistView.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]));
}
});
然后在WeaponDetails.class中,您可以获取图像资源并使用
设置imageViewint image = getIntent().getExtras().getInt("image");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(image);