Android的编程教程 - 教程5 [英] Android Programming tutorials - Tutorial 5
问题描述
您好,我从做书的Android编程教程教程,我有理解教程5.这是主要的Java类的问题:
Hello, I am doing tutorials from book "Android programming tutorials", I am having problems understanding Tutorial 5. This is the main Java class:
package tiago.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class Now extends Activity {
List<Restaurant> model = new ArrayList<Restaurant>();
RestaurantAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView) findViewById(R.id.restaurants);
adapter = new RestaurantAdapter();
list.setAdapter(adapter);
}
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
Restaurant r = new Restaurant();
EditText name = (EditText) findViewById(R.id.name);
EditText address = (EditText) findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types = (RadioGroup) findViewById(R.id.types);
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
r.setType("sit_down");
break;
case R.id.take_out:
r.setType("take_out");
break;
case R.id.delivery:
r.setType("delivery");
break;
}
adapter.add(r);
}
};
class RestaurantAdapter extends ArrayAdapter<Restaurant> {
RestaurantAdapter() {
super(Now.this, R.layout.row, model);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RestaurantHolder holder = null;
if (row==null) {
LayoutInflater inflater = getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
holder = new RestaurantHolder(row);
row.setTag(holder);
}
else {
holder = (RestaurantHolder) row.getTag();
}
holder.populateFrom(model.get(position));
return(row);
}
}
static class RestaurantHolder {
private TextView name = null;
private TextView address = null;
private ImageView icon = null;
RestaurantHolder(View row) {
name = (TextView) row.findViewById(R.id.title);
address = (TextView) row.findViewById(R.id.address);
icon = (ImageView) row.findViewById(R.id.icon);
}
void populateFrom(Restaurant r) {
name.setText(r.getName());
address.setText(r.getAddress());
if (r.getType().equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);
}
else if (r.getType().equals("take_out")) {
icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}
}
}
}
我在这一刻真正的疑问是哪里\\ getView()\\方法曾经叫什么名字?这是工作,但在code什么时候叫我无法理解。如果任何人都熟悉这本书,请你能提供给我发生的事情在这个code快速解释?
My real doubt at this moment is where is \getView()\ method ever called? This is working, but in code I cannot understand when is it called. If anyone is familiar with this book, please can you provide me a fast explanation about what's happening in this code?
感谢:)
推荐答案
getView()
基本上是调用时的东西正试图创建查看
是与该元素关联的 ArrayAdapter
。
getView()
is basically called when something is trying to create the View
that is associated with an element in that ArrayAdapter
.
这将返回查看
是在元素创建 ArrayAdapter
。
It will return the View
that is created for the element in the ArrayAdapter
.
有一个类似的问题已经被问过,看看这个<一个href=\"http://stackoverflow.com/questions/10160475/when-getview-in-arrayadapter-is-called\">question可以帮助你的任何。
A similar question has been asked before, see if this question helps you any.
在这个问题上另一位伟大的教程的可能解释是有帮助的是什么事情与 Adapaters
。
Another great tutorial on this subject that may be helpful in explaining what is going on with Adapaters
.
这篇关于Android的编程教程 - 教程5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!