使用arrayAdapter为listview创建一个OnClick列表器,以实现其他屏幕的意图

时间:2015-11-03 19:58:30

标签: android listview android-intent

我制作了一个应用程序,它从我的服务器获取一个JSON文件,并通过listview中的适配器在屏幕上显示内容,效果很好!我想创建一个onClick列表器,它获取已在listview中单击的项目(0,1,2,3等)的编号,并为另一个活动创建一个intent(并发送该编号)..我已经在stackoverflow上的其他问题中尝试了几个建议,但我无法让它们工作..有人可以帮我这个吗?我似乎无法找到正确的解决方案:/

Recepten.java

public class Recepten extends Activity {

ListView list;
ReceptenAdapter adapter;
ArrayList<ReceptenDefine> receptenList;

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

    list = (ListView)findViewById(R.id.receptenList);
    receptenList = new ArrayList<>();

    new ReceptenAsynTask().execute("http://janvandijk.me/zooi/receptenapp/getrecipes.php?type=request&datatype=recepten");
}


public class ReceptenAsynTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            HttpClient client = new SystemDefaultHttpClient();
            HttpPost post = new HttpPost(params[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if (status == 200){
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONArray jArray = new JSONArray(data); //jArray.length()
                for (int i=0; i<jArray.length(); i++){
                    ReceptenDefine recept;
                    recept = new ReceptenDefine();

                    JSONObject jRealObject = jArray.getJSONObject(i);

                    recept.setReceptnaam(jRealObject.getString("naam"));
                    recept.setReceptauthor("Recept van: " + jRealObject.getString("unaam"));
                    recept.setReceptimageurl(jRealObject.getString("imgurl"));
                    recept.setReceptrating("Aantal sterren: " + jRealObject.getString("id"));

                    receptenList.add(recept);

                }
                return true;


            }

        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result){
        super.onPostExecute(result);

        if(!result){
            //Show message that data wasn't parsed
        } else {
            ReceptenAdapter adapter = new ReceptenAdapter(getApplicationContext(), R.layout.row, receptenList);
            list.setAdapter(adapter);
        }



    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_recepten, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        return true;
    }

    return super.onOptionsItemSelected(item);
}

Receptendefine.java

public class ReceptenDefine{
private String receptnaam;
private String receptauthor;
private String receptimageurl;
private String receptrating;

public ReceptenDefine(){

}

public String getReceptnaam() {
    return receptnaam;
}

public void setReceptnaam(String receptnaam) {
    this.receptnaam = receptnaam;
}

public String getReceptauthor() {
    return receptauthor;
}

public void setReceptauthor(String receptauthor) {
    this.receptauthor = receptauthor;
}

public String getReceptimageurl() {
    return receptimageurl;
}

public void setReceptimageurl(String receptimageurl) {
    this.receptimageurl = receptimageurl;
}

public String getReceptrating() {
    return receptrating;
}

public void setReceptrating(String receptrating) {
    this.receptrating = receptrating;
}

ReceptenAdapter.java

public class ReceptenAdapter extends ArrayAdapter<ReceptenDefine>{

ArrayList<ReceptenDefine> ArrayListRecepten;
int Resource;
Context context;
LayoutInflater vi;


public ReceptenAdapter(Context context, int resource, ArrayList<ReceptenDefine> objects) {
    super(context, resource, objects);

    ArrayListRecepten = objects;
    Resource = resource;
    this.context = context;

    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Verschillende items laten zien

}

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

    ViewHolder holder;

    if(convertView == null){
        //Eerste listview item wordt gemaakt
        convertView = vi.inflate(Resource, null);
        holder = new ViewHolder();

        //Set all textview and imageview dingen



        holder.listImage = (ImageView)convertView.findViewById(R.id.listImage);
        holder.listAuthor = (TextView)convertView.findViewById(R.id.listAuthor);
        holder.listName = (TextView)convertView.findViewById(R.id.listName);
        holder.listRating = (TextView)convertView.findViewById(R.id.listRating);

        //Zet het neer
        convertView.setTag(holder);
    } else {
        //volgende list items, doe alles opnieuw zetten
        holder = (ViewHolder)convertView.getTag();
    }

    new DownloadImageTask(holder.listImage).execute(ArrayListRecepten.get(position).getReceptimageurl());
    holder.listName.setText(ArrayListRecepten.get(position).getReceptnaam());
    holder.listAuthor.setText(ArrayListRecepten.get(position).getReceptauthor());
    holder.listRating.setText(ArrayListRecepten.get(position).getReceptrating());
    return convertView;
}

static class ViewHolder {
    public ImageView listImage;
    public TextView listName;
    public TextView listAuthor;
    public TextView listRating;
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

1 个答案:

答案 0 :(得分:0)

在你的onCreate()中:

list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // do stuff ('position' is the position of the item you've clicked on)
    }
});