首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我完成了listview,在点击listview之后,它可以在android studio中显示url mysql的图像。

我完成了listview,在点击listview之后,它可以在android studio中显示url mysql的图像。
EN

Stack Overflow用户
提问于 2017-04-11 15:47:40
回答 1查看 69关注 0票数 0

这是timetable.java文件显示列表视图:

代码语言:javascript
复制
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();
        }
    }
}

这是SecondActivity.java文件:

代码语言:javascript
复制
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中有16个类名,当用户单击每个类名时,它将从url检索图片(时间表)存储。每个类名都有不同的形象。现在,我完成了listview,但是当单击also时,它也会检索相同的图像。我的想法是使用如果其他,但我不知道代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-11 16:09:36

如果要将值selectedclassTimetable传递给SecondActivity,请使用Intent.putExtra()并在下面更新onItemClick方法:

Timetable.java:

代码语言:javascript
复制
    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);
        }
    });

如果您想从selectedclass获得SecondActivity的值,请使用getString()

SecondActivity.java:

代码语言:javascript
复制
@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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43350676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档