如何使用自定义数组适配器实现onItemClickListener?

时间:2015-11-11 16:41:18

标签: android arrays listview android-arrayadapter onitemclicklistener

我一直在搜索这个问题,但我仍然不清楚如何使用数组适配器的点击监听器。

我需要为列表中的每个项目提供一个点击监听器。

代码:

区域项目:

package fogames.tamagomonsters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;


public class AreasAdapter extends ArrayAdapter<Area> {
    public AreasAdapter(Context context, ArrayList<Area> Areas) {
        super(context, 0, Areas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Area Area = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_area, parent, false);
        }
        // Lookup view for data population
        TextView tvname = (TextView) convertView.findViewById(R.id.tvName_area);
        TextView tvnumber = (TextView) convertView.findViewById(R.id.tvNumber_of_beasts);
        // Populate the data into the template view using the data object
        tvname.setText(Area.name);
        tvnumber.setText(Area.number);
        // Return the completed view to render on screen
        return convertView;
    }
}

数组适配器:

package fogames.tamagomonsters;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import java.util.ArrayList;


public class PlayMenuActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        FrameLayout fl = new FrameLayout(this);
        fl.setBackgroundColor(Color.argb(255, 0, 0, 0));

        // Construct the data source
        ArrayList<Area> arrayOfAreas = new ArrayList<Area>();
// Create the adapter to convert the array to views
        AreasAdapter adapter = new AreasAdapter(this, arrayOfAreas);

// Attach the adapter to a ListView
        ListView lv = new ListView(this);
        lv.setAdapter(adapter);

        // Restore preferences
        SharedPreferences prefs = getSharedPreferences(PreferenceConstants.PREFERENCE_NAME, MODE_PRIVATE);
        int mlen = prefs.getInt(PreferenceConstants.MLEN, 0);
        long money = prefs.getLong(PreferenceConstants.MONEY, 0);
        int mall = 6; //hay que ver que hacer con esto...
        int eqall = 3; //igual
        boolean[] mgot = new boolean[mall];
        int[] exp = new int[eqall];
        int[] lvl = new int[eqall];
        int[] at = new int[eqall];
        int[] en = new int[eqall];
        for (int i = 0; i < mall; i++) {
            mgot[i] = prefs.getBoolean(PreferenceConstants.MGOT[i], false);
        }
        for (int i = 0; i < eqall; i++) {
            exp[i] = prefs.getInt(PreferenceConstants.EXP[i], 0);
            lvl[i] = prefs.getInt(PreferenceConstants.LVL[i], 0);
            at[i] = prefs.getInt(PreferenceConstants.AT[i], 0);
            en[i] = prefs.getInt(PreferenceConstants.EN[i], 0);
        }

        String name[] = {getString(R.string.a001)};

        int prado_got = 0;
        if (mgot[0]) {
            prado_got += 1;
        }

        if (mgot[3]) {
            prado_got += 1;
        }

        String prado = String.valueOf(prado_got) + " / 2";

        String number[] = {prado};

        // Add item to adapter
        Area a001 = new Area(name[0], number[0]);
        adapter.add(a001);


        this.setContentView(fl);
    }
}

活动:

public ActionResult Download(Guid customerOrderId)
{
    var order = this.UnitOfWork.GetRepository<CustomerOrder>().Get(customerOrderId);

    var csv = new StringBuilder();

    csv.Append("Customer,Bill To Name,Ship To Name,Patient,Order#,Order Date," +
        "Line,Item#,Item Description,Qty,UOM,Price,Ext Price,Carrier," +
        "Notes,Purchase Order");

    var customer = order.CustomerNumber;
    var billToName = order.BTDisplayName;
    var shipToName = order.ShipTo.CustomerName;
    var orderNum = order.OrderNumber;
    var orderDate = order.OrderDate;
    var carrier = order.ShippingDisplay;
    var notes = order.Notes;
    var subtotal = order.OrderSubTotalDisplay;
    var total = order.OrderGrandTotalDisplay;
    var shipping = order.ShippingAndHandling;
    var tax = order.TotalSalesTaxDisplay;
    var patient = "";
    var purchaseOrder = order.CustomerPO;
    foreach (var cartLine in order.OrderLines)
    {
        var line = cartLine.Line;
        var itemNum = cartLine.Product.ProductCode;
        var itemDesc = cartLine.Description;
        var qty = cartLine.QtyOrdered;
        var uom = cartLine.UnitOfMeasure;
        var price = cartLine.ActualPriceDisplay;
        var ext = cartLine.ExtendedActualPriceDisplay;

        //Customer,Bill To Name,Ship To Name,Patient,Order#,Order Date," + 
        //"Line,Item#,Item Description,Qty,UOM,Price,Ext Price,Carrier," +
        //"Notes,Purchase Order
        var newLine = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15}",
            customer, billToName, shipToName, patient, orderNum, orderDate, line, itemNum, itemDesc,
            qty, uom, price, ext, carrier, notes, purchaseOrder);

        csv.AppendLine(newLine);
    }

    csv.AppendLine();
    csv.AppendLine("Subtotal,Shipping & Handling,Tax,Total");
    csv.AppendLine(string.Format("{0},{1},{2},{3}", subtotal, shipping, tax, total));

    var filename = "MSD-Order-" + orderNum + ".csv";            

    var bytes = Encoding.UTF8.GetBytes(csv.ToString());
    return this.File(bytes, "text/csv");
}

提前谢谢。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

•首先在适配器中插入此方法

@Override
public Area getItem(int position) {
        return [yourArrayAreas].get(position);
}

•然后,在你的活动......

final AreasAdapter adapter = new AreasAdapter(this, arrayOfAreas);

ListView lv = new ListView(this);

lv.setAdapter(adapter);

mListStopsMuni.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id{

Area yourClickArea = adapter.getItem(position);

//Rest of code...

}
});
祝你好运;)

答案 1 :(得分:0)

这样的事情:

ArrayList<Area> arrayListAreas;
public AreasAdapter(Context context, ArrayList<Area> Areas) {
        super(context, 0, Areas);
        this.arrayListAreas = Areas;
}

//And then....
@Override
public Area getItem(int position) {
        return arrayListAreas.get(position);
}