티스토리 뷰
// User.kt (데이터 클래스)
data class User(
val id: Int,
val name: String,
val location: String
)
<!-- res/layout/user_list_item.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<ImageView
android:id="@+id/user_image"
android:layout_width="50dp"
android:src="@mipmap/ic_launcher"
android:layout_height="50dp" />
<TextView
android:id="@+id/user_name_text"
android:layout_width="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_height="match_parent" />
</LinearLayout>
// UserAdapter.kt (Adapter 클래스)
class UserAdapter(
private val mContext: Context,
private val mList: MutableList<User>
) : RecyclerView.Adapter<UserAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.user_list_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user = mList[position]
holder.bind(user)
}
override fun getItemCount(): Int = mList.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(user: User) {
itemView.findViewById<TextView>(R.id.user_name_text).text = user.name
if (user.location != "기존의 장소명") {
// 장소가 다른 경우 파란색으로 표시
itemView.findViewById<TextView>(R.id.user_name_text).setTextColor(Color.BLUE)
}
}
}
}
// MainActivity.kt (안드로이드 메인 코드)
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var userAdapter: UserAdapter
private val userList = mutableListOf<User>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// RecyclerView 초기화
recyclerView = findViewById(R.id.recyclerView)
userAdapter = UserAdapter(this, userList)
recyclerView.adapter = userAdapter
recyclerView.layoutManager = LinearLayoutManager(this)
// 데이터 추가 (예시)
userList.add(User(1, "Alice", "New York"))
userList.add(User(2, "Bob", "Los Angeles"))
userList.add(User(3, "Charlie", "Chicago"))
// 데이터 변경 알림
userAdapter.notifyDataSetChanged()
}
}
반응형
'SWDesk > App' 카테고리의 다른 글
[Android] Fragment 개념 (1) | 2024.04.08 |
---|---|
[Android] 그래프 예제 (2) | 2024.04.06 |
[Android] Bilient Navigator 출시 (0) | 2024.01.10 |
[Android] WebView 예제 소스코드 (0) | 2024.01.05 |
명함 이미지에서 명함 데이터를 추출하는 앱 (1) | 2023.03.18 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 절연형
- 전압
- 전압전류모니터링
- badp
- bilient
- image
- 아두이노
- ServantClock
- 혁신
- 전류
- Hurdles
- Innovations&Hurdles
- Innovation&Hurdles
- 티스토리챌린지
- Innovations
- Video
- 치매
- 둎
- 오블완
- 혁신과허들
- arduino
- 빌리언트
- BSC
- 심심풀이
- 심심풀이치매방지기
- Decorator
- 배프
- 치매방지
- 허들
- DYOV
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함