Android ArrayAdapter onclick项目

时间:2016-05-15 15:30:33

标签: android

我创建了一个自定义arrayadapter,它使我从soundcloud请求“get”中获取元素列表,但是当我点击其中一个元素时,会给我这条消息:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

这是我的代码:

活动

    package com.giuseppemorra.domusic;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.impl.client.HttpClients;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.util.EntityUtils;

public class Second extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{
    Adapter adapter;
    ListView lista;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    protected void onStart(){
        super.onStart();
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String musica = extras.getString("musica");
            String ric = musica;
            new Mod().execute(ric);
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

        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();

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }



    private class Mod extends AsyncTask<String, Integer, String> {
        protected String doInBackground(String... parametri) {
            String par = Arrays.toString(parametri);
            par = par.replace("[", "");
            par = par.replace("]", "");
            String[] risultati = par.split(",");
            String musica = risultati[0];
            Log.d("cell", musica);
            HttpClient httpclient = HttpClients.createDefault();
            HttpGet httppost = new HttpGet();
            String gio = "";
            try {
                URI website = new URI("http://api.soundcloud.com/tracks/?client_id=4ee809712e980dfc07b9e8af6f42d434&limit=200&&q="+URLEncoder.encode(musica,"UTF-8"));
                httppost.setURI(website);
                HttpResponse response = httpclient.execute(httppost);
                BufferedReader in = new BufferedReader(new InputStreamReader( response.getEntity().getContent()));
                String line = in.readLine();
                Log.d("Risposta dal server", line);
                gio = line;
            } catch (IOException | URISyntaxException b) {
                b.printStackTrace();
                b.printStackTrace();
            }
            return gio;
        }

        protected void onProgressUpdate(String... progress) {

        }

        protected void onPostExecute(String result) {
            String str = result;
            Log.d("new",str);
            JSONArray jArrayObject;
            try {
                jArrayObject = new JSONArray(str);
                String[] datiFin = new String[jArrayObject.length()];
                String[] titleFin = new String[jArrayObject.length()];
                String[] durataFin = new String[jArrayObject.length()];
                String[] avatar_urlFin = new String[jArrayObject.length()];
                for (int i = 0; i<jArrayObject.length(); i++) {
                    String stream =  jArrayObject.getJSONObject(i).getString("stream_url").toString();
                    String id =  jArrayObject.getJSONObject(i).getString("id").toString();
                    String title =  jArrayObject.getJSONObject(i).getString("title").toString();
                    String durata =  jArrayObject.getJSONObject(i).getString("duration").toString();
                    JSONObject jObj = new JSONObject(jArrayObject.getJSONObject(i).getString("user").toString());
                    String avatar_url =  jObj.getString("avatar_url").toString();
                    datiFin[i] = stream.concat(",").concat(id);
                    titleFin[i] = title;
                    int minutes = (int) ((Integer.parseInt(durata) / (1000*60)) % 60);
                    String durataFinale = minutes+" Minuti";
                    durataFin[i] = durataFinale;
                    avatar_urlFin[i] = avatar_url;
                }
                lista = (ListView)findViewById(R.id.musica_lista);
                lista.setFastScrollEnabled(true);
                adapter = new com.giuseppemorra.domusic.Adapter(Second.this,titleFin,datiFin,durataFin,avatar_urlFin);
                lista.setAdapter((ListAdapter) adapter);
                lista.setOnItemClickListener(new OnItemClickListener(){

                    @SuppressWarnings("unchecked")
                    public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
                    Log.d("ciao","ciao");

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

}

ArrayAdaptery:

package com.giuseppemorra.domusic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Adapter extends ArrayAdapter<String> {

    String[] titolo, dati, durata, avatar;
    Context context;
    Holder holder;

    public Adapter(Context context, String[] titolo, String[] dati, String[] durata, String[] avatar) {
        super(context, R.layout.lista_musica, titolo);
        // TODO Auto-generated constructor stub
        this.titolo = titolo;
        this.avatar = avatar;
        this.durata = durata;
        this.dati = dati;
        this.context = context;
    }

    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            holder = new Holder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.lista_musica, null, true);
            holder.titoloMod = (TextView) view.findViewById(R.id.titoloMusica);
            holder.avatarMod = (ImageView) view.findViewById(R.id.immagieMusica);
            holder.durataMod = (TextView) view.findViewById(R.id.durataMusica);
            holder.contMod = (TextView) view.findViewById(R.id.contDati);
            holder.titoloMod.setText(titolo[position]);
            holder.durataMod.setText(durata[position]);
            holder.contMod.setText(dati[position]);
            Picasso.with(context).load(avatar[position]).into(holder.avatarMod);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        return view;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

class Holder {
    TextView titoloMod, durataMod, contMod;
    ImageView avatarMod;
}

的ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.giuseppemorra.domusic.Second"
    tools:showIn="@layout/second_bar_main"
    android:focusable="false"
    android:clickable="false">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/musica_lista"
        android:scrollingCache="false"
        android:smoothScrollbar="true"
        android:clickable="true" />
</LinearLayout>

单项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:clickable="true" >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/immagieMusica"
        android:layout_margin="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/titoloMusica"
        android:layout_alignTop="@+id/immagieMusica"
        android:layout_toEndOf="@+id/immagieMusica"
        android:textSize="10dp"
        android:textIsSelectable="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/durataMusica"
        android:layout_alignBottom="@+id/immagieMusica"
        android:layout_toEndOf="@+id/immagieMusica" />

    <EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/contDati"
        android:layout_above="@+id/durataMusica"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

然后我会理解我错在哪里,以及如何执行qaundo功能,你点击其中一个列表元素

0 个答案:

没有答案