ArrayAdapter를 사용하는 방법
ArrayList<MyClass> myList = new ArrayList<MyClass>();
ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this, R.layout.row,
to, myList.);
listView.setAdapter(adapter);
수업 : MyClass
class MyClass {
public String reason;
public long long_val;
}
레이아웃에서 row.xml을 만들었지 만 ArrayAdapter를 사용하여 ListView에서 reason과 long_val을 모두 표시하는 방법을 모르겠습니다.
클래스에 대한 사용자 정의 어댑터를 구현하십시오.
public class MyClassAdapter extends ArrayAdapter<MyClass> {
private static class ViewHolder {
private TextView itemView;
}
public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) {
super(context, textViewResourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.listview_association, parent, false);
viewHolder = new ViewHolder();
viewHolder.itemView = (TextView) convertView.findViewById(R.id.ItemView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
MyClass item = getItem(position);
if (item!= null) {
// My layout has only one TextView
// do whatever you want with your string and long
viewHolder.itemView.setText(String.format("%s %d", item.reason, item.long_val));
}
return convertView;
}
}
안드로이드 프레임 워크에 익숙하지 않은 사람들을 위해 https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 에서 자세히 설명합니다 .
http://developer.android.com/reference/android/widget/ArrayAdapter.htmltoString()
에 따라 MyClass에 메소드를 추가 할 수 있습니다 .
그러나 TextView는 참조되며 배열에있는 각 객체의 toString ()으로 채워집니다. 사용자 정의 객체의 목록 또는 배열을 추가 할 수 있습니다. 객체의 toString () 메서드를 재정 의하여 목록의 항목에 표시 할 텍스트를 결정합니다.
class MyClass {
@Override
public String toString() {
return "Hello, world.";
}
}
I think this is the best approach. Using generic ArrayAdapter class and extends your own Object adapter is as simple as follows:
public abstract class GenericArrayAdapter<T> extends ArrayAdapter<T> {
// Vars
private LayoutInflater mInflater;
public GenericArrayAdapter(Context context, ArrayList<T> objects) {
super(context, 0, objects);
init(context);
}
// Headers
public abstract void drawText(TextView textView, T object);
private void init(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
drawText(vh.textView, getItem(position));
return convertView;
}
static class ViewHolder {
TextView textView;
private ViewHolder(View rootView) {
textView = (TextView) rootView.findViewById(android.R.id.text1);
}
}
}
and here your adapter (example):
public class SizeArrayAdapter extends GenericArrayAdapter<Size> {
public SizeArrayAdapter(Context context, ArrayList<Size> objects) {
super(context, objects);
}
@Override public void drawText(TextView textView, Size object) {
textView.setText(object.getName());
}
}
and finally, how to initialize it:
ArrayList<Size> sizes = getArguments().getParcelableArrayList(Constants.ARG_PRODUCT_SIZES);
SizeArrayAdapter sizeArrayAdapter = new SizeArrayAdapter(getActivity(), sizes);
listView.setAdapter(sizeArrayAdapter);
I've created a Gist with TextView layout gravity customizable ArrayAdapter:
https://gist.github.com/m3n0R/8822803
Subclass the ArrayAdapter and override the method getView() to return your own view that contains the contents that you want to display.
Here's a quick and dirty example of how to use an ArrayAdapter if you don't want to bother yourself with extending the mother class:
class MyClass extends Activity {
private ArrayAdapter<String> mAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
mAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, android.R.id.text1);
final ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(mAdapter);
//Add Some Items in your list:
for (int i = 1; i <= 10; i++) {
mAdapter.add("Item " + i);
}
// And if you want selection feedback:
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do whatever you want with the selected item
Log.d(TAG, mAdapter.getItem(position) + " has been selected!");
}
});
}
}
참고URL : https://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass
'programing tip' 카테고리의 다른 글
TypeScript 주석의 구문은 어디에 기록되어 있습니까? (0) | 2020.07.03 |
---|---|
.NET에서 매핑 및 축소 (0) | 2020.07.03 |
배열에서 첫 x 항목 반환 (0) | 2020.07.03 |
나열 할 Pandas DataFrame 열 (0) | 2020.07.03 |
JQuery없이 리스너에 여러 이벤트를 바인딩 하시겠습니까? (0) | 2020.07.03 |