与onItemClickListener不工作的应用 [英] Application with onItemClickListener not working

查看:217
本文介绍了与onItemClickListener不工作的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个应用程序。我想获得一些信息在Web上的JSON文件。这个工程。现在我想有一个onItemClickListener与内容列表视图中的项目。该onItemClickListener必须发送点击列表中的信息,以我的新的活动。

我MainActiviy.java的code是:

 包com.jitesh.androidjsonparser;

进口的java.util.ArrayList;
进口的java.util.HashMap;

进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;

进口android.app.ListActivity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;


公共类MainActivity扩展ListActivity {
    私人上下文的背景下;
    私有静态字符串URL =htt​​p://docs.blackberry.com/sampledata.json; // URL VAN HET JSON BESTAND

    私有静态最后弦乐TAG_VTYPE =vehicleType; //费尔登UIT DE JSON HALEN
    私有静态最后弦乐TAG_VCOLOR =vehicleColor;
    私有静态最后弦乐TAG_FUEL =燃料;
    私有静态最后弦乐TAG_TREAD =treadType;
    私有静态最后弦乐TAG_OPERATOR =approvedOperators;
    私有静态最后弦乐TAG_NAME =名;
    私有静态最后弦乐TAG_POINTS =experiencePoints;

    ArrayList的< HashMap的<字符串,字符串>> jsonlist =新的ArrayList< HashMap的<字符串,字符串>>();

    ListView的LV;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        新ProgressTask(MainActivity.this).execute();
    }

    私有类ProgressTask扩展的AsyncTask<字符串,太虚,布尔> {
        私人ProgressDialog对话框;

        私人ListActivity的活动;

        //私有列表<消息>消息;
        公共ProgressTask(ListActivity活动){
            this.activity =活动;
            上下文=活动;
            对话框=新ProgressDialog(上下文);
        }

        / **进度对话框显示用户的备份处理。 * /

        / **应用程序上下文。 * /
        私人上下文的背景下;

        在preExecute保护无效(){
            this.dialog.setMessage(进度开始);
            this.dialog.show();
        }

        @覆盖
        保护无效onPostExecute(最终布尔成功){
            如果(dialog.isShowing()){
                dialog.dismiss();
            }
            ListAdapter适配器=新SimpleAdapter(背景下,jsonlist,
                    R.layout.list_item,新的String [] {TAG_VTYPE,TAG_VCOLOR,
                            TAG_FUEL,TAG_TREAD},新的INT [] {
                            R.id.vehicleType,R.id.vehicleColor,R.id.fuel,
                            R.id.treadType});

            setListAdapter(适配器);

            //选择单一的ListView项
             LV = getListView();
        }

        保护布尔doInBackground(最后的字符串参数... args){

            JSONParser jParser =新JSONParser();

            //从URL获取JSON字符串
            JSONArray JSON = jParser.getJSONFromUrl(URL);

            的for(int i = 0; I< json.length();我++){

                尝试 {
                    JSONObject的C = json.getJSONObject(我);
                    字符串由Vtype = c.getString(TAG_VTYPE);

                    字符串vcolor = c.getString(TAG_VCOLOR);
                    字符串vfuel = c.getString(TAG_FUEL);
                    字符串vtread = c.getString(TAG_TREAD);

                    HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();

                    //添加每个子节点HashMap中的key =>值
                    map.put(TAG_VTYPE,Vtype域);
                    map.put(TAG_VCOLOR,vcolor);
                    map.put(TAG_FUEL,vfuel);
                    map.put(TAG_TREAD,vtread);
                    jsonlist.add(图)
                }赶上(JSONException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
            返回null;

        }


    }

}
 

谁能帮我用onItemClickListener和哪个地方我必须设置该onItemClickListener?

  @覆盖保护无效onListItemClick(ListView的LV,视图V,INT位置,长的id){
         意图I =新的意图(getApplicationContext(),single_list_item_view.class);
         串vehicleType =((TextView的)ⅴ).getText()的toString()。
         i.putExtra(TAG_VTYPE,vehicleType);
         startActivity(ⅰ);
 }
 

解决方案

您正在扩展 ListActivity ,所以你不需要 onItemClickListener 。您可以直接覆盖 onListItemClick(ListView的L,视图V,INT位置,长ID)。当在列表中选择了一个项目,此方法将被调用。

正如你做的onCreate,可以为 onListItemClick

做同样的

  @覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    意图I =新的意图(这一点,single_list_item_view.class);
    HashMap的<字符串,字符串>项目pressed =(HashMap的<字符串,字符串>)l.getItemAtPosition(位置);
    字符串vehicleType =项目pressed.get(TAG_VTYPE);
     i.putExtra(TAG_VTYPE,vehicleType);
     startActivity(ⅰ);
}
 

I am making an application. I want to get some information out of a Json file on the web. This works. Now i want to have an onItemClickListener to the items of the listview with the content. The onItemClickListener must send the information of the clicked list to my new activity.

The code of my MainActiviy.java is:

package com.jitesh.androidjsonparser;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MainActivity extends ListActivity {
    private Context context;
    private static String url = "http://docs.blackberry.com/sampledata.json"; // URL VAN HET JSON BESTAND

    private static final String TAG_VTYPE = "vehicleType"; //VELDEN UIT DE JSON HALEN
    private static final String TAG_VCOLOR = "vehicleColor";
    private static final String TAG_FUEL = "fuel";
    private static final String TAG_TREAD = "treadType";
    private static final String TAG_OPERATOR = "approvedOperators";
    private static final String TAG_NAME = "name";
    private static final String TAG_POINTS = "experiencePoints";

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        private ListActivity activity;

        // private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR,
                            TAG_FUEL, TAG_TREAD }, new int[] {
                            R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
                            R.id.treadType });

            setListAdapter(adapter);

            // selecting single ListView item
             lv = getListView();             
        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);
                    String vtype = c.getString(TAG_VTYPE);

                    String vcolor = c.getString(TAG_VCOLOR);
                    String vfuel = c.getString(TAG_FUEL);
                    String vtread = c.getString(TAG_TREAD);

                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_VTYPE, vtype);
                    map.put(TAG_VCOLOR, vcolor);
                    map.put(TAG_FUEL, vfuel);
                    map.put(TAG_TREAD, vtread);
                    jsonlist.add(map);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;

        }


    }

}

Can anyone help me with the onItemClickListener and on which place i must set this onItemClickListener?

@Override protected void onListItemClick(ListView lv, View v, int position, long id) {       
         Intent i = new Intent(getApplicationContext(), single_list_item_view.class);     
         String vehicleType = ((TextView) v).getText().toString();   
         i.putExtra("TAG_VTYPE", vehicleType); 
         startActivity(i); 
 }

解决方案

You are extending ListActivity, so you do not need onItemClickListener. You can directly override onListItemClick(ListView l, View v, int position, long id). This method will be called when an item in the list is selected.

As you did with onCreate, you can do the same for onListItemClick

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, single_list_item_view.class);     
    HashMap<String, String> itemPressed = (HashMap<String, String>)l.getItemAtPosition(position); 
    String vehicleType = itemPressed.get(TAG_VTYPE);   
     i.putExtra("TAG_VTYPE", vehicleType); 
     startActivity(i); 
}

这篇关于与onItemClickListener不工作的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆