580
No
class MainActivity
핵심 코드
String item =String.valueOf(parent.getItemAtPosition(position)); |
getItemAtPosition 으로 아이템 이름 가지고 오기
package kr.co.dothome.braverokmc.tutorial5;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.listView);
String[] items ={"망고 쥬스", "토마토 쥬스", "포도 쥬스"};
// ListAdapter listAdapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListAdapter listAdapter =new ImageAdapter(this, items);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item =String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
class ImageAdapter
핵심코드
생성자
super()
super(context, R.layout.image_layout, items); |
다음과 같이 컨텍스트 객체를 쉽게 가져올 수 있다.
LayoutInflater inflater = LayoutInflater.from(getContext()); |
package kr.co.dothome.braverokmc.tutorial5;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by choi on 2017-04-25.
*/
public class ImageAdapter extends ArrayAdapter<String>{
ImageAdapter(Context context, String[] items){
super(context, R.layout.image_layout, items);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater =LayoutInflater.from(getContext());
View view =inflater.inflate(R.layout.image_layout, parent, false);
String item=getItem(position);
TextView textView1 =(TextView)view.findViewById(R.id.textView1);
ImageView imageView1=(ImageView)view.findViewById(R.id.imageView1);
textView1.setText(item);
imageView1.setImageResource(R.mipmap.a8);
return view;
}
}
R.layout.image_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_weight="1"
app:srcCompat="@drawable/a8"
tools:ignore="ContentDescription"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="2">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="2"
android:text="TextView"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
댓글 ( 4)
댓글 남기기