1

をクリックできませんListView。トースト メッセージが表示されません。アイテムをクリックしても機能しません。

関連コード:

public class MainActivity5 extends ListActivity {

final Context context = this;
private ImageButton homebutton;
JSONArray customers = null;
private ProgressDialog pDialog;
private static String url_search_customer = "http://192.168.1.241/myapp/search.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_RESULT = "result";
private static final String TAG_ID = "customer_id";
private static final String TAG_F_NAME = "customer_fname";
private static final String TAG_C_PHONE = "customer_phone";
private static String phNumber;
private static String curActivity;
private static String ID;
private static TextView tvid;
JSONArray result = null;
myObject myObj = new myObject();
JSONParser jsonParser = new JSONParser();

EditText TextViewName;
EditText TextViewPhone;
private Button buttonNewMember;
private Button buttonSearchAgain;

SessionManager session;

static final ArrayList<HashMap<String, String>> customerList = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_list_view);

    Intent i = getIntent();
    phNumber = i.getStringExtra("phn_no");
    customerList.clear();
    new SearchCustomer().execute();

    ListView lv = this.getListView();
    OnItemClickListener listener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {
            Toast.makeText(getApplicationContext(), "Long Clicked " , Toast.LENGTH_SHORT).show();
        }
    };
    lv.setOnItemClickListener(listener);
}

class SearchCustomer extends AsyncTask<String, String, String> {
    private boolean successFlag;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity5.this);
        pDialog.setMessage("Searching Customer..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        String inputPhnNoText = phNumber;

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("phno", inputPhnNoText));
        JSONObject json = jsonParser.makeHttpRequest(url_search_customer,
                "POST", params);
        Log.d("Search Response", json.toString());
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    successFlag = true;
                    result = json.getJSONArray(TAG_RESULT);
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject c = result.getJSONObject(i);
                        String id = c.getString(TAG_ID);
                        String fname = c.getString(TAG_F_NAME);
                        String phoneNumber = c.getString(TAG_C_PHONE);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_ID, id);
                        map.put(TAG_F_NAME, fname);
                        map.put(TAG_C_PHONE, phoneNumber);
                        customerList.add(map);
                    }
                } else if (success == 0) {
                    successFlag = false;
                    Intent i = new Intent(getApplicationContext(),
                            MainActivity3.class);
                    i.putExtra("norecords",
                            "No record Found with phone number only ! Try again !!");
                    startActivity(i);
                    pDialog.dismiss();
                    finish();
                }
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
        return null;
    }

    protected void onPostExecute(String file_url) {
        if (successFlag = true) {
            pDialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(MainActivity5.this,
                    customerList, R.layout.activity_main5, new String[] {
                            TAG_F_NAME, TAG_C_PHONE, TAG_ID }, new int[] {
                            R.id.TextViewName, R.id.TextViewPhone, R.id.TextViewID });
            setListAdapter(adapter);
        } else if(successFlag = false) {

        }
    }
}

}

4

4 に答える 4

0

AsyncTask クラスでは動かないと思います。AsyncTask クラスで条件が失敗した場合、onCreate() メソッドに onItemClickListner を記述し、他のアクティビティを自動的に呼び出します。

コードが成功した場合は、そのアクティビティで ListView を表示できます...

于 2012-11-02T12:24:04.360 に答える
0

コードを次のように変更します。

     lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursorID= (Cursor)lv.getItemAtPosition(position); 
                String selectedID = lv.getString(1); 
                Intent i = new Intent(getApplicationContext(), Congratulations.class);
                i.putExtra("selectedID", selectedID);
                startActivity(i);
            }
        });
于 2012-11-02T11:40:00.447 に答える
0

ListActivity を使用する場合、このメソッドをオーバーライドしてクリック/取得します

onListItemClick(android.widget.ListView, android.view.View, int, long)

http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView、android.view.View、int、long )

于 2012-11-02T12:11:44.910 に答える
0

この行の前に onCreate() メソッドで以下のコードを記述しますnew SearchCustomer().execute();

ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String selectedID = TAG_ID;
        Intent i = new Intent(getApplicationContext(), Congratulations.class);
        i.putExtra("selectedID", selectedID);
        startActivity(i);
    }
});
于 2012-11-02T11:36:44.037 に答える