解决了;)我知道它的想法,但我使用了 Globals .class,现在它可以工作了;)现在它可以工作了;) hurahhh
感谢你们
主要问题
当我第二次单击“ListView”项目时,出现错误并且应用程序关闭。
有人可以告诉我我做错了什么,因为它第一次完美地与每个元素一起工作,而第二次它甚至不工作,即使是第一次单击的 ListView 项目(没有任何问题)再次单击。
package com.klemenjezakon.koceSLO;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class KocaInter extends ListActivity {
StringBuilder sb = new StringBuilder();
String temp;
InputStreamReader reader;
int n = 2;
koca[] koce = new koca[n];
ArrayList<String> kocee = new ArrayList<String>();
ArrayList<String> podatek = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, kocee));
inicjalizacijaKoc();
}
private void inicjalizacijaKoc() {
try {
reader = new InputStreamReader(getAssets().open("s_koce.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
for (int i = 0; i < 2; i++) {
try {
kocee.add("" + br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
int a = 0;
switch (position) {
case 0:
a=R.drawable.slika0000;
break;
case 1:
a=R.drawable.slika0001;
break;
default:
a=R.drawable.slika0000;
}
try {
reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
for(int i=-1;i<position;i++){
try {
temp = "" + br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] nov = temp.split("\\|");
for(int i=0;i<23;i++){
podatek.add(""+nov[i]);
}
temp=""+sb.append(a);
podatek.set(0,temp);
Intent in = new Intent(this, MainActivity.class);
in.putExtra("array", podatek);
startActivity(in);
podatek.clear();
}
}
主要活动:
package com.klemenjezakon.koceSLO;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
ImageView slika;
ArrayList<String> podatki;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Bundle extras = getIntent().getExtras();
if (extras != null) {
podatki = extras.getStringArrayList("array");
}
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
setContentView(R.layout.koca);
int n=-1;
int pic=++n;
TextView ime = (TextView) findViewById(R.id.ime);
ime.setText("" + (podatki.get(++n).toString()));
TextView visina = (TextView) findViewById(R.id.visina);
visina.setText("" + (podatki.get(++n).toString()));
TextView odprt = (TextView) findViewById(R.id.odprt);
odprt.setText("" + (podatki.get(++n).toString()));
TextView gorPredel = (TextView) findViewById(R.id.gorPredel);
gorPredel.setText("" + (podatki.get(++n).toString()));
TextView PD = (TextView) findViewById(R.id.PD);
PD.setText("" + (podatki.get(++n).toString()));
TextView Oskrbnik = (TextView) findViewById(R.id.Oskrbnik);
Oskrbnik.setText("" + (podatki.get(++n).toString()));
TextView tel = (TextView) findViewById(R.id.Tel);
tel.setText("" + (podatki.get(++n).toString()));
TextView GSM = (TextView) findViewById(R.id.GSM);
GSM.setText("" + (podatki.get(++n).toString()));
TextView telPD = (TextView) findViewById(R.id.TelPD);
telPD.setText("" + (podatki.get(++n).toString()));
TextView mail = (TextView) findViewById(R.id.mail);
mail.setText("" + (podatki.get(++n).toString()));
TextView web = (TextView) findViewById(R.id.web);
web.setText("" + (podatki.get(++n).toString()));
TextView naslov = (TextView) findViewById(R.id.naslov);
naslov.setText("" + (podatki.get(++n).toString()));
TextView kat = (TextView) findViewById(R.id.kat);
kat.setText("" + (podatki.get(++n).toString()));
TextView lez = (TextView) findViewById(R.id.lez);
lez.setText("" + (podatki.get(++n).toString()));
TextView stJed = (TextView) findViewById(R.id.stJed);
stJed.setText("" + (podatki.get(++n).toString()));
TextView cen = (TextView) findViewById(R.id.cen);
cen.setText("" + (podatki.get(++n).toString()));
TextView dostop = (TextView) findViewById(R.id.dostop);
dostop.setText("" + (podatki.get(++n).toString()));
TextView opis = (TextView) findViewById(R.id.opis);
opis.setText("" + (podatki.get(++n).toString()));
TextView razg = (TextView) findViewById(R.id.razg);
razg.setText("" + (podatki.get(++n).toString()));
TextView pTure = (TextView) findViewById(R.id.pTure);
pTure.setText("" + (podatki.get(++n).toString()));
TextView bKoce = (TextView) findViewById(R.id.bKoce);
bKoce.setText("" + (podatki.get(++n).toString()));
TextView vzponi = (TextView) findViewById(R.id.vzponi);
vzponi.setText("" + (podatki.get(++n).toString()));
Bitmap foto = BitmapFactory.decodeResource(getResources(),
Integer.parseInt(podatki.get(pic)));
ImageView slika = (ImageView) findViewById(R.id.foto);
slika.setImageBitmap(foto);
slika.setMinimumWidth(width);
slika.setMaxWidth(width);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
谢谢 ;)