创建时突出显示列表视图的第一项

时间:2016-04-09 21:23:57

标签: java android listview highlight

我可以更改列表视图中所选项目的背景。我希望在创建列表时突出显示第一个项目的背景。当我尝试执行" performItemClick"。

时,下面的代码会出错

我已经看到了修改数组适配器中的getView的建议。但是,当我尝试在我的java代码中创建类MyCustomAdapter时,我得到错误" Android.widget.ArrayAdapter中没有默认构造。

因此,如果我需要创建一个我的ArrayAdapter类的扩展来执行此操作,我该如何解决此错误。如果还有其他方式,我愿意接受建议。

由于

package com.hofficer.aclsfast;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.lang.reflect.Array;

public class NarrowChild extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_narrow_child);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

        //fix orientation

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //MyCustomAdapter adapter = new MyCustomAdapter();
        createList();
    }
    /*
    class MyCustomAdapter extends ArrayAdapter{

    }*/

    void createList(){
        String[] narrowChoices = {"aFib/Flutter", "Narrow Cmplx Tachycardia", "PSVT", "Junctional Tachycardia", "Multifocal Tachycardia"};

        ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, narrowChoices);
        final ListView listview = (ListView) findViewById(R.id.narrowListView);

        listview.setAdapter(theAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //clear menu and highlight selected item
                for (int i = 0; i < 5; i++) {
                    listview.getChildAt(i).setBackgroundColor(Color.WHITE);
                }
                listview.getChildAt(position).setBackgroundColor(Color.CYAN);
            }
        });
        Boolean result = listview.performItemClick(listview,0,0);   //THIS GIVES ERROR NULL POINT EXCEPTION
    }
}

3 个答案:

答案 0 :(得分:1)

在MyCustomAdapter中,您必须覆盖ArrayAdapter的一个或多个构造函数或创建自己的构造函数。自定义适配器的示例是:

    public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context;
    int layoutResourceId;   
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

来自source

的更多信息

在你的情况下,你需要检查位置是否为0,然后返回所需的项目。

答案 1 :(得分:0)

我现在无法测试,但你不能只拨打listview.getChildAt(0).setBackgroundColor(Color.CYAN);而不是Boolean result = listview.performItemClick(listview,0,0);吗?

答案 2 :(得分:0)

试试这个:

Boolean result = listview.performItemClick(listview.getAdapter().getView(0),0,0);

或:

Boolean result = listview.performItemClick(listview.getAdapter().getView(0, null, null),0,0);

请参阅此question并查看docs

修改

我认为您的问题是getChildAt()方法。你应该看看this问题。