1

私は最初の Android アプリに取り組んでおり、既にここに投稿されている多くの回答を確認した後、理解は深まりましたが、問題を解決できないようです。

簡単に言えば、私は を持っていてListView、特定の行のテキストを他の行とは異なるものにしたいと考えています。ListView.GetItemAtPositionをaにキャストし、そのテキストの色を変更することでこれを実行しようとしていTextViewますが、キャスト例外が発生しています。

コードのエラーを理解するのを手伝ってくれるか、より良い方法を提案していただければ幸いです。

public class MeetingManager extends Activity {

public ListView mAgenda;

//public Item agenda = new Item();
List<Item> agenda=new ArrayList<Item>();
ArrayAdapter<Item> adapter=null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.meeting);

    // Obtain handles to UI objects
    mAgenda = (ListView)findViewById(R.id.lstAgenda);

    adapter=new ArrayAdapter<Item>(this, R.layout.agendalist, agenda);
    mAgenda.setAdapter(adapter);        
    mAgenda.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    //Load items into agenda
    initAgenda();
}

protected void updateDetails() {
    mRecipients.setText("This message will be sent to items " + (currentItemNum + 1) + " and later:");
    mSMS.setText(currentItem.getSMS());
}

protected void initAgenda() {
    currentItem = new Item();
    currentItem.setTitle("Reset Meeting");
    adapter.add(currentItem);
    setColour(0, Color.RED);
}

public void setColour(int pos, int col) {
    TextView tv = (TextView)mAgenda.getItemAtPosition(pos); //This is where the exception is thrown
    tv.setTextColor(col);
}
}

以下は、ListView

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"  
    android:paddingTop="2dip" 
    android:paddingBottom="3dip"
    android:textSize="12pt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
4

2 に答える 2

2

カスタムアダプタを作成し、色付けの基準に応じてメソッドTextView.setTextColor();で使用する必要があります。getView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = View.inflate(context, layout, null);
    View row = convertView;

    TextView first = (TextView) convertView.findViewById(R.id.ListItem1);
    TextView second = (TextView) convertView.findViewById(R.id.ListItem2);
    if(condition == changecolor) {
        first.setTextColor(#FFFF0000);
        second.setTextColor(#FFFF0000);
    }
}
于 2012-06-16T06:25:27.810 に答える
0

サブクラス化できArrayAdapter、サブクラスの getView メソッド内で魔法を実行できます

public View getView(int position, View convertView, ViewGroup parent) {
  TextView tv = super.getView(position, convertView, parent);

  if (position < 3) {     // I have just put dummy condition you can put your condition
    textView.setTextColor(colors);  // Here you can put your color
  } else {
    textView.setTextColor(colors);
  }
}
于 2012-06-16T06:29:02.680 に答える