如何更改textview的自定义listview上点击-Android

问题描述:

我做了一个android应用程序定制listview与复选框&文本每行。现在我想改变文本颜色上单击文本或复选框,如果任何。如何我可以做这个?如何更改textview的自定义listview上点击-Android

我的代码:

String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", 
     "Jupiter", "Saturn", "Uranus", "Neptune" }; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LSOne = (ListView) findViewById(R.id.listView1); 
    planetList.addAll(Arrays.asList(planets)); 
    // Create ArrayAdapter using the planet list. 
    LsAdapter listAdapter = new LsAdapter(this, R.layout.country_info, 
      planetList); 
    LSOne.setAdapter(listAdapter); 
    LSOne.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View arg1, 
       int position, long id) {   
     } 
    }); 
} 
public class LsAdapter extends ArrayAdapter<String> { 
    private LayoutInflater mInflater; 
    private String[] mTaxi = null; 
    private String[] mid = null; 
    long id; 
    public static final boolean isEnabled = true; 
    private int mViewResourceId; 
    public LsAdapter(Context ctx, int viewResourceId, 
      ArrayList<String> planetList) { 
     super(ctx, viewResourceId); 
     mInflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     String[] tax = planetList.toArray(new String[planetList.size()]); 
     mTaxi = tax; 
     mViewResourceId = viewResourceId; 
    } 
    @Override 
    public int getCount() { 
     return mTaxi.length; 
    } 
    @Override 
    public String getItem(int position) { 
     return mTaxi[position]; 
    } 
    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 
    @Override 
    public int getViewTypeCount() { 
     // TODO Auto-generated method stub 
     return 20; 
    } 
    @Override 
    public int getItemViewType(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     Log.v("ConvertView", String.valueOf(position)); 
     int _intPosition = getItemViewType(position); 
     if (convertView == null) { 
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = vi.inflate(R.layout.country_info, null); 
    holder = new ViewHolder(); 
    holder.code = (TextView) convertView.findViewById(R.id.textView1); 
    holder.name = (RadioButton) convertView.findViewById(R.id.radioButton1); 
    convertView.setTag(holder); 
    holder.code.setText(mTaxi[position]); 
    holder.name.setId(_intPosition); 
    if (flag == 1) { 
    holder.name.setEnabled(false); 
    } else if (flag == 0) { 
    holder.name.setEnabled(true); 
} 
    holder.name.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      for (int i = 0; i < _intRadio.length; i++) { 
      if (i == v.getId()) { 
      _intRadio[i] = 1; 
     } else { 
      _intRadio[i] = 0; 
     } 
    } 
      holder.code.setTextColor(Color.parseColor("#008000")); 
    notifyDataSetChanged(); 
    } 
}); 
holder.code.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     holder.code.setTextColor(Color.parseColor("#008000")); 
     // v.setBackgroundColor(Color.BLUE); 
     notifyDataSetChanged(); 
       } 
      }); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     if (_intRadio[_intPosition] == 1) { 
      holder.name.setChecked(true); 
     } else { 
      holder.name.setChecked(false); 
     } 
     return convertView; 
    } 
    private class ViewHolder { 
     TextView code; 
     RadioButton name; 
     Button btn; 
    } 
}} 

感谢。

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     if (row == null) { 
      // ROW INFLATION 
      Log.d("ExamAdapter", "Starting XML Row Inflation ... "); 

      LayoutInflater inflater = (LayoutInflater) this.getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.custom_gold, parent, false); 
      Log.d("ExamAdapter", "Successfully completed XML Row Inflation!"); 
     } 

     // Get item 
     Gold text = getItem(position); 
     subject = (TextView) row.findViewById(R.id.textView1); 

     time = (TextView) row.findViewById(R.id.textView2); 

     String content = text.content; 

     String times = text.time; 

     subject.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       subject.setBackgroundColor(R.color.red_color); 
      } 
     }); 



     /* String[] com = content.split(Pattern.quote("*")); */ 

     subject.setText(content.replace("*", "\n")); 

     return row; 
    } 

尝试这个代码..

subject.setBackgroundColor(R.color.red_color);这条线内的onClick监听器...

+0

Nope.Its不改变向上 – sanjay 2013-05-07 10:12:57

在你的代码之前notifyDataSetChanged();,得到的位置并将其存储在一个静态变量。在return convertView;之前用静态变量检查位置并尝试holder.name.setBackgroundColor(<color>)。我认为它会工作。

里面你的onClick监听器...尝试这样

 holder.name.setTextColor(Color.parseColor("#008000")); 

希望这将工作