초간단 리스트뷰 (listview - 로컬 이미지, 텍스트)
* 설명부분 생략 후 코딩 부분만 스크랩
* 이미지는 4번 항목을 통해서 다운로드 할것
* 터치 이벤트를 사용하고 싶을때는 아래 url 참조 (추가된 포스팅) > 레이아웃 변경했다면 LinearLayout 깊이를 나타내는 부분 주의
http://wptrafficanalyzer.in/blog/android-itemclicklistener-for-a-listview-with-images-and-text/
4. Download and extract the zip file containing flag’s images to the directory /res/drawable-ldpi
5. Open and update the file /res/layout/main.xml with the given below code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<TextView android:id="@+id/textview" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> |
6. Create a new file namely /res/layout/listview_layout.xml and update the content with the given below code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/flag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello" android:paddingTop="10dp" android:paddingRight="10dp" android:paddingBottom="10dp" />
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" />
<TextView android:id="@+id/cur" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10dp" /> </LinearLayout> </LinearLayout> |
7. Open and update the file /src/in/wptrafficanalyzer/listviewwithimagesandtext/MainActivity.java with the given below code
package in.wptrafficanalyzer.listviewwithimagesandtext;
import java.util.ArrayList; import java.util.HashMap; import java.util.List;
import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// Array of strings storing country names String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" };
// Array of integers points to images stored in /res/drawable-ldpi/ int[] flags = new int[]{ R.drawable.india, R.drawable.pakistan, R.drawable.srilanka, R.drawable.china, R.drawable.bangladesh, R.drawable.nepal, R.drawable.afghanistan, R.drawable.nkorea, R.drawable.skorea, R.drawable.japan };
// Array of strings to store currencies String[] currency = new String[]{ "Indian Rupee", "Pakistani Rupee", "Sri Lankan Rupee", "Renminbi", "Bangladeshi Taka", "Nepalese Rupee", "Afghani", "North Korean Won", "South Korean Won", "Japanese Yen" };
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
// Each row in the list stores country name, currency and flag List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){ HashMap<String, String> hm = new HashMap<String,String>(); hm.put("txt", "Country : " + countries[i]); hm.put("cur","Currency : " + currency[i]); hm.put("flag", Integer.toString(flags[i]) ); aList.add(hm); }
// Keys used in Hashmap String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items // R.layout.listview_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView listView.setAdapter(adapter); } } |
8. Run the application
9. Download the source code
ListView with Images and Text 187.83 KB
[출처] http://wptrafficanalyzer.in/blog/listview-with-images-and-text-using-simple-adapter-in-android/
'프로그래밍 > Android (Java)' 카테고리의 다른 글
[탭버튼] 프래그먼트 (Fragment) 를 이용한 탭 버튼 구현 예제 (슬라이딩 아님) (0) | 2019.08.23 |
---|---|
안드로이드 기본아이콘 사용하기 (0) | 2019.08.22 |
Custom Dialog 만들기 (0) | 2019.08.19 |
나침반 만들기 (0) | 2019.08.07 |
어플 모두 로딩시까지 로딩화면 보여주기 (Intro Screen) (0) | 2019.08.05 |