所以我有一个包含一堆行的列表视图,当你点击它时会打开一个新的活动。此新活动显示一些信息,然后显示其下方的图像。我已成功获取字符串信息以使用xml中的字符串数组显示。但我无法弄清楚如何使用xml中的整数数组和一堆@drawable链接来实现它。
有没有办法像我使用routedetail字符串数组那样做。我可以在哪里使用intent.getExtra()
继承我的mainactivity.java
package com.example.zach.listview;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//fill list view with xml array of routes
final String[] values = getResources().getStringArray(R.array.routeList);
//fills route detail text view with xml array info
final String[] routeDetail= getResources().getStringArray(R.array.routeDetail);
//fills route detail image view with xml array of images
final TypedArray image = getResources().obtainTypedArray(R.array.routeImage);
//custom adapter for list view
ListAdapter routeAdapter = new CustomAdapter(this, values);
final ListView routeListView = (ListView) findViewById(R.id.routeListView);
routeListView.setAdapter(routeAdapter);
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String route = values[position];
//for loop to increment through each route list item
int i;
for (i=0; i < values.length;i++)
{
if (route.equals(values[i]))
{
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[i]);
startActivity(intent);
}
}
}
}
);
}
}
继承我的路线detail.java
这是引用我在mainactivity中的意图的行 routeDetailsView.setText(getIntent()getExtras()的getString(&#34;路线&#34));
package com.example.zach.listview;
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
import android.widget.TextView;
public class RouteDetails extends AppCompatActivity {
Matrix matrix = new Matrix();
Float scale = 1f;
ScaleGestureDetector SGD;
ImageView routeImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));
//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
//routeImage.setImageResource(R.drawable.birdsboroareamap);
SGD = new ScaleGestureDetector(this, new ScaleListener());
}
//listener for pinch zoom
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
@Override
public boolean onScale(ScaleGestureDetector detector) {
scale = scale * detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5f));
matrix.setScale(scale, scale);
routeImage.setImageMatrix(matrix);
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
SGD.onTouchEvent(event);
return true;
}
}
并且继承了我的routelistviewitems xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="routeList">
<item>Information</item>
<item>Crag Map</item>
<item>Main Wall</item>
<item>1. Shark Bait - 5.9</item>
<item>2. Rain Check - 5.8</item>
<item>3. Rain Check Direct - 5.8</item>
<item>4. Arocknophobia - 5.7</item>
<item>5. Balls Deep - 5.9+</item>
<item>6. Jingle Bells - 5.9</item>
<item>7. Itching to Climb - 5.8</item>
<item>8. Hog Nose 5.10+</item>
<item>9. Slab Dance - 5.10</item>
</string-array>
<string-array name="routeDetail">
<item>Information</item>
<item>Crag Map</item>
<item>Overview of the Main Wall Climbing Area</item>
<item>1. Shark Bait - 5.9</item>
<item>2. Rain Check - 5.8</item>
<item>3. Rain Check Direct - 5.8</item>
<item>4. Arocknophobia - 5.7</item>
<item>5. Balls Deep - 5.9+</item>
<item>6. Jingle Bells - 5.9</item>
<item>7. Itching to Climb - 5.8</item>
<item>8. Hog Nose 5.10+</item>
<item>9. Slab Dance - 5.10</item>
</string-array>
<integer-array name="routeImage">
<item>@drawable/birdsboroareamap</item>
</integer-array>
</resources>
感谢您的帮助! 还在寻找任何帮助!