私は最初の 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" />