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();
}
});
我一直是listview的get position项的问题。当我点击listview上的另一个项目(我的列表视图中有30个项目)显示错误时,Android刚刚在devide的creen中显示了大约12行。 这是错误: &#34;尝试调用虚拟方法&#39; android.view.View android.view.View.findViewById(int)&#39;在null对象引用&#34; 。 感谢您的阅读。
答案 0 :(得分:0)
你的问题确实是关于Null Pointer Exception,但更难确定为什么会发生这种情况。问题可以在这里找到:
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);
}
由于recycling机制,您的for
上限错误,并且由于此机制,您的列表视图永远不会具有相同的行数需要显示的数据量(阅读有关回收的内容以了解这一点)。鉴于此事实,我们确信列表视图(listContent.getChildCount()
)中的观看次数将小于showLists.size()
,从而使调用listContent.getChildAt(i)
返回NULL
当索引等于创建崩溃的listContent.getChildCount()
时的值。
现在您可能想用showLists.size()
更改listContent.getChildCount()
并看到应用程序不再崩溃,但是如果您单击一行,那么当您滚动时其他行也会被着色清单(再循环再次成为问题)。要真正解决问题,您应该保存所选行的索引并调用notifyDatasetChanged
,因此在适配器中调用getView
时,只需检查当前位置以显示所选位置。如果相等,则将文本颜色更改为红色,否则更改为黑色。下面,您将找到示例的某些部分:
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();
}
});
答案 1 :(得分:0)
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();
}
});
答案 2 :(得分:0)
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;
}
//这里是创建customListMoiNhat对象 final customListMoiNhat customListMoiNhat = new customListMoiNhat(context,R.layout.moinhat_customlistview,showLists,currentpos); customListMoiNhat.notifyDataSetChanged(); listContent.setAdapter(customListMoiNhat);
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentpos=position;
customListMoiNhat.notifyDataSetChanged();
}
});