嘿伙计们,自从过去4个小时以来,我需要你帮忙解决这个问题,但却无法完全掌握它。
实际上我有很多图像,并希望根据ListView上的点击显示它们
到目前为止我做了什么:
创建了2个xml文件:
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:divider="#00000000" />
</RelativeLayout>
formulas.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivFormulas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/formulas" />
</LinearLayout>
现在我用java代码完成了3个类
Formulas.java
package com.shivam.listview;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Formulas extends ListActivity {
String name[] = {"Triangle 1", "Triangle 2"};
String files[] = {"formulas/triangle1.png", "formulas/triangle2.png"};
ListView lv;
private ForAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if( mAdapter == null){
mAdapter = new ForAdapter(this);
setListAdapter(mAdapter);
}
mAdapter.setData(name);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
showText(files[position]);
}
private void showText(String filename) {
Intent showIt = new Intent(this, Formulas.class);
Bundle toShow = new Bundle();
toShow.putString("show_for", filename);
showIt.putExtras(toShow);
startActivity(showIt);
}
}
这里我取位置并将file []的值传递给SetFormulas.java并将值传递给ForAdapter.java
ForAdapter.java
package com.shivam.listview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class ForAdapter extends ArrayAdapter<String>{
private final LayoutInflater mInflater;
public ForAdapter(Context context) {
super(context, android.R.layout.simple_list_item_2);
mInflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(String name[]) {
clear();
if(name != null){
for (String formu : name)
add(formu);
}
}
public View getView(int position, View convertView,
ViewGroup parent){
View view;
if(convertView == null){
view = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
}
else{
view = convertView;
}
String data = this.getItem(position);
((ImageView) view.findViewById(R.id.ivFormulas)).(data);
return view;
}
}
SetFormulas.java
package com.shivam.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class SetFormulas extends Activity{
ImageView ivFormulas;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.id.ivFormulas);
initializers();
}
private void initializers() {
ivFormulas = (ImageView) findViewById(R.id.ivFormulas);
Bundle getBasket = getIntent().getExtras();
String mFormulas = getBasket.getString("show_for");
ivFormulas.setImageDrawable(mFormulas);
}
}
另外请详细解释一下,以便我能够理解并且不会再次重复这个错误。
P.S:我已将图像放在assets / forumlas以及drawable /文件夹中。
由于