我是Android编程新手。我正在开发Android应用程序,我有一个带有多个textViews的listview,其中2个textViews是Clickable。如果用户点击“手机”textView,它应该拨打该号码,如果用户点击“street_address”textView,它应该使用谷歌地图进行路由。以下是我的适配器类的代码
class ListViewHolder {
TextView name;
TextView street_address;
TextView phone;
TextView distance;
ListViewHolder(View v)
{
name = (TextView) v.findViewById(R.id.name);
street_address = (TextView) v.findViewById(R.id.address);
phone = (TextView) v.findViewById(R.id.phone);
distance = (TextView) v.findViewById(R.id.distance);
}
}
public class ListDisplayAdapter extends BaseAdapter
{
Context mcontext = null;
public List<NameAddress> listData;
public ListDisplayAdapter(Context context, List<NameAddress> list)
{
mcontext = context;
this.listData = list;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int i)
{
return listData.get(i);
}
@Override
public long getItemId(int i)
{
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View row = view;
ListViewHolder holder = null;
if (row == null)
{
LayoutInflater inflater =(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_row, viewGroup, false);
holder=new ListViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ListViewHolder) row.getTag();
}
NameAddress data = listData.get(position);
holder.name.setText(data.getName());
holder.phone.setText(data.getPhone());
holder.street_address.setText(data.toString());
holder.distance.setText(data.getDistance());
return row;
}
}
以下是活动类的代码
public class ListDisplayActivity extends Activity
{
ListView listView;
public String tag_name;
public List<NameAddress> nameAddressList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_display);
listView = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
if(intent!= null)
{
tag_name = intent.getStringExtra("DashItemName");
}
nameAddressList = null;
try
{
XMLDOMParserHandler parser = new XMLDOMParserHandler(tag_name);
nameAddressList = parser.parseXML(getAssets().open("data.xml"));
ListDisplayAdapter listAdapter = new ListDisplayAdapter(this, nameAddressList);
listView.setAdapter(listAdapter);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
请告诉我如何编码
答案 0 :(得分:0)
试试这个,
public class ListDisplayAdapter extends BaseAdapter
{
Context mcontext = null;
public List<NameAddress> listData;
public ListDisplayAdapter(Context context, List<NameAddress> list)
{
mcontext = context;
this.listData = list;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int i)
{
return listData.get(i);
}
@Override
public long getItemId(int i)
{
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View row = view;
ListViewHolder holder = null;
if (row == null)
{
LayoutInflater inflater =(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ListViewHolder();
row = inflater.inflate(R.layout.list_row, viewGroup, false);
name = (TextView) v.findViewById(R.id.name);
holder.street_address = (TextView) v.findViewById(R.id.address);
holder.phone = (TextView) v.findViewById(R.id.phone);
holder.distance = (TextView) v.findViewById(R.id.distance);
row.setTag(holder);
}
else
{
holder = (ListViewHolder) row.getTag();
}
NameAddress data = listData.get(position);
holder.name.setText(data.getName());
holder.phone.setText(data.getPhone());
holder.phone.settag(data.getPhone());
holder.street_address.setText(data.toString());
holder.street_address.settag(data.toString());
holder.distance.setText(data.getDistance());
holder.phone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phoneno = ((Textview)v).gettag();
}
});
holder.street_address.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String streetaddress = ((Textview)v).gettag();
}
});
return row;
}
class ListViewHolder {
TextView name;
TextView street_address;
TextView phone;
TextView distance;
}
}