如何通过其中一个arraylist的元素点击电话?

时间:2016-04-27 11:10:19

标签: java android arrays

我有一个带有树元素的ArrayList (icon, text(name), text(phonenumber))。 我想做两件事,

1)使列表可点击的整个元素

2)拨打该号码打电话

现在我知道我需要使用onclicklistener但是如何在我的代码中实现它?我的主要和适配器

    public class MainActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target_item);


    // if extending Activity
    //setContentView(R.layout.activity_main);

    // 1. pass context and data to the custom adapter
    MyAdapter adapter = new MyAdapter(this, generateData());

    // if extending Activity 2. Get ListView from activity_main.xml
    //ListView listView = (ListView) findViewById(R.id.listview);

    // 3. setListAdapter
    //listView.setAdapter(adapter); if extending Activity
    setListAdapter(adapter);
    }

private ArrayList<Model> generateData() {
    ArrayList<Model> models = new ArrayList<Model>();
    // here are elements of the arraylist header & next are list's elements
    models.add(new Model("[text]"));
    models.add(new Model(R.drawable.[icon], "[text]", "**[text(number)]**"));
    models.add(new Model(R.drawable.[icon2], "[text2]", "**[text(number2)]**"));
    return models;
}
}

 public class MyAdapter extends ArrayAdapter<Model> {

private final Context context;
private final ArrayList<Model> modelsArrayList;


public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {

    super(context, R.layout.target_item, modelsArrayList);

    this.context = context;
    this.modelsArrayList = modelsArrayList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // 1. Create inflater
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // 2. Get rowView from inflater

    View rowView = null;
    if(!modelsArrayList.get(position).isGroupHeader()){
        rowView = inflater.inflate(R.layout.target_item, parent, false);

        // 3. Get icon,title & counter views from the rowView
        ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
        TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
        TextView counterView = (TextView) rowView.findViewById(R.id.item_counter);

        // 4. Set the text for textView
        imgView.setImageResource(modelsArrayList.get(position).getIcon());
        titleView.setText(modelsArrayList.get(position).getTitle());
        counterView.setText(modelsArrayList.get(position).getCounter());

    }
    else{
        rowView = inflater.inflate(R.layout.group_header_item, parent, false);
        TextView titleView = (TextView) rowView.findViewById(R.id.header);
        titleView.setText(modelsArrayList.get(position).getTitle());

    }

    // 5. retrn rowView
    return rowView;
}
}

2 个答案:

答案 0 :(得分:0)

试试这个 -

http://mywebsite.com/category/beauty/

答案 1 :(得分:0)

MyAdapter.class进行更改

rowview.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:0377778888"));
          // hear set your array position vise set mobile nmber
            startActivity(callIntent);  
        }
    });