Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用自定义阵列适配器实现McClickListener?_Android_Arrays_Listview_Android Arrayadapter_Onitemclicklistener - Fatal编程技术网

Android 如何使用自定义阵列适配器实现McClickListener?

Android 如何使用自定义阵列适配器实现McClickListener?,android,arrays,listview,android-arrayadapter,onitemclicklistener,Android,Arrays,Listview,Android Arrayadapter,Onitemclicklistener,我一直在研究这个问题,但我仍然不太清楚如何使用带有阵列适配器的单击侦听器 我需要一个列表中每个项目的单击侦听器 代码: 区域项目: package fogames.tamagomonsters; public class Area { public String name; public String number; public Area(String name, String number) { this.name = name;

我一直在研究这个问题,但我仍然不太清楚如何使用带有阵列适配器的单击侦听器

我需要一个列表中每个项目的单击侦听器

代码:

区域项目:

package fogames.tamagomonsters;

public class Area {

    public String name;
    public String number;

    public Area(String name, String number) {
        this.name = name;
        this.number = number;
    }
}
阵列适配器:

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;
    }
}
包fogames.tamagomonsters;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类区域适配器扩展阵列适配器{
公共区域适配器(上下文、阵列列表区域){
超级(上下文,0,区域);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
面积=获取项目(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.item_区域,父项,false);
}
//数据填充的查找视图
TextView tvname=(TextView)convertView.findViewById(R.id.tvname_区域);
TextView tvnumber=(TextView)convertView.findViewById(R.id.tvnumber\u of_);
//使用数据对象将数据填充到模板视图中
tvname.setText(Area.name);
tvnumber.setText(区域编号);
//返回要在屏幕上渲染的已完成视图
返回视图;
}
}
活动:

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);
    }
}
包fogames.tamagomonsters;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.Window;
导入android.view.WindowManager;
导入android.widget.AdapterView;
导入android.widget.FrameLayout;
导入android.widget.ListView;
导入java.util.ArrayList;
公共类PlayMenuActivity扩展了活动{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
FrameLayout fl=新的FrameLayout(本);
fl.setBackgroundColor(Color.argb(255,0,0,0));
//构建数据源
ArrayList arrayOfAreas=新的ArrayList();
//创建适配器以将阵列转换为视图
AreasAdapter适配器=新的AreasAdapter(此为arrayOfAreas);
//将适配器连接到ListView
ListView lv=新的ListView(本);
低压设置适配器(适配器);
//恢复首选项
SharedReferences prefs=GetSharedReferences(PreferenceConstants.PREFERENCE\u NAME,MODE\u 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
布尔值[]mgot=新布尔值[mall];
int[]exp=新的int[eqall];
int[]lvl=新int[eqall];
int[]at=新的int[eqall];
int[]en=新的int[eqall];
对于(int i=0;i

提前谢谢。

您可以这样做:

•首先使用此方法插入适配器

@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...

}
});
final AreasAdapter adapter=新的AreasAdapter(此为arrayOfAreas);
ListView lv=新的ListView(本);
低压设置适配器(适配器);
mliststopsumni.setonicmclicklistener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id{
Area yourClickArea=adapter.getItem(位置);
//代码的其余部分。。。
}
});
祝你好运;)

诸如此类:

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);
}
arraylistarraylistarreas;
公共区域适配器(上下文、阵列列表区域){
超级(上下文,0,区域);
this.arrayListAreas=面积;
}
//然后。。。。
@凌驾
公共区域getItem(内部位置){
返回arrayListAreas.get(位置);
}

在这种情况下,我应该写什么来代替[yourArrayAreas]?