2012-04-20 3 views
0

나는 안드로이드를 처음 접했다. 난 행보기에 대한 custome_row_view.xml 파일을 만들었습니다.버튼 사용자 정의 행 뷰의 OnclickEvent 안드로이드의 ListView

목록을 사용하면 2 행을 표시합니다. 이제 custome_row_view에 버튼 하나를 추가했습니다.

해당 버튼에 대한 코드를 작성하는 방법

메인. 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 난 당신이 목록보기를 채울 CustomAdapter를 사용하는 생각

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

+0

만들어 내 전체 코드를 편집 .......... 우리에게 –

+0

를 몇 가지 코드를하시기 바랍니다 부여 것을 몇 가지 코드를 포함한다. 지금 설명해주세요. @Krishna Suthar – Naresh

답변

0

..

그래서 다음의 경우, 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

Pls가 위 코드를보고이 코드를 어디에 써야할까요? – Naresh

+0

USe custom listview – Abhi

+0

pls 위 코드를 편집하십시오. 이 코드를 사용하여 .. 할 수 있습니까? 나는이 일을 wr 모르겠다. – Naresh

관련 문제