为自定义的ListView的条目添加对话框

来源:互联网 发布:区域生长分割算法 编辑:程序博客网 时间:2024/05/16 00:30
package android.alertDialog;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import android.app.Activity;
import android.os.Bundle;
import android.test.examples.R;
import android.widget.ListView;


public class MyNewAdapterActivity extends Activity {



private ListView listView;
private MyNewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alertdialog_main);

listView = (ListView) this.findViewById(R.id.listView000);
adapter = new MyNewAdapter(this, getDataArrayList());
listView.setAdapter(adapter);
}


private ArrayList<Map<String, Object>> getDataArrayList() {
ArrayList<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("name", "zhao");
data.add(map);
return data;
}
}




//自定义的ListView


package android.alertDialog;


import java.util.ArrayList;
import java.util.Map;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.test.examples.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;




public class MyNewAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private Context context;
private ArrayList<Map<String, Object>> dataArrayList ;




public MyNewAdapter(Context context,
ArrayList<Map<String, Object>> dataArrayList) {
super();
this.context = context;
this.dataArrayList = dataArrayList;
this.layoutInflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
return dataArrayList.size();
}


@Override
public Object getItem(int arg0) {

return dataArrayList.get(arg0);
}


@Override
public long getItemId(int position) {
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
NewComposites newComposites = null ;
if(convertView == null ){
newComposites = new NewComposites();
convertView = layoutInflater.inflate(R.layout.activity_alertdialog_content, null);
newComposites.textName = (TextView) convertView.findViewById(R.id.textNamenew);
newComposites.btnClick = (Button) convertView.findViewById(R.id.btnClick);
convertView.setTag(newComposites);
} else {
newComposites = (NewComposites) convertView.getTag();
}
newComposites.textName.setText(dataArrayList.get(position).get("name").toString());
newComposites.btnClick.setOnClickListener(new OnClickListener() {
int i = 0;
@Override
public void onClick(View v) {
// Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("添加好友");
builder.setMessage("是否加为好友?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){


@Override
public void onClick(DialogInterface dialog, int which) {


}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});
builder.show();
System.out.println("=====Hello======"+i);
i++;
}
});
return convertView;
}
}


//组件包
package android.alertDialog;


import android.widget.Button;
import android.widget.TextView;


public class NewComposites {
public TextView textName;
public Button btnClick;
}




//布局文件之主文件


<?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:layout_gravity="center_horizontal"
        android:text="@string/TextView000"
        android:textSize="23sp" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView
            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:text="@string/TextView001"
            android:textSize="23sp" />


        <TextView
            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.36"
            android:textSize="23sp"
            android:text="@string/Button000" />


    </LinearLayout>


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


</LinearLayout>


//布局文件之 每条目显示


<?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="wrap_content"
    android:orientation="horizontal" >
    <TextView
            android:id="@+id/textNamenew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:text="@string/TextView001"
            android:textSize="23sp" />


        <Button
            android:id="@+id/btnClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.36"
            android:text="@string/Button000" />
</LinearLayout>







原创粉丝点击