0

我应该如何单击列表视图上的列表,我应该将我的 onclick 侦听器放在哪里?我应该如何单击列表视图上的列表,我应该将我的 onclick 侦听器放在哪里?

当我单击它时,我想显示一个对话框..

我的Java代码:

public class SearchlistviewActivity extends Activity {
/** Called when the activity is first created. */
private ListView lv1;


    private ArrayList<String> arr_sort= new ArrayList<String>();
    int textlength=0;
    private ArrayList<String> map= new ArrayList<String>();
    //private  ArrayList<HashMap<String, String>> listItem = new     ArrayList<HashMap<String>>();
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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


    getServerData(strURL);

    lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , map));



}


public static final String strURL = "http://192.168.0.2/ville.php";

private String getServerData(String returnString ) {
    InputStream is = null;
    String result = "";
    // Envoyer la requête au script PHP.
    // Script PHP : $sql=mysql_query("select * from tblVille where Nom_ville like '".$_REQUEST['ville']."%'");
    // $_REQUEST['ville'] sera remplacé par L dans notre exemple.
    // Ce qui veut dire que la requête enverra les villes commençant par la lettre L
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("ville",""));

    // Envoie de la commande http
    try{
        HttpClient httpclient = new DefaultHttpClient();
        String http="http://" ;
        String myip ="192.168.0.2" ;
        String vil="/ville.php";
        String myurl=http+myip+vil;

        HttpPost httppost = new HttpPost(myurl);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // Convertion de la requête en string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    // Parse les données JSON
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            // Affichage ID_ville et Nom_ville dans le LogCat
            Log.i("log_tag","CostumerOrder: "+json_data.getString("CostumerOrder")+
                    ", OrderPrice: "+json_data.getString("OrderPrice")
            );
            // Résultats de la requête

            // ici commence l ajout des champs du base de donnees
          //  map = new ArrayList<String>();
           // on ajoute champ ID_ville apres la conversion int to string 
            map.add(json_data.getString("CostumerOrder").toString());



            // on ajoute champ Nom_ville  apres la conversion int to string 


            // ajout ds la liste view 
        //    listItem.add(map);

        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return returnString; 
}
}
4

2 回答 2

0

尝试这个

 lv1.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView arg0, View v, int position,
 long id) {
 selected = position; //get the Item
 AlertDialog.Builder alert=new AlertDialog.Builder(context); 
         alert.setTitle("Title ");
         alert.setMessage("Message); 
         alert.setPositiveButton("ok", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {                    
             }
         });
         alert.setNegativeButton("Cancel", null); 
         alert.show();
}
});
于 2013-07-16T05:30:23.607 回答
0
listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapter1, View view,
            int position, long arg3) {


        Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();

    }

});

在发布此类问题之前,请尝试使用谷歌搜索。

于 2013-07-16T05:19:15.767 回答