自定义ListView和的onclick [英] Custom ListVIew and onclick

查看:113
本文介绍了自定义ListView和的onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code - 一切。我照你说的,但我还是不能老是点击什么。我的意思是我可以点击,但没有任何反应。

Here is my code - everything. I did as you told but I still can`t click on nothing. I mean I can click but nothing happens


package fixus.core;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import fixus.lib.News;
import fixus.testing.DataInput;

public class NewserActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataInput di = new DataInput();
        ArrayList news = di.getNews();

        NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
        setListAdapter(naa);
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view,
          int position, long id) {
        Log.i("testy", "I Clicked on Row " + position + " and it worked!");
      }
    });


   }

    @Override
    /**
     * When the user selects an item in the list, do an action
     * @param ListView l
     * @param View v
     * @param int position
     * @param long id
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        final int index = position;

        //You can add whatever you want to happen when you click here

        Log.i("testy", "I Clicked on Row " + index + " and it worked!");

    }


    private class NewsArrayAdapter extends ArrayAdapter {
        protected ArrayList items;

        public NewsArrayAdapter(Context context, int textViewResourceId, ArrayList items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if(v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }

            News news = this.items.get(position);
            if(news != null) {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);

                if (tt != null) {
                      tt.setText("Name: "+ news.getName());
                }
                if(bt != null){
                      bt.setText("Status: "+ news.getUrl().toString());
                }
            }

            return v;
        }
    }

}

如果你知道一个好的教程/例子来证明我是很乐意看到它的好办法:)

If you know a good tutorial/example that would show me the good way I would love to see it :)

推荐答案

在你的清单活动,你应该做的是类似如下:

In your list activity what you should do is something like the following:

public class YourClass extends ListActivity {
    //Your Variables
    ArrayList<Type> yourlist;
    YourAdapterClass adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        yourlist = new ArrayList<Type>();
        this.adapter = new YourAdapterClass(this, R.layout.row, yourlist);
        setListAdapter(this.adapter);
        //you might be able to see if the below works instead of overriding 
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        Log.i("testy", "I Clicked on Row " + position + " and it worked!");
      }
    });
    }

    @Override
    /**
     * When the user selects an item in the list, do an action
     * @param ListView l
     * @param View v
     * @param int position
     * @param long id
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        final int index = position;

        //You can add whatever you want to happen when you click here

        Log.i("testy", "I Clicked on Row " + index + " and it worked!");

    }

//other methods can go here for you list


}

您不希望您的 onClickListener getView(...)方法中的列表适配器,这只是你在哪里想修改的行看起来像这样(添加按钮,文本框等,对于每一行),而不是你想拥有的 onClickListener 在扩展一个类 ListActivity 连接到你的适配器

You don't want to have your onClickListener inside your getView(...) method in the List Adapter, that is just where you are suppose modify the way your row looks like (adding buttons, textfields, etc. for each row) instead you want to have the onClickListener in a class that extends ListActivity that connects to your Adapter

祝你好运,希望这有助于

Good luck, hope this helped

//编辑(添加详细信息)

//Edit (adding more info)

您需要在onCreate方法将其添加到您的适配器(见上文编辑code)希望这应该解决这个问题。让我知道,如果它仍然不工作,虽然。如果不工作,你可以尝试使用 setOnItemClickListener code我放在的onCreate 方法来代替,如果仍然不工作,我会尝试检查了一些教程(我会看看我能找到你一个好)

You need to 'add it to your adapter' in the onCreate method (see edited code above) Hopefully that should fix the problem. Let me know if it still doesn't work though. If that doesn't work you can try using the setOnItemClickListener code I put in the onCreate method instead, if that still doesn't work I would try checking out some tutorials (I'll see if i can find you a good one)

这篇关于自定义ListView和的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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