2012-04-20 68 views
0

我是新來的android。我爲行視圖創建了custome_row_view.xml文件。按鈕在自定義行中的OnclickEvent在Android中查看ListView在Android中的

使用列表我顯示兩行。現在我在custome_row_view中添加了一個按鈕。

如何編寫代碼,該按鈕的onclick

主。 XML

<ListView 
     android:id="@id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

custom_row_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/buton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

CustomeViewActivity.java

package naresh.custom.view; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
import java.util.ArrayList; 
import java.util.HashMap; 
import android.app.ListActivity; 

public class CustomeViewActivity extends ListActivity implements OnItemClickListener{ 
    static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     list.removeAll(list); 

     ListView lista=(ListView)findViewById(android.R.id.list); 
     int count = lista.getChildCount(); 

     SimpleAdapter adapter = new SimpleAdapter(
      this, 
      list, 
      R.layout.custom_row_view, 
      new String[] {"title","description"}, 
      new int[] {R.id.textView1,R.id.textView2} 
     ); 

     if(count<5){ 
      HashMap<String,String> temp = new HashMap<String,String>(); 
      temp.put("title","On/Off"); 
      temp.put("description", "Alert to be On or Off"); 
      list.add(temp); 
      HashMap<String,String> temp1 = new HashMap<String,String>(); 
      temp1.put("title","Select tone"); 
      temp1.put("description", "Select tone for alerting"); 

      list.add(temp1); 
      HashMap<String,String> temp2 = new HashMap<String,String>(); 
      temp2.put("title","Alert Time"); 
      temp2.put("description", "Select Time for alerting Before/After"); 
      list.add(temp2); 
      HashMap<String,String> temp3 = new HashMap<String,String>(); 
      temp3.put("title","Change Password"); 
      temp3.put("description", "Can cahnge password any time"); 
      list.add(temp3); 

      setListAdapter(null); 
      setListAdapter(adapter); 
     } 
     getListView().setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this,"ITem clicked", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

至少包括你做了什麼已經 – mihail 2012-04-20 08:10:13

+0

給我們一些代碼,請.......... – 2012-04-20 08:12:54

+0

我修改了完整的代碼一些代碼。現在向我解釋@Krishna Suthar – Naresh 2012-04-20 08:22:20

回答

0

我認爲你正在使用CustomAdapter來填充ListView ..

如果是,則在getView()包含按鈕操作:

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

          View row=convertView; 
          FetchHolder holder=null; 

          if (row==null) 
          { 
          LayoutInflater inflater=getLayoutInflater(); 
          row=inflater.inflate(R.layout.grid, parent, false); 

          holder=new FetchHolder(row); 


          row.setTag(holder); 
          } 
         else 
          { 
          holder=(FetchHolder)row.getTag(); 
          } 

          holder.button.setOnClickListener(new OnClickListener() 
          { 
           public void onClick(View v) { 

           } 
          }); 
          return row; 
          } 

         } 



        static class FetchHolder 
        { 
         private Button time=null; 

         FetchHolder(View row) 
         { 

          time=(Button)row.findViewById(R.id.timebutton); 

           } 
+0

請參閱上面的代碼,並告訴我在哪裏編寫此代碼? – Naresh 2012-04-20 09:11:32

+0

使用自定義列表視圖 – Abhi 2012-04-20 09:53:58

+0

請編輯我上面的代碼。使用此代碼..你可以嗎?我不知道wr做到這一點.. – Naresh 2012-04-20 15:46:34

相關問題