2011-05-02 58 views
0

我想在每個列表項中添加一個按鈕,當用戶按下它來撥打電話時。但是當用戶按下文本時,什麼也沒有發生......這可能嗎?這是我的代碼:在列表中添加一個按鈕查看

public class museum extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.museum); 


     ListView list = (ListView) findViewById(R.id.list); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 


     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("name", "Archaeological Museum of Chania"); 
     map.put("address", "Chalidon 21 , Chania"); 

     mylist.add(map); 
     map = new HashMap<String, String>(); 
     map.put("name", "Byzantine Museum"); 
     map.put("address", "Theotokopoulou 82 , Chania"); 

     mylist.add(map); 
     // ... 
     ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row_museum, 
        new String[] {"name", "address"}, new int[] {R.id.TextView01, R.id.TextView02}); 
     list.setAdapter(mSchedule); 



     list.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       switch(position) 
       { 
        case 0: 
          break; 
        case 1: 
         break; 
        case 2: 
         break; 
        case 3: 
         break; 


       } 
      } 
      }); 
    } 
} 
+0

你可以使用自定義適配器... – 2011-05-02 11:25:11

回答

1

雅你可以做。爲此

  1. 您將不得不爲包含textview和按鈕的列表行創建一個佈局文件。
  2. 在自定義ArrayAdapter中使用該佈局。

查看例如site

+0

我已經看到了這個例子,但作爲即時通訊新的android開發,我覺得它太複雜.... :( – kostas 2011-05-02 11:25:26

+0

就是這樣...可能是第一次遇到問題..但在很多情況下,我們使用該...我們也可以擴展BaseAdapter .. – Mathew 2011-05-02 11:33:40

+0

與該示例的區別在於,我想輸入自己的數據而不是從服務器解析它......所以我必須爲每一行創建一個字符串表? – kostas 2011-05-02 12:46:02

0

您必須擁有一個自定義列表適配器,如下所示。

public class CustomListAdapter extends BaseAdapter { 
private ArrayList<SingleElementDetails> allElementDetails; 
private Context con; 
private LayoutInflater mInflater; 

public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) { 
    allElementDetails = results; 
    mInflater = LayoutInflater.from(context); 
    con=context; 
     public View getView(int position, View convertView, ViewGroup parent) 
{ 
    convertView = mInflater.inflate(R.layout.listview1, null); 

    Button bt=(Button)convertView.findViewById(R.id.bt); 
    TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry); 
    TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry); 
    TextView textview3=(TextView)convertView.findViewById(R.id.description_entry); 
    textview1.setText(allElementDetails.get(position).getDishName()); 
    textview2.setText(allElementDetails.get(position).getCategory()); 
    textview3.setText(allElementDetails.get(position).getDescription());  


    bt.setOnClickListener(new OnClickListener(){ 


     public void onClick(View v) { 
      Intent intent=new Intent(con,MainActivity.class); 
      con.startActivity(intent); 

     } 

    }); 
    return convertView; 
}  

}

0

您可以使用自定義適配器是這樣的:

ListView lv_ArchivePartylist; 

ArrayList<Parties> select_archived_party; 

lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01); 
     lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener() 
      { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 
       // TODO Auto-generated method stub 
       if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE) 
       { 
        view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE); 
        Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show(); 
select_archived_party.add(archived_parties.get(position)); 
} 
} 

}); 

然後,TextView的,使用按鈕,而不是和那個按鈕的個人點擊事件,您可以編寫代碼來撥打電話...希望它有助於:-)