我在listview中使用Base Adapter来填充android中的数据。但我只是在android的初学者。所以,在基础适配器中有点混乱。我在基础适配器中显示数据。
这里,我的主要问题是,如果我点击位置0,那么我想改变位置的txtview的颜色。如果我点击位置5然后改变位置5的textview的颜色,其他颜色保持相同的颜色。
只需将可点击的位置texview更改为红色其他白色。
if 0 - red - clicked
1-white
2 - white
3-white
4- white
5- white
if 0 - white
1-white
2 - white
3- red- clicked
4- white
5- white
我的代码:
ArrayList<Geo> list;
public class TestListAdapter extends BaseAdapter {
public TestListAdapter(Context context, int textViewResourceId,
ArrayList<Geo> objects, String name_of_APP) {
this.objects = objects;
contx = context;
layoutInflator = LayoutInflater.from(contx);
_APP = name_of_APP;
VVV = layoutInflator.inflate(R.layout.row_cell_multilevel, null);
list=objects;
for(int i=0;i<list.size();i++){
itemPos.add(i);
}
}
// @Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
try{
CC = getItem(position);//objects.get(position);
if (convertView == null){
convertView = View.inflate(contx, R.layout.row_cell_multilevel, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.row_cell_text_multilevel);
holder.btn = (Button) convertView.findViewById(R.id.row_cell_btn_multilevel);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.btn.setTag(position);
holder.txtName.setTag(position);
holder.txtName.setText(CC.Name);
holder.txtName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = holder.txtName.getText().toString();
Toast.makeText(contx, "You Have Choosed " + name , Toast.LENGTH_SHORT).show();
if (itemPos.contains(position)) {
holder.txtName.setTextColor(Color.RED); notifyDataSetChanged();}
else {
holder.txtName.setTextColor(Color.WHITE); notifyDataSetChanged();}
}
});
}catch (Exception e){
Log.d("Exception", "" + e.getMessage());
}
return convertView;
}
static class ViewHolder {
TextView txtName;
}
//@Override
public int getCount() {
// TODO Auto-generated method stub
return this.objects == null ? 0 : this.objects.size();
}
//@Override
public Geo getItem(int position) {
// TODO Auto-generated method stub
return this.objects == null ? null : this.objects.get(position);
}
// @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public String getID() {
Log.e(post, "post id=" + CC.ID);
return CC.ID;
}
}
答案 0 :(得分:4)
不要处理适配器getView()内的点击。
在“片段/活动”中,将项目单击侦听器添加到列表视图并更改文本颜色。
这是一个简单的示例代码..
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
TextView previousView = null; // to hold the previous clicked view
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)view.findViewById(R.id.row_cell_text_multilevel);
if(previousView != null) {
// revert the previous view when a new item is clicked
previousView.setTextColor(Color.WHITE);
}
textView.setTextColor(Color.RED);
previousView = textView;
}
});
答案 1 :(得分:1)
在ListView上设置setOnItemClickListener并覆盖其中的onItemClick方法
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
//apply switch case here for different position
switch(position){
case 0:
your_textView.setTextColor(your color code);
}
}
});