如何单击列表视图项?

时间:2018-07-11 05:06:12

标签: java android json

我已经成功获取json数据并进行了列表视图。但是我无法单击列表项,也无法将其他内容发送给其他活动。这是我的适配器。

我想将地址字符串发送到新活动。 (我是android开发人员中的新手)

这是我的工作。我做错了什么? 列表显示完美,但是没有点击。 :(

private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
private ListView listView;


ArrayList<HashMap<String, String>> channelList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.live_stream);

    channelList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.list);

    new GetChannel().execute();
}





private class GetChannel extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(LiveStreaming.this, "Streaming list is loading", Toast.LENGTH_LONG).show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "http://ubpower.net/cricktv/tv.json";
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);
        if (jsonStr != null) {

            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray channel = jsonObj.getJSONArray("channel");

                for (int i = 0; i < channel.length(); i++) {
                   JSONObject c = channel.getJSONObject(i);
                    String id = c.getString("id");
                    String name = c.getString("name");
                    String quality = c.getString("quality");
                    address = c.getString("address");


                    // tmp hash map for single contact
                    HashMap<String, String> channel_hash = new HashMap<>();

                    // adding each child node to HashMap key => value
                    channel_hash.put("id", id);
                    channel_hash.put("name", name);
                    channel_hash.put("quality", quality);
                    channel_hash.put("address", address);

                    // adding contact to contact list
                    channelList.add(channel_hash);
                }



            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ListAdapter adapter = new SimpleAdapter(LiveStreaming.this, channelList,
                R.layout.stream_list, new String[]{ "name","quality","address"},
                new int[]{R.id.channel_title, R.id.channel_quality, R.id.channel_address});

        lv.setAdapter(adapter);




    }

    public AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Intent intent = new Intent(context, video_view.class);
            intent.putExtra("address", address);
            startActivity(intent);
            lv.setOnItemClickListener(mMessageClickedHandler);

        }

    };

}

}

2 个答案:

答案 0 :(得分:1)

您忘记设置监听器。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.live_stream);

    channelList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.list);
    lv.setOnItemClickListener(mMessageClickedHandler);
    new GetChannel().execute();
}

答案 1 :(得分:0)

您可以使用此代码。

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});