簡體   English   中英

Android:項目列表單擊JSON生成的列表視圖-無法單擊

[英]Android: item list click on JSON generated list view - unable to click

我從網址獲取JSON,以生成聯系人姓名和電話號碼的列表視圖。 我打算在用戶只需點擊聯系人姓名或電話直接撥打電話的地方使用它。 但是我在獲取檢測到的項目上有問題。 我試圖在“ onItemClick”上放一個日志,但似乎沒有被調用。 你能幫我看看我做錯了什么嗎? 這是我的代碼

            package com.example.hk.sample;

            import java.io.IOException;
            import java.util.ArrayList;
            import java.util.HashMap;

            import org.apache.http.HttpResponse;
            import org.apache.http.HttpVersion;
            import org.apache.http.client.ClientProtocolException;
            import org.apache.http.client.HttpClient;
            import org.apache.http.client.methods.HttpPost;
            import org.apache.http.impl.client.DefaultHttpClient;
            import org.apache.http.params.BasicHttpParams;
            import org.apache.http.params.CoreProtocolPNames;
            import org.apache.http.params.HttpParams;
            import org.apache.http.util.EntityUtils;
            import org.json.JSONArray;
            import org.json.JSONException;
            import org.json.JSONObject;

            import android.app.Activity;
            import android.app.ProgressDialog;
            import android.content.Intent;
            import android.os.AsyncTask;
            import android.os.Bundle;
            import android.util.Log;
            import android.view.Menu;
            import android.view.View;
            import android.widget.AdapterView;
            import android.widget.Button;
            import android.widget.ListAdapter;
            import android.widget.ListView;
            import android.widget.SimpleAdapter;
            import android.widget.TextView;
            import android.widget.Toast;

            public class AllAccepted extends Activity {

                ListView list;
                TextView phone;
                TextView name;

                ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
                //URL to get JSON Array
                private static String url;
                //JSON Node Names
                private static final String TAG_OS = "myjson";
                private static final String TAG_VER = "phone";
                private static final String TAG_NAME = "name";
                JSONArray android = null;
                //String matchId = Singleton.getInstance().getMatchIdString();
                String matchId = "80";

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

                    list = (ListView) findViewById(R.id.listView1);

                    url = "http://demo.com/list.php?match_id="+ matchId; 

                    Log.e("final view", "final layout array list");
                    oslist = new ArrayList<HashMap<String, String>>(); 

                    new JSONParse().execute();

                    final Button btnHistory = (Button) findViewById(R.id.goHome);
                    btnHistory.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub    
                            Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(myIntent);

                        }
                    });   

                }

                protected void onItemClick(ListView l, View v, int position, long id) {
                    // TODO Auto-generated method stub
                    Log.e("item clicks", "selected: " + position);
                }


                private class JSONParse extends AsyncTask<String, String, JSONObject> {
                    private ProgressDialog pDialog;
                   @Override
                   protected void onPreExecute() {
                       super.onPreExecute();
                       phone = (TextView)findViewById(R.id.listPhone);
                       name = (TextView)findViewById(R.id.listName);
                       pDialog = new ProgressDialog(AllAccepted.this);
                       pDialog.setMessage("Getting Data ...");
                       pDialog.setIndeterminate(false);
                       pDialog.setCancelable(true);
                       pDialog.show();
                   }
                   @Override
                   protected JSONObject doInBackground(String... args) {
                       JSONParser jParser = new JSONParser();
                       // Getting JSON from URL
                       JSONObject json = jParser.getJSONFromUrl(url);
                       return json;
                   }

                    @Override
                    protected void onPostExecute(JSONObject json) {
                        pDialog.dismiss();
                        try {
                               // Getting JSON Array from URL
                               android = json.getJSONArray(TAG_OS);
                               for(int i = 0; i < android.length(); i++){
                               JSONObject c = android.getJSONObject(i);
                               // Storing  JSON item in a Variable
                               String ver = c.getString(TAG_VER);
                               String name = c.getString(TAG_NAME);
                               // Adding value HashMap key => value
                               HashMap<String, String> map = new HashMap<String, String>();
                               map.put(TAG_VER, ver);
                               map.put(TAG_NAME, name);
                               oslist.add(map);
                               //list=(ListView)findViewById(R.id.listView1);
                               ListAdapter adapter = new SimpleAdapter(AllAccepted.this, oslist,
                                       R.layout.all_accepted_layout,
                                       new String[] { TAG_VER,TAG_NAME }, new int[] {
                                               R.id.listPhone,R.id.listName});
                               list.setAdapter(adapter);

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

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

                @Override
                public void onBackPressed() {
                }

            }

提前致謝。

找到的解決方案:

將此添加到onCreate方法中

                    list = (ListView) findViewById(R.id.listView1);

                    list.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

                            Log.e("item clicks", "selected: " + position);
                            String item = list.getItemAtPosition(position).toString();

                            String contactNum = ((TextView)view.findViewById(R.id.listPhone)).getText().toString();

                            Log.e("phone number", contactNum);

                            Intent intent = new Intent(Intent.ACTION_CALL);
                            intent.setData(Uri.parse("tel:" + contactNum));
                            startActivity(intent);
                        }
                    });

感謝Kunu的幫助

在onCreate()上使用它

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long id) {
                Log.e("item clicks", "selected: " + position);
                String contactNum = ((TextView)view.findViewById(R.id.listPhone)).getText().toString();
                phoneCall(contactNum);
            }
});

並且不要忘記導入import android.widget.AdapterView.OnItemClickListener;

取回號碼后,嘗試撥打此號碼

public void phoneCall(String number)
{
            String phoneCallUri = "tel:"+number;
            Intent phoneCallIntent =new Intent(Intent.ACTION_CALL);
            phoneCallIntent.setData(Uri.parse(phoneCallUri));
            startActivity(phoneCallIntent);
}

設置itemClickListener使用:

listView.setOnItemClickListener(ActivityName.this);

覆蓋的方法,也是XML內設置focusableInTouchMode為false,也focusable為false,textviews

希望能有所幫助

在OnCreate list.setOnItemClickListener(new OnItemClickListener() {}); 然后添加取消實現方法

public class MainActivity extends Activity {
ListView lView ;
String[] ara={"a","b","c","d","e","f"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lView=(ListView)findViewById(R.id.listView1);
    ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ara);
    lView.setAdapter(adapter);
    lView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }});
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM