Java 获取列表视图的位置项时出错 listContent.setOnItemClickListener(新的AdapterView.OnItemClickListener() { @凌驾 public void onItemClick(AdapterView父对象、视图、整型位置、长id){ 对于(int i=0;i

Java 获取列表视图的位置项时出错 listContent.setOnItemClickListener(新的AdapterView.OnItemClickListener() { @凌驾 public void onItemClick(AdapterView父对象、视图、整型位置、长id){ 对于(int i=0;i,java,android,listview,Java,Android,Listview,您的问题确实是关于,但很难确定为什么会发生这种情况。问题可以在这里找到: listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { for(i

您的问题确实是关于,但很难确定为什么会发生这种情况。问题可以在这里找到:

listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            for(int i=0;i<showLists.size();i++){
              //
                TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes);
                v.setTextColor(Color.BLACK);

            }

            TextView v=(TextView)listContent.getChildAt(position).findViewById(R.id.txtDes);
            v.setTextColor(Color.RED);
            Toast.makeText(context,"POS "+showLists.get(position).getDes(),Toast.LENGTH_SHORT).show();

        }
    });
listContent.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
对于(int i=0;i
//下面是创建customListMoiNhat对象 最终customListMoiNhat customListMoiNhat=新customListMoiNhat(上下文,R.layout.moinhat\u customlistview,ShowList,currentpos); customListMoiNhat.notifyDataSetChanged(); setAdapter(customListMoiNhat)

listContent.setOnItemClickListener(新的AdapterView.OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
当前位置=位置;
customListMoiNhat.notifyDataSetChanged();
}
});

此项的可能重复项仍将崩溃。谢谢,我知道列表视图具有recyling机制。当我单击列表视图上的某个项目时(30个项目)示例项的索引为0,嗯,甚至在该标记下有两个项也是红色的。您能修复它吗?对不起,我这么晚才读到这条评论。谢谢,我现在就试试。没问题。如果需要帮助,请告诉我。我试过这段代码,但所有项listview都设置为红色或我的代码都是错误的。@@我刚刚发布了新的cmt,请再次检查:D
for(int i=0;i<showLists.size();i++){ <-- this line actually causes the crash
//
    the crash is in the next line, at the findViewById
    TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes); 
    v.setTextColor(Color.BLACK);
}
int currentPosition = -1;

// Just a basic adapter. The getView method is the key here
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) {
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            if (position == currentPosition) {
                ((TextView) view).setTextColor(Color.RED);
            } else {
                ((TextView) view).setTextColor(Color.BLACK);
            }
            return  view;
        }
    };

// and here is the onItemClick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            currentPosition = position;
            adapter.notifyDataSetChanged();
        }
    });
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            for (int i = 0; i < showLists.size(); i++) {

                TextView v = (TextView) view.findViewById(R.id.txtDes);
                v.setTextColor(Color.BLACK);

            }

            TextView v = (TextView) view.findViewById(R.id.txtDes);
            v.setTextColor(Color.RED);
            Toast.makeText(context, "POS " + showLists.get(position).getDes(), Toast.LENGTH_SHORT).show();

        }
    });
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewRow=convertView;
    if(viewRow==null){
        viewRow=inflater.inflate(layout,parent,false);
        viewHolder viewHolder=new viewHolder();
        viewHolder.imgIcon    = (ImageView) viewRow.findViewById(R.id.imgIcon);
        viewHolder.txtDes     = (TextView) viewRow.findViewById(R.id.txtDes);

        viewRow.setTag(viewHolder);
    }

    viewHolder  holder= (viewHolder) viewRow.getTag();
    holder.imgIcon.setImageResource(listMoiNhat.get(position).getIcon());
    holder.txtDes.setText(listMoiNhat.get(position).getDes());
    if(position==currentpos){
        holder.txtDes.setTextColor(Color.RED);
    }
    else {
        holder.txtDes.setTextColor(Color.RED);

    }
    return viewRow;
}
    listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



            currentpos=position;
            customListMoiNhat.notifyDataSetChanged();


        }
    });