解決しました;)バットのアイデアは知っていますが、Globals .classを使用しましたが、動作するようになりました;)動作するようになりました;)フラー
君たちありがとう
主な質問
「ListView」アイテムを 2 回目にクリックすると、エラーが発生してアプリが閉じます。
最初は各要素で完全に機能しますが、2回目は最初に(問題なく)クリックされた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);
}
}
ありがとうございました ;)