증상 : adapter.addItem으로 추가한 이미지만 화면에 정상적으로 보이지 않습니다. (텍스트는 정상표시는데 이미지는 빈 공간으로 보여요)
singer_item.xml에서 ImageView태그 안에 src만 적용한 상태에서는 이미지가 잘 보입니다.
강좌를 여러번 봐도 틀린 부분이 잘 안보이는데 몇 일째 해결이 안되네요. 도와주세요!
아래 코드 첨부할게요.
<<MainActivity.java 파일>>
package org.techtown.list;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
SingerAdapter adapter;
EditText editText;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new SingerAdapter();
for(int i=0; i<10000; i++) {
adapter.addItem(new SingerItem("소녀시대" + i, "010-1000-" + i, R.drawable.kakao));
}
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SingerItem item = (SingerItem) adapter.getItem(position);
Toast.makeText(getApplicationContext(), "선택 : " + item.getName(), Toast.LENGTH_LONG).show();
}
});
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText.getText().toString();
String mobile = editText2.getText().toString();
adapter.addItem(new SingerItem(name, mobile, R.drawable.kakao));
adapter.notifyDataSetChanged();
}
});
}
class SingerAdapter extends BaseAdapter {
ArrayList<SingerItem> items = new ArrayList<SingerItem>();
@Override
public int getCount() {
return items.size();
}
public void addItem(SingerItem item) {
items.add(item);
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SingerItemView view = null;
if( convertView == null) {
view = new SingerItemView(getApplicationContext());
} else {
view = (SingerItemView) convertView;
}
SingerItem item = items.get(position);
view.setName(item.getName());
view.setMobile(item.getMobile());
view.setImage(item.getResId());
return view;
}
}
}
<<singer_item.xml 파일>>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름"
android:textSize="30dp"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="전화번호"
android:textSize="24dp"
android:textColor="@android:color/holo_orange_dark"/>
</LinearLayout>
</LinearLayout>
<SingerItemView.java 파일>>
package org.techtown.list;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SingerItemView extends LinearLayout {
TextView textView;
TextView textView2;
ImageView imageView;
public SingerItemView(Context context) {
super(context);
init(context);
}
public SingerItemView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.singer_item, this, true);
textView = (TextView) findViewById(R.id.textView);
textView2 = (TextView) findViewById(R.id.textView2);
imageView = (ImageView) findViewById(R.id.imageView);
}
public void setName(String name) {
textView.setText(name);
}
public void setMobile(String mobile) {
textView2.setText(mobile);
}
public void setImage(int resId) {
imageView.setImageResource(resId);
}
}
<SingerItem.java 파일>>
package org.techtown.list;
public class SingerItem {
String name;
String mobile;
int resId;
public SingerItem(String name, String mobile, int resId) {
this.name = name;
this.mobile = mobile;
this.resId = resId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public int getResId() {
return resId;
}
public void setResId(int resId) {
this.resId = resId;
}
}
리스트뷰 코드는 서로 연관되어 있어 모두 올려봅니다. 부탁드려요.
comment