1

我无法点击ListView。我没有收到 toast 消息。单击项目不起作用。

相关代码:

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

在此行之前的 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 回答
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 回答