2011-12-15 60 views
0

好吧,我试图做一个列表视图,列出一些地方,每个项目将有一个图片(ImageVew)和一个TextView,当一个地方被击中时,一个AlerDialog框将出现信息(每个地方的不同信息)。我知道如何制作列表视图...但我不知道如何使它可点击并显示带有差异信息的对话框窗口......我还需要一个适配器。是有可能做到这一点?如果是这样的话?怎么样?比你TextView与AlerDialog

回答

1

在我的情况下,图像是一个复选框。

的XML可能是这样的:

<?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"> 

<CheckBox android:id="@+id/check" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:button="@android:drawable/btn_star" 
    android:focusable="false"/> 

<TextView android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
    android:layout_alignParentRight="true" 
    android:layout_marginLeft="5px" 
    android:layout_marginTop="6px" 
    android:layout_toRightOf="@+id/check" 
    android:textSize="25px" 
    android:focusable="false"/> 

</RelativeLayout> 

你需要像一个适配器:

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

    final Object np = getItem(position); 

    View view = null; 

    if (convertView == null) { 

     LayoutInflater inflator = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     view = inflator.inflate(R.layout.listitem, null); 

     final ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.text = (TextView) view.findViewById(R.id.label); 
     viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check); 

     view.setTag(viewHolder); 
     viewHolder.checkbox.setTag(np); 

    } else { 

     view = convertView; 
     ((ViewHolder) view.getTag()).checkbox.setTag(np); 

    } 

    final ViewHolder holder = (ViewHolder) view.getTag(); 
    holder.text.setText(np.toString); 

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

     @Override 
     public void onClick(View v) { 

      [Create the Dialog] 

     } 
    }); 

    holder.checkbox 
      .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 

        [do something] 

       } 
      }); 

    return view; 
} 

static class ViewHolder { 

    protected TextView text; 
    protected CheckBox checkbox; 

} 
1

对列表视图点击添加事件侦听器:

getListView().setOnItemClickListener(new OnItemClickListener() { 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // you create your dialog here 
     } 
    }); 

来创建一个对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("your_message_here") 
        .setPositiveButton(getResources().getString(R.string.ok), 
          dialogClickListener).setCancelable(false).show();