This is timetable.java
file display listview:
public class timetable extends AppCompatActivity {
ListView lv;
// Let's set up a string array to inflate the listview with
ArrayAdapter<String> adapter;
String address = "http://192.168.1.10/timetable/conn.php";
InputStream is = null;
String line = null;
String result = null;
String[] data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timetable);
lv = (ListView) findViewById(R.id.lv);
// allow network in main thread
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
// retrieve
getData();
// adapter
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter);
// register onClickListener to handle click events on each item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedClass = data[position];
Toast.makeText(getApplicationContext(), selectedClass+" selected", Toast.LENGTH_LONG).show();
Intent intent = new Intent (timetable.this, SecondActivity.class);
intent.putExtra("SELECTED_CLASS", selectedClass);
startActivity(intent);
}
});
}
private void getData() {
try {
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
is = new BufferedInputStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
// read is content into a string
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// parse json data
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
data = new String[ja.length()];
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
data[i] = jo.getString("classname");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is SecondActivity.java
file:
public class SecondActivity extends AppCompatActivity {
private ImageView iv;
private Bitmap bitmap;
PhotoViewAttacher mAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
iv = (ImageView) findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
String selectedClass = "";
if(bundle != null){
selectedClass = bundle.getString("SELECTED_CLASS");
}
String imageUrl = "";
if (selectedClass.equalsIgnoreCase("PERP - BILIK SEMINAR 1"))
imageUrl = "http://192.168.1.10/timetable/class/bs1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK SEMINAR 2"))
imageUrl = "http://192.168.1.10/timetable/class/bs2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 1"))
imageUrl = "http://192.168.1.10/timetable/class/bt1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 10"))
imageUrl = "http://192.168.1.10/timetable/class/bt10.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 2"))
imageUrl = "http://192.168.1.10/timetable/class/bt2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 3"))
imageUrl = "http://192.168.1.10/timetable/class/bt3.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 4"))
imageUrl = "http://192.168.1.10/timetable/class/bt4.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 5"))
imageUrl = "http://192.168.1.10/timetable/class/bt5.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 6"))
imageUrl = "http://192.168.1.10/timetable/class/bt6.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 7"))
imageUrl = "http://192.168.1.10/timetable/class/bt7.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 8"))
imageUrl = "http://192.168.1.10/timetable/class/bt8.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 9"))
imageUrl = "http://192.168.1.10/timetable/class/bt9.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 1"))
imageUrl = "http://192.168.1.10/timetable/class/dk1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 2"))
imageUrl = "http://192.168.1.10/timetable/class/dk2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 3"))
imageUrl = "http://192.168.1.10/timetable/class/dk3.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 4"))
imageUrl = "http://192.168.1.10/timetable/class/dk4.jpg";
bitmap = getBitmapFromURL(imageUrl);
iv.setImageBitmap(bitmap);
mAttacher = new PhotoViewAttacher(iv);
}
public Bitmap getBitmapFromURL(String src){
try{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
listview output retrieve image output
i have 16 classname in listview, i want do when user click the each classname it will retrieve a picture (timetable) store from url mysql. each classname have different image. now i done the listview but when click the all list it also retrieve the same image. my idea is use if else but i don't know the code.
If you want to pass value selectedclass
from Timetable
to SecondActivity
, then use Intent.putExtra()
and update your onItemClick
method ad below:
Timetable.java:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String selectedclass = data[position];
Toast.makeText(getApplicationContext(), selectedclass+" selected", Toast.LENGTH_LONG).show();
Intent intent = new Intent (timetable.this, SecondActivity.class);
intent.putExtra("SELECTED_CLASS", selectedclass);
startActivity(intent);
}
});
If you want to get value selectedclass
from SecondActivity
, use getString()
.
SecondActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
iv = (ImageView) findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
String selectedClass = "";
if(bundle != null){
selectedClass = bundle.getString("SELECTED_CLASS");
}
String imageUrl = "";
if (selectedClass.equalsIgnoreCase("PERP - BILIK SEMINAR 1"))
imageUrl = "http://192.168.1.10/timetable/class/bs1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK SEMINAR 2"))
imageUrl = "http://192.168.1.10/timetable/class/bs2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 1"))
imageUrl = "http://192.168.1.10/timetable/class/bt1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 2"))
imageUrl = "http://192.168.1.10/timetable/class/bt2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 3"))
imageUrl = "http://192.168.1.10/timetable/class/bt3.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 4"))
imageUrl = "http://192.168.1.10/timetable/class/bt4.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 5"))
imageUrl = "http://192.168.1.10/timetable/class/bt5.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 6"))
imageUrl = "http://192.168.1.10/timetable/class/bt6.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 7"))
imageUrl = "http://192.168.1.10/timetable/class/bt7.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 8"))
imageUrl = "http://192.168.1.10/timetable/class/bt8.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 9"))
imageUrl = "http://192.168.1.10/timetable/class/bt9.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - BILIK TUTORIAL 10"))
imageUrl = "http://192.168.1.10/timetable/class/bt10.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 1"))
imageUrl = "http://192.168.1.10/timetable/class/dk1.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 2"))
imageUrl = "http://192.168.1.10/timetable/class/dk2.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 3"))
imageUrl = "http://192.168.1.10/timetable/class/dk3.jpg";
else if (selectedClass.equalsIgnoreCase("PERP - DEWAN KULIAH 4"))
imageUrl = "http://192.168.1.10/timetable/class/dk4.jpg";
bitmap = getBitmapFromURL(imageUrl);
iv.setImageBitmap(bitmap);
mAttacher = new PhotoViewAttacher(iv);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.