o19830015
o19830015
2013-01-07 02:18
采纳率: 76.7%
浏览 12.5k

android中自定义ListView的单击事件不响应

本人创建了自定义ListAdapter,但是每行的单击事件不响应,应该实现的是点击之后获取这行的一些内容然后在另外的Activity中用。

public class MainActivity extends Activity {

// ArrayList of type "Person". We are making not a list of string, sondern eine list of persons
ArrayList<Person> arrayOfWebData = new ArrayList<Person>();

class Person{
    public String person_id;
    public String name;
    public String birthday;
    public String favorite_color;
    public String profilbild;
}

// This is our new Adapter:
FancyAdapter aa = null;

// For each row we returned until we use the array to create our person obj
static ArrayList<String> resultRow;

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

// Params,Progress,Result
private class getJson extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... arg0) {
        String result = "";
        try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.julianfuerderer.de/json.php");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            // Convert response to String:
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;

                while((line = reader.readLine()) != null){
                    sb.append(line+"\n");
                }
                is.close();
                result = sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        // Parse JSON:
        try{
            JSONArray jArray = new JSONArray(result);
            for(int i = 0;i < jArray.length();i++)
            {
                // Get our object, this is on persons data:
                JSONObject json_data = jArray.getJSONObject(i);
                // Create a new person:
                Person resultRow = new Person();
                // Set thats persons attributes:
                resultRow.person_id = json_data.getString("id");
                resultRow.name = json_data.getString("name");
                resultRow.favorite_color = json_data.getString("favorite_color");
                resultRow.birthday = json_data.getString("birthday");
                resultRow.profilbild = json_data.getString("profilbild");
                arrayOfWebData.add(resultRow);
            }   
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;    
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        populateListView();
    }   
}




public void populateListView()
{
    final ListView myListView = (ListView)findViewById(R.id.myListView);
    // initialite FancyAdapter object:
    aa = new FancyAdapter();
    myListView.setAdapter(aa);  

    // Click on ListView Row:
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    {
          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
          {
                 Object o = myListView.getItemAtPosition(position);
                // Here i want have values of the clicked row,- like the name and/or id...,- but i get the following:
                // Output o.toString(): com.example.customlistview.MainActivity$Person@41252310
                Log.i("TEST", o.toString()); 

                // Here i intend to Start a new Activity passing the "name" of the user (and/or id ...) to the new activity
          }
    }); 
}


class FancyAdapter extends ArrayAdapter<Person>
{
    FancyAdapter(){
        super(MainActivity.this,android.R.layout.simple_list_item_1,arrayOfWebData);
        //imageLoader = new ImageLoader(MainActivity.this);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.row3, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder); 
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arrayOfWebData.get(position));
        return convertView; 
    }
}

class ViewHolder
{
    public TextView name = null;
    public TextView birthday = null;
    public TextView favorite_color = null;
    public ImageView profilbild = null;
    //public ImageLoader imageLoader;
    ImageLoader imageLoader = new ImageLoader(MainActivity.this);

    ViewHolder(View row){
        name = (TextView)row.findViewById(R.id.name);
        birthday = (TextView)row.findViewById(R.id.birthday);
        favorite_color = (TextView)row.findViewById(R.id.favorite_color);
        profilbild = (ImageView)row.findViewById(R.id.imageView1);  
    }
    // Notice we have to change our populateFrom() to take an argument of type "Person"
    void populateFrom(Person r){
        name.setText(r.name);
        birthday.setText(r.birthday);
        favorite_color.setText(r.favorite_color);
        imageLoader.DisplayImage(r.profilbild, profilbild); 
    }   
}

请帮忙看一下populateListView()方法,当运行应用点击某一行时,通过log输出,得到:

 // Output o.toString(): com.example.customlistview.MainActivity$Person@41252310
 Log.i("TEST", o.toString())

我想输出的是内容,比如name或者id。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

18条回答 默认 最新

  • Brightshadow11111
    五柳--先生 2014-11-18 04:29
    已采纳

    看看你有没有在xml中把点击事件屏蔽掉,再看看 你在adapter里面的contentview 里面有没有绑定点击时间,在adaper里面绑定了事件,listview.setonitemclicklistener 会捕捉不到点击事件

    点赞 评论
  • cytown
    Billy_崔海斌 2013-01-07 02:36
    Log.i("TEST", arrayOfWebData[position].name);
    

    如果没反应,在ListView里面加

    android:focusable="false"
    android:focusableInTouchMode="false"
    
    点赞 评论
  • liu13722785488
    棒怡情 2014-11-19 01:39

    你在onItemClick并没有进行什么可以跳转的操作啊,怎么知道它没有执行呢?

    点赞 评论
  • bao594681259
    bao594681259 2014-11-21 04:13

    你没有绑定ListView 单项item 事件

    点赞 评论
  • u013718120
    Songlcy 2014-11-21 04:39

    setOnItemOnClick试一下,可以能是因为你没有给单个选项添加事件

    点赞 评论
  • start_smile_life
    酱酱酱ing 2014-11-23 02:17

    焦点抢夺?你的listview 里面有没有button imagebutton?

    点赞 评论
  • guwei4037
    guwei4037 2014-11-23 04:43

    参考一下这个:ListView

    点赞 评论
  • u011271055
    vicky-y 2014-11-24 10:39

    你不是有arrayOfWebData这个List嘛, 你可以直接在OnItemClick里面写arrayOfWebData.getPosition(position)来获取点击那一条的内容,如果你的arrayOfWebData不是全局变量,你可以重新声明一个内容和这个一样。不必存在object里面。

    点赞 评论
  • zxz837807522
    zxz837807522 2014-11-25 02:12

    android:descendantFocusability="blocksDescendants" 加上这句代码

    点赞 评论
  • qq_21235171
    qq_21235171 2014-11-27 01:24

    你那item position没有设置

    点赞 评论
  • wangfei199101
    默默耕田人 2014-11-27 01:35

    参照Android源码中的ListView编写基本不会出错

    点赞 评论
  • qq_21119327
    碎比 2014-11-27 04:15

    你没有监听事件吧,监听到才能操作对象吧

    点赞 评论
  • frj463806056
    不必了 2014-11-27 12:16

    这不是点击了没反应!按照你想要的,解决办法:首先你可以在Person类中实现toString()的方法,其次在Object o = myListView.getItemAtPosition(position); 可以换成Person person=(Person)myListView.getItemAtPosition(position);然后再打印Log.i("TEST",person.toString()); 。第二种方法就是在打印的时候Log.i("TEST","name:"+person.name+",id:"+person.id);

    点赞 评论
  • u010700335
    瞭望天空 2014-11-28 01:38

    设置断点调试一下,试一试~~~

    点赞 评论
  • shexiaoheng
    shexiaoheng 2014-12-01 03:43

    把onItemClick里面的
    Object o = myListView.getItemAtPosition(position);
    Log.i("TEST", o.toString());

    改为如下内容,得到的结果就是你想要的
    if ( arrayOfWebData!=null && arrayOfWebData.size() > 0 ){
    Log.i("TEST", arrayOfWebData.get(position).name);
    Log.i("TEST", arrayOfWebData.get(position).person_id);
    }

    点赞 评论
  • u013179982
    豌豆琪琪 2014-12-01 14:26

    可能是事件被占用了

    点赞 评论
  • menglele1314
    随意而然 2014-12-15 10:30

    android:descendantFocusability=”blocksDescendants”

    点赞 评论
  • u011997773
    she11111222 2014-12-29 09:02

    确认下item中有没有其他可以获取focus的组件如checkbox等,如果有在xml里面将其设置成focusable=flase,应该就ok了

    点赞 评论

相关推荐