0

我每行添加一个按钮以在数组适配器中显示该行中的地图。当单击该按钮时,我想获得该行中的值。如何在单击按钮时获得这些值。

我的课:

public class MyListAdapter extends ArrayAdapter<String> {

private final Context context;
private final ArrayList<HashMap<String, ArrayList<String>>> pjclist;
private final ArrayList<PermJorneyCycleBean> pjcarraylist ;
String villagename;
int black = Color.WHITE;
float village = 20f;
float depot = 16f;
int red = Color.RED;
int count;
ArrayList<String> Deoptname;

public MyListAdapter(Context context,ArrayList<HashMap<String, ArrayList<String>>>pjcretrivelist, String [] villagename,ArrayList<PermJorneyCycleBean>itempjcarraylist) {
  //    public MyListAdapter(Context context,ArrayList<PermJorneyCycleBean> pjcretrivelist, String [] villagename) {
    super(context, R.layout.scheduleplan,villagename);
    this.context = context;
    this.pjcarraylist=itempjcarraylist;
    this.pjclist=pjcretrivelist;
    count =pjcretrivelist.size();

}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout rowView1=null;
    LinearLayout rowView=null;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (position<count){
    rowView1= (LinearLayout) inflater.inflate(R.layout.scheduleplan, null, true);
    rowView= (LinearLayout) rowView1.findViewById(R.id.plan);
    HashMap<String, ArrayList<String>> depotlistnew = new HashMap<String, ArrayList<String>>();
    depotlistnew = pjclist.get(position);
    Iterator<Entry<String, ArrayList<String>>> itr = depotlistnew.entrySet().iterator();

    while (itr.hasNext()) {
        Map.Entry pairs = (Map.Entry) itr.next();
        villagename = pairs.getKey().toString();
        createNewRow(rowView, villagename, black, village);
        Deoptname = (ArrayList) pairs.getValue();
        for (int i = 0; i < Deoptname.size(); i++) {
            String depotname = new String();
            depotname = Deoptname.get(i);
            createNewRow(rowView, depotname, red, depot);

        }

    }

    Button mapbutton = createbutton(rowView, "Locate on Map");
    mapbutton.setTag(position);
    mapbutton.setClickable(true);
    mapbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), " This is to depot map"+villagename,Toast.LENGTH_LONG).show();
        }
    });
    }

    else if (position==count){

        rowView1 = (LinearLayout) inflater.inflate(R.layout.schedulemap, null, true);

        Button villagebutton = (Button)rowView1.findViewById(R.id.getBack);
        villagebutton.setClickable(true);
        villagebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "This is for Map"+villagename,Toast.LENGTH_LONG).show();
            }
        });

    }
    else if (position==count+1)
    {
    rowView1 = (LinearLayout) inflater.inflate(R.layout.scheduleplanlast, null, true);  
    Button backbutton = (Button)rowView1.findViewById(R.id.getBackHome);
    backbutton.setClickable(true);
    backbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), " This is to test it",Toast.LENGTH_LONG).show();
        }
    });
    }



    return rowView1;
}

public void createNewRow(LinearLayout ll1, String value, Integer color,float size) {
    TextView tv = new TextView(ll1.getContext());
    tv.setTextColor(color);
    tv.setTextSize(size);
    tv.setText(value);
    ll1.addView(tv);

}

public Button createbutton(LinearLayout ll1, String value) {
    Button backbutton = new Button(ll1.getContext());
    backbutton.setText(value);
    backbutton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    ll1.addView(backbutton);
    return backbutton;
}

public TextView createTextView(LinearLayout ll1, String value){
    TextView lattextview = new TextView(ll1.getContext());
    lattextview.setVisibility(0);
    lattextview.setText(value);
    lattextview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    ll1.addView(lattextview);


    return lattextview;
}

}

单击这些按钮时,我无法获得位置。

4

2 回答 2

3

供您参考,我有以下代码片段,用于单击阵列适配器

   class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private Context context;

    public MySimpleArrayAdapter(Context context) {
        super(context, R.layout.buddy_list);
        this.context = context;

    }

    public int getCount() {
        return speedList.size();
    }

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

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

        View rowView = convertView;

        if (rowView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = vi.inflate(R.layout.speeddial_list, null);
        }
        TextView name = (TextView) rowView.findViewById(R.id.Name);
        TextView buddyId = (TextView) rowView.findViewById(R.id.sipid);
        Button btn = (Button)rowView.findViewById(R.id.speeddialbtn);


        name.setText(speedList.get(position).getName());
        buddyId.setText(speedList.get(position).getNumber());
        btn.setText(Integer.toString(speedList.get(position).getSPDIndex()));

        /*name.setText(names.get(position).toString());
        buddyId.setText(buddyIds.get(position).toString());
        btn.setText(numberButton.get(position).toString());*/

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!speedList.get(0).getName().equals("    No SpeedDial Found")) {
                    registerForContextMenu(getListView());
                    getListView().showContextMenu();
                } else {
                    unregisterForContextMenu(getListView());
                }

                selected_name_fromlist = speedList.get(position).getName();
                selected_number_fromlist = speedList.get(position).getNumber();
                System.out.println(" selected :" + selected_name_fromlist);
            }
        });
        return rowView;
    }
}
于 2012-08-20T07:45:26.910 回答
0

这是ListView Row教程中一个很好的处理按钮点击。

于 2012-08-20T07:22:11.557 回答