如何创建具有多个可单击区域的ListView

时间:2014-05-01 07:15:44

标签: android android-listview

我是Android编程的新手。我正在开发一个应用程序,当用户点击任何ListView项目时,它会转到谷歌地图应用程序并在地图上显示该地址的图钉。但 现在我想再向ListView,电话号码和电子邮件地址添加两个字符串。当用户点击地址字符串时,它应该调用谷歌地图。当用户点击电话号码时,它应该拨打该号码,当用户点击电子邮件ID时,它应该打开电子邮件设施。

以下是我的展示活动:

public class DisplayActivity extends Activity implements OnItemClickListener{

ListView listView;
private String tag_name; 
public List<NameAddress> nameAddressList;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);

    Intent intent = getIntent();
    if(intent!= null)
    {
        tag_name = intent.getStringExtra("DashItemName");
        setTitle("List of " +tag_name+ " addresses");
    }

    nameAddressList = null;
    try {
            XMLDOMParserHandler parser = new XMLDOMParserHandler(tag_name);
        nameAddressList = parser.parseXML(getAssets().open("data.xml"));
        ArrayAdapter<NameAddress> adapter =
            new ArrayAdapter<NameAddress>(this,R.layout.list_item, nameAddressList);
        listView.setAdapter(adapter);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display, menu);
    return true;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    // Build the intent
    String address = nameAddressList.get(position).toString();
    address = "geo:0,0?q=" + address;
    String query = URLEncoder.encode(address, "utf-8");
    Uri location = Uri.parse(query);
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, location;
    mapIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

    // Verify it resolves
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
    boolean isIntentSafe = activities.size() > 0;

    // Start an activity if it's safe
    if (isIntentSafe) {
        startActivity(mapIntent);
    }
    else
    {
        Toast.makeText(this, "Please install google maps app", Toast.LENGTH_LONG).show();
    }

} 

}

请建议我解决这个问题的方法。

3 个答案:

答案 0 :(得分:2)

public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
layoutResourceId;
ArrayList<User> data = new ArrayList<User>();

public UserCustomAdapter(Context context, int layoutResourceId,
        ArrayList<User> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.textName = (TextView) row.findViewById(R.id.textView1);
        holder.textAddress = (TextView) row.findViewById(R.id.textView2);
        holder.textLocation = (TextView) row.findViewById(R.id.textView3);
        holder.btnEdit = (Button) row.findViewById(R.id.button1);
        holder.btnDelete = (Button) row.findViewById(R.id.button2);
        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    User user = data.get(position);
    holder.textName.setText(user.getName());
    holder.textAddress.setText(user.getAddress());
    holder.textLocation.setText(user.getLocation());
    holder.btnEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("Edit Button Clicked", "**********");
            Toast.makeText(context, "Edit button Clicked",
                    Toast.LENGTH_LONG).show();
        }
    });
    holder.btnDelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.i("Delete Button Clicked", "**********");
            Toast.makeText(context, "Delete button Clicked",
                    Toast.LENGTH_LONG).show();
        }
    });
    return row;

}

static class UserHolder {
    TextView textName;
    TextView textAddress;
    TextView textLocation;
    Button btnEdit;
    Button btnDelete;
}

}

答案 1 :(得分:1)

Send Email的代码:

    Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.setType("plain/text");

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[] { "emailadd@gmail.com" });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "sub");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Phone Number Calling的代码

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + number));
    ctx.startActivity(callIntent);

将此代码用于ListView Item中的特定文字点击事件。您需要为此创建Custom Adapter

答案 2 :(得分:0)

您需要为列表视图实现自定义适配器,在那里您可以将单击侦听器添加到列表视图的每一行中的特定项目,而onItemClickListener对于完整行也将工作相同,只需确保添加android:descendantFocusability="afterDescendants"在列表视图的行项目布局中。

要了解如何实施优化列表适配器,您可以查看由我撰写的this博客