package com.project.cookingguide;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class FoodActivity extends Activity {
private ListView listfood;
private ListViewAdap listViewAdap;
private ArrayList<EntryFood> entry ;
private int[] drawables = {
R.drawable.k1,
R.drawable.k2,
R.drawable.k3,
R.drawable.k4,
R.drawable.k5,
R.drawable.k6,
R.drawable.k7,
R.drawable.k8,
R.drawable.k9,
R.drawable.k10,
R.drawable.k11,
R.drawable.k12,
R.drawable.k13,
R.drawable.k14,
R.drawable.k15
};
private String[] titles = {
"แกงส้มชะอมไข่",//0
"แกงเขียวหวาน",//1
"แกงฟักทอง",//2
"ต้มข่าไก่",//3
"ต้มยำกุ้งน้ำข้น",//4
"ยำวุ้นเส้น",//5
"ยำสามกรอบ",//6
"ปลากะพงนึ่งซีอิ๊ว",//7
"ทอดมันกุ้ง",//8
"ปลาทับทิมทอดกระเทียม",//9
"ไก่ผัดซอส",//10
"ปลาหมึกผัดไข่เค็ม",//11
"ผัดซีอิ๊ว",//12
"หอยเซลล์ย่างซอสมิโซะ",//13
"ไก่ย่างราดซอสน้ำสลัดญี่ปุ่น"//14
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_layout);
//view matching
listfood = (ListView) findViewById(R.id.list_food);
entry = new ArrayList<EntryFood>
);
for (int i = 0 ;i < drawables.length; i++) {
EntryFood entryFood = new EntryFood();
entryFood.setTitle(titles
);
entryFood.setDrawable(getResources().getDrawable(drawables));
entry.add(entryFood);
}
listViewAdap = new ListViewAdap();
listfood.setAdapter(listViewAdap);
//even
listfood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
switch (position) {
case 0:
Intent intent1 = new Intent(FoodActivity.this,MenuK1.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(FoodActivity.this,MenuK2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(FoodActivity.this,MenuK3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(FoodActivity.this,MenuK4.class);
startActivity(intent4);
break;
}
}
});
}
private class ListViewAdap extends BaseAdapter {
private ListViewHold hold;
@Override
public int getCount() {
return entry.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(FoodActivity.this).inflate(R.layout.layout_listview, null);
hold = new ListViewHold();
hold.image = (ImageView) convertView.findViewById(R.id.image_list);
hold.title = (TextView) convertView.findViewById(R.id.text_list);
if (entry.get(position).getTitle()!= null) {
hold.title.setText(entry.get(position).getTitle());
}
if (entry.get(position).getDrawable() != null) {
hold.image.setImageDrawable(entry.get(position).getDrawable());
}
return convertView;
}
private class ListViewHold {
public ImageView image;
public TextView title;
}
}
}
นี่ตัวโค๊ดผมครับ ตัว intent อ่ะครับ case 0 - 2 เข้าได้ปกติ พอมา case 3 ขึ้นแอพจะเด้งอ่ะครับ ไม่มีเออเร่อเลย ช่วยทีนะครับ
ผมมีปัญหาเกี่ยวกับการเขียนแอพ Android ครับ ผู้รู้ช่วยด้วยครับ
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class FoodActivity extends Activity {
private ListView listfood;
private ListViewAdap listViewAdap;
private ArrayList<EntryFood> entry ;
private int[] drawables = {
R.drawable.k1,
R.drawable.k2,
R.drawable.k3,
R.drawable.k4,
R.drawable.k5,
R.drawable.k6,
R.drawable.k7,
R.drawable.k8,
R.drawable.k9,
R.drawable.k10,
R.drawable.k11,
R.drawable.k12,
R.drawable.k13,
R.drawable.k14,
R.drawable.k15
};
private String[] titles = {
"แกงส้มชะอมไข่",//0
"แกงเขียวหวาน",//1
"แกงฟักทอง",//2
"ต้มข่าไก่",//3
"ต้มยำกุ้งน้ำข้น",//4
"ยำวุ้นเส้น",//5
"ยำสามกรอบ",//6
"ปลากะพงนึ่งซีอิ๊ว",//7
"ทอดมันกุ้ง",//8
"ปลาทับทิมทอดกระเทียม",//9
"ไก่ผัดซอส",//10
"ปลาหมึกผัดไข่เค็ม",//11
"ผัดซีอิ๊ว",//12
"หอยเซลล์ย่างซอสมิโซะ",//13
"ไก่ย่างราดซอสน้ำสลัดญี่ปุ่น"//14
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_layout);
//view matching
listfood = (ListView) findViewById(R.id.list_food);
entry = new ArrayList<EntryFood>);
for (int i = 0 ;i < drawables.length; i++) {
EntryFood entryFood = new EntryFood();
entryFood.setTitle(titles);
entryFood.setDrawable(getResources().getDrawable(drawables));
entry.add(entryFood);
}
listViewAdap = new ListViewAdap();
listfood.setAdapter(listViewAdap);
//even
listfood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
switch (position) {
case 0:
Intent intent1 = new Intent(FoodActivity.this,MenuK1.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(FoodActivity.this,MenuK2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(FoodActivity.this,MenuK3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(FoodActivity.this,MenuK4.class);
startActivity(intent4);
break;
}
}
});
}
private class ListViewAdap extends BaseAdapter {
private ListViewHold hold;
@Override
public int getCount() {
return entry.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(FoodActivity.this).inflate(R.layout.layout_listview, null);
hold = new ListViewHold();
hold.image = (ImageView) convertView.findViewById(R.id.image_list);
hold.title = (TextView) convertView.findViewById(R.id.text_list);
if (entry.get(position).getTitle()!= null) {
hold.title.setText(entry.get(position).getTitle());
}
if (entry.get(position).getDrawable() != null) {
hold.image.setImageDrawable(entry.get(position).getDrawable());
}
return convertView;
}
private class ListViewHold {
public ImageView image;
public TextView title;
}
}
}
นี่ตัวโค๊ดผมครับ ตัว intent อ่ะครับ case 0 - 2 เข้าได้ปกติ พอมา case 3 ขึ้นแอพจะเด้งอ่ะครับ ไม่มีเออเร่อเลย ช่วยทีนะครับ