2014-07-24 3 views
1

목록보기가 있습니다. 목록의 모든 항목에는 textviews가 들어 있습니다. 목록에서 항목의 버튼을 클릭하면 단추가 나타납니다. 단추를 클릭하면 단추의 위치를 ​​인쇄하고 싶습니다. 첫 번째 줄에있는 버튼을 나는 0에서 인쇄에 가고 싶어하지만 작동하지 않습니다이 내가이 문제에 시간을 많이 보냈다목록보기에서 선택된 버튼의 위치를 ​​얻습니다.

private void displayListView() { 

      Cursor cursor = dbHelper.fetchAllCountries(); 

      // The desired columns to be bound 
      String[] columns = new String[] { 
        PhonesDbAdapter.KEY_NAME, 
        PhonesDbAdapter.KEY_CONTINENT, 

      }; 

      // the XML defined views which the data will be bound to 
      int[] to = new int[] { 
        R.id.continent, 
        R.id.name 

      }; 

      // create the adapter using the cursor pointing to the desired data 
      //as well as the layout information 
      dataAdapter = new SimpleCursorAdapter(
        this, R.layout.phone_layout, 
        cursor, 
        columns, 
        to, 
        0); 

      listView = (ListView) findViewById(R.id.listView1); 
      // Assign adapter to ListView 
      listView.setAdapter(dataAdapter); 
    } 
    public void print(View v) 
     { 

     } 


and here how my item look like 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="6dip" 
    android:background="#f0f0f0" > 

    <TextView 
     android:id="@+id/continent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 

     android:textColor="#275a0d"/> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 

     android:text="TextView" 
     android:textColor="#000"/> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/ic_launcher"/> 

    <Button 
     android:id="@+id/call" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/button1" 
     android:background="@drawable/ic_launcher" 
     android:onClick="print"/> 

</RelativeLayout> 

and this the layout that contain the listview 
<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#f0f0f0"> 

     <EditText android:id="@+id/myFilter" android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:ems="10"> 
     </EditText> 

     <ListView android:id="@+id/listView1" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

    </LinearLayout> 

보기를 표시하는 나의 방법이며 내가 유 나를 도울 수 있기를 바랍니다 그리고 내 나쁜 영어에 대해 정말 미안합니다

답변

2

getView() 어댑터를 구현하고 있습니까? 당신은 당신이 Button의 태그와 위치를 설정하고 onClick 방법에를 검색 할 수 있습니다 그렇게되면 당신의 Button

OnClickListener을 추가 할 위치 때문이다. 이 같은

뭔가 : 지난 3 삼년에서

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

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.your_item_layout, parent, false); 
     Button myButton = (Button) row.findViewById(R.id. call); 
     myButton.setOnClickListener(new OnClickListener { 

      @Override 
      public void onClick(View view) { 
       int position = (int)view.getTag(); 
       //Do whatever you want with the position here 
      } 
     }); 

     myButton.setTag(position); 

     return row; 
} 
+0

같은 것을 나는 말했지만 코드를 쓰지 않았다 ... 정답은 틀림 없다. 먼저 사용자 지정 어댑터를 만들어야합니다. –

+0

불행히도, getView 메서드를 구현하지 않았습니다. – user3871129

+0

@ user3871129 구현해야합니다. –

1

나는 안드로이드에서 작동하지하고 그래서 내 제안이 올바른지 아닌지 모르겠습니다. 당신은 당신을 위해 그것을 테스트해야합니다.

위의 경우 onItemClickListner는 전체 목록 행을 추적 할 때만 사용할 수 있기 때문에 사용할 수 없습니다. 내 생각에 사용자 지정 어댑터를 만들고 getView 메서드를 재정의 할 수 있습니다. getView 메소드에서 당신의 위치를 ​​얻고 getview에서 button click listner 버튼을 설정할 수 있습니다. 이렇게하면 이런 식으로 버튼 위치를 얻을 수 있습니다. 당신은 그럼 내가 당신을 위해 그것을 쓰기를 시도 할 수 있습니다 몇 가지 코드를해야하는 경우 한 번 시도해

은 ...

이것은 당신이 CUSTOM 어댑터를 만드는 방법입니다.

public class my_custom_adapter extends ArrayAdapter<String> { 
    private Context context      = null; 
    ArrayList<String> elements     = null; 

    public my_custom_adapter(Context context, int type, ArrayList<String> elements) 
    { 
     super(context, type, elements); 
     this.elements = elements; 
     this.context = context; 
    } 


    //THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW. 
    static class ViewHolder 
    { 
     public TextView tv; 
     public Button cb; 
    } 

    @Override 
    public View getView (final int position, View convertView, ViewGroup parent) 
    { 
     View rowView = convertView; 
     ViewHolder holder = null; 

     if (rowView == null) { 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(
               Context.LAYOUT_INFLATER_SERVICE); 
      // HERE I AM INFLATING LISTVIEW LAYOUT. 
      rowView = inflater.inflate(R.layout.inflated_layout, null, false); 
      holder = new ViewHolder(); 
      holder.cb = (Button) rowView.findViewById(R.id.checkBox1); 
      holder.tv = (TextView) rowView.findViewById(R.id.textView1); 
      rowView.setTag(holder); 

     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 

     if (holder != null) { 

      holder.cb.setOnClickListener(new View.OnClickListener() { 

       @Override 
      public void onClick(View arg0) { 
       // IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER 

      } 
      }); 
     } 
     return rowView; 
    } 
} 
+0

일부 코드는 고맙습니다. – user3871129

+0

CarlosJimenez는 이미 코드를 작성하여 배열 어댑터 또는 simpleadapter 클래스를 재정의해야하며 getview 메소드를 무시할 수 있습니다. –

+0

미안하지만 어떻게 할 수 있는지 이해할 수 없다. – user3871129

관련 문제