gpt4 book ai didi

java - 创建时突出显示 ListView 的第一项

转载 作者:行者123 更新时间:2023-12-01 10:05:18 25 4
gpt4 key购买 nike

我能够更改 ListView 中所选项目的背景。我希望在创建列表时突出显示第一个项目的背景。当我尝试执行“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
}
}

最佳答案

在 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,然后返回所需的项目。

关于java - 创建时突出显示 ListView 的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com