当我的 ListView 项目被选中时,无法突出显示它们

发布于 2024-10-16 19:29:30 字数 2379 浏览 1 评论 0原文

我有一个应用程序,它访问 REST 服务,获取一些 json,然后将其放入列表视图中。我的主要活动扩展了 Activity 而不是 ListActivity。我可以滚动列表项目,但无法选择任何项目(至少通过触摸)。我通过 findViewBYId 访问列表视图,并有一个扩展 SimpleAdapter 的适配器。我已将适配器的 getVIew() 内的 ConvertView 设置为可点击。但是,当我这样做时,我无法再单击 listView 中的列表项?我做错了吗?

public class App extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        jobsListView = (ListView) findViewById(R.id.jobs_list);
        jobsListView.setAdapter(new JobItemAdapter(jobsListView.getContext(), 
                jobsList, 
                R.layout.list_item, 
                new String[] {"title","location"},
                new int[] {R.id.job_title, R.id.job_location}));
        jobsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Log.i("jobsListView.setOnItemClickListener()"," item clicked: " + arg0.getSelectedItemPosition());
            }

        });

    }

    public class JobItemAdapter extends SimpleAdapter {
        private List<HashMap<String,String>> jobsList;

        public JobItemAdapter(Context context, List<HashMap<String,String>> jobs, int resource, String[] from, int[] to)
        {   
            super(context, jobs, resource, from, to);
            jobsList = jobs;            
        }

        public View getView(int position, View convertView, ViewGroup parent){
            Log.i(TAG," in  JobItemAdapter getView() ");

            if (convertView == null) {
                //no previous views, inflate views
                convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
            }

            //update textviews in layout
            TextView title = (TextView)convertView.findViewById(R.id.job_title);
            title.setText(jobsList.get(position).get("title"));

            TextView location = (TextView)convertView.findViewById(R.id.job_location);
            location.setText(jobsList.get(position).get("location"));
            //make the view clickable
            convertView.setClickable(true);
            return convertView;
        }

    }

}

I have an app that hits a REST service, gets some json and then places it into a listview. My main activity extends Activity and not ListActivity. I can scroll through the list items, but I cannot select any (at least by touch). I get to the listview through findViewBYId and have an adapter that extends SimpleAdapter. I have set the convertView inside the adapter's getVIew() as clickable. However when I do this, I can no longer click on the list items in the listView? Did I do this wrong?

public class App extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        jobsListView = (ListView) findViewById(R.id.jobs_list);
        jobsListView.setAdapter(new JobItemAdapter(jobsListView.getContext(), 
                jobsList, 
                R.layout.list_item, 
                new String[] {"title","location"},
                new int[] {R.id.job_title, R.id.job_location}));
        jobsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Log.i("jobsListView.setOnItemClickListener()"," item clicked: " + arg0.getSelectedItemPosition());
            }

        });

    }

    public class JobItemAdapter extends SimpleAdapter {
        private List<HashMap<String,String>> jobsList;

        public JobItemAdapter(Context context, List<HashMap<String,String>> jobs, int resource, String[] from, int[] to)
        {   
            super(context, jobs, resource, from, to);
            jobsList = jobs;            
        }

        public View getView(int position, View convertView, ViewGroup parent){
            Log.i(TAG," in  JobItemAdapter getView() ");

            if (convertView == null) {
                //no previous views, inflate views
                convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
            }

            //update textviews in layout
            TextView title = (TextView)convertView.findViewById(R.id.job_title);
            title.setText(jobsList.get(position).get("title"));

            TextView location = (TextView)convertView.findViewById(R.id.job_location);
            location.setText(jobsList.get(position).get("location"));
            //make the view clickable
            convertView.setClickable(true);
            return convertView;
        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2024-10-23 19:29:30

可能发生的情况是,您正在为 R.layout.list_item 中定义的行定义背景颜色,并且该背景颜色不是选择器中定义的颜色,例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_press"/> 
        </shape> 
    </item> 
    <item android:state_selected="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item android:state_focused="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item> 
        <shape android:shape="rectangle" >
            <solid
                android:color="@color/white" /> 
        </shape> 
    </item> 
</selector>

在其中,您可以定义以下颜色:视图的每种状态,在本例中:聚焦和按下。
为了使用它,在 R.layout.list_item 中,您可以正常设置背景,例如

android:background="@drawable/list_row_background"

考虑到上述 xml 文件名为 list_row_background 且位于可绘制文件夹中

,并且您不应该将其设置为可点击,因为它已经可以点击了。
我希望能解决你的问题:D
干杯

Probably what can be happening is that you are defining a background color for the row, defined in R.layout.list_item, and that background color is not a color defined in a selector, for example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_press"/> 
        </shape> 
    </item> 
    <item android:state_selected="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item android:state_focused="true" > 
        <shape android:shape="rectangle"> 
            <solid
                android:color="@color/blue_button_focus" /> 
        </shape> 
    </item> 
    <item> 
        <shape android:shape="rectangle" >
            <solid
                android:color="@color/white" /> 
        </shape> 
    </item> 
</selector>

In it, you can define the color for each state of the View, in this case: focused and pressed.
In order to use it, in R.layout.list_item, you set the background normaly, for example

android:background="@drawable/list_row_background"

considering that the xml file file described aboved is named list_row_background and its in the drawable folder

And you shouldn't set it as clickable, since it is already clickable.
I hope that solves your problem :D
Cheers

£冰雨忧蓝° 2024-10-23 19:29:30

一种解决方案是将您的 Activity 更改为 ListActivity。

当用户触摸列表项时,将调用此覆盖:

@Override 受保护无效
onListItemClick(ListView l, View v,
int 位置,长 id)

如果您想更改它,则 v 是单元格。
该位置是适配器的索引。

One solution is to change your Activity to a ListActivity.

When the user touches a list item this override is called:

@Override protected void
onListItemClick(ListView l, View v,
int position, long id)

the v is the cell if you want to alter it.
The position is the index into adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文