2017-01-09 2 views
0

ここに私はserver.ifから来ている1リストビューを表示しています私はいくつかの警告ボックスを開きたいです...しかし、私は条件を満たす必要がありますeg.if listitem文字列 "Biancaピザ" alertbox else toast "こんにちは" ..ここで私のコードでは、リストビュー項目の文字列を取っていません。アンドロイドの「if else」条件での文字列値の照合方法は?

トースト(私が前に置いた条件であれば、正しいかどうかをチェックするコード)のリストビュー項目の第1の値ですが、私はそれが機能していないことを比較しています。

「ビアンカピザ」を食べるのが好きですが、「ビアンカピザ」と比較したいときは、一致する必要があります。

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> data; 
    ImageLoader imageLoader; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    public ListViewAdapter(Context context, 
          ArrayList<HashMap<String, String>> arraylist) { 
     this.context = context; 
     data = arraylist; 
     imageLoader = new ImageLoader(context); 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // Declare Variables 
     TextView id; 
     TextView name; 
     TextView population; 
     CircleImageView image; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View itemView = inflater.inflate(R.layout.list_item1, parent, false); 
     // Get the position 
     resultp = data.get(position); 

     // Locate the TextViews in listview_item.xml 
     // id = (TextView) itemView.findViewById(R.id.idq); 
     name = (TextView) itemView.findViewById(R.id.type1); 


     // Locate the ImageView in listview_item.xml 
     image = (CircleImageView) itemView.findViewById(R.id.subimg); 
     // icon = (ImageView) itemView.findViewById(R.id.arrow); 
     // Capture position and set results to the TextViews 
     // id.setText(resultp.get(SubMenu.RANK)); 
     name.setText(resultp.get(SubMenu.COUNTRY)); 

     // Capture position and set results to the ImageView 
     // Passes flag images URL into ImageLoader.class 
     imageLoader.DisplayImage(resultp.get(SubMenu.FLAG), image); 
     // imageLoader.DisplayImage(resultp.get(SubMenu.FLAG), icon); 
     // Capture ListView item click 
     /**itemView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
     // Get the position 
     resultp = data.get(position); 
     Intent intent = new Intent(context, SingleItemView.class); 
     // Pass all data rank 
     intent.putExtra("rank", resultp.get(MainActivity.RANK)); 
     // Pass all data country 
     intent.putExtra("country", resultp.get(MainActivity.COUNTRY)); 
     // Pass all data population 
     intent.putExtra("population",resultp.get(MainActivity.POPULATION)); 
     // Pass all data flag 
     intent.putExtra("flag", resultp.get(MainActivity.FLAG)); 
     // Start SingleItemView Class 
     context.startActivity(intent); 

     } 
     });*/ 
     return itemView; 
    } 
} 
+0

あなたが意味 – Pavya

+0

文字列内のスイッチでhttps://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.htmlを行い、それは大文字と小文字を区別すべきですか? –

+0

equalsIgnoreCaseの代わりにequalsを試してみてください..... –

答えて

1

問題はあなたがparentを使用していること:

public class SubMenu extends AppCompatActivity { 

    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String RANK = "id"; 
    static String COUNTRY = "name"; 
    private ProgressDialog pDialog; 
    String status=""; 
    static String FLAG = "image"; 
    Integer i = 1; 
    String _stringVal; 





    private static String url_create_book = "http://cloud....com/broccoli/creatinfo.php"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setContentView(R.layout.activity_sub_menu); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     String SelectedId = getIntent().getStringExtra("id"); 


     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


     // Get the view from listview_main.xml 

     // Execute DownloadJSON AsyncTask 
     new DownloadJSON().execute(); 
    } 

    // DownloadJSON AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener { 

     // @Override 
     // protected void onPreExecute() { 
     // super.onPreExecute(); 
     // Create a progressdialog 
     // mProgressDialog = new ProgressDialog(SubMenu.this); 
     // Set progressdialog title 
     // mProgressDialog.setTitle("Categories of Main categories....."); 
     // Set progressdialog message 
     // mProgressDialog.setMessage("Loading..."); 
     // mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     // mProgressDialog.show(); 
     // } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonarray = JsonFunctions 
        .getJSONfromURL("http://cloud....com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id")); 
      try { 
       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 
        jsonobject = jsonarray.getJSONObject(i); 
        map.put("name", jsonobject.getString("name")); 
        map.put("image", jsonobject.getString("image")); 
        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.list1); 
      // Pass the results into ListViewAdapter.java 
      adapter = new ListViewAdapter(SubMenu.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
      listview.setOnItemClickListener(this); 

     } 


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

      TextView name = (TextView) parent.findViewById(R.id.type1); 


      Toast.makeText(SubMenu.this, "this is "+name.getText(), Toast.LENGTH_SHORT).show(); 



      if (name.getText().toString().equalsIgnoreCase("Bian‌​‌​ca Pizza")) 
      { 
      //open alertbox 
      } 
      else{ 

       Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show(); 
} 

はここに私のリストビューアダプタです:

は、ここに私のコードです! parentはアダプタ全体のビューであり、 `R.id.type1 'を持つTextviewがある多くの行を含んでいます。 parentを使用すると、階層内にそのIDを持つ最初のアイテムが見つかります。それが常に最初のアイテムを返す理由です。

あなたはこれを行う必要があります:私はあなたのためにこれは便利だと思い

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

     // TextView name = (TextView) parent.findViewById(R.id.type1); WRONG 
     TextView name = (TextView) view.findViewById(R.id.type1); 
     Toast.makeText(SubMenu.this, "this is "+name.getText(), Toast.LENGTH_SHORT).show(); 
     if (name.getText().toString().equalsIgnoreCase("Bian‌​‌​ca Pizza")) 
     { 
     //open alertbox 
     } 
     else { 
      Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show(); 
     } 
    } 
+0

しかし、もし私がトースト私はアイテムの文字列を与える私の全体のオブジェクトの文字列を与えていない –

+0

私はトーストの名前は、その私に見えるandroid.support.v7.widget .......アプリケーション: id/type1} –

+0

これはリストビューでクリックすると表示されます –

0

を。

まず、アイテムをクリックしてからonitem click機能を呼び出す前にDownloadJSONクラスを閉じます。

private class DownloadJSON extends AsyncTask<Void, Void, Void> implements  
AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener { 

    // @Override 
    // protected void onPreExecute() { 
    // super.onPreExecute(); 
    // Create a progressdialog 
    // mProgressDialog = new ProgressDialog(SubMenu.this); 
    // Set progressdialog title 
    // mProgressDialog.setTitle("Categories of Main categories....."); 
    // Set progressdialog message 
    // mProgressDialog.setMessage("Loading..."); 
    // mProgressDialog.setIndeterminate(false); 
    // Show progressdialog 
    // mProgressDialog.show(); 
    // } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Create an array 
     arraylist = new ArrayList<HashMap<String, String>>(); 
     // Retrieve JSON Objects from the given URL address 
     jsonarray = JsonFunctions 
       .getJSONfromURL("http://cloud....com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id")); 
     try { 
      for (int i = 0; i < jsonarray.length(); i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       jsonobject = jsonarray.getJSONObject(i); 
       map.put("name", jsonobject.getString("name")); 
       map.put("image", jsonobject.getString("image")); 
       // Set the JSON Objects into the array 
       arraylist.add(map); 
      } 
     } catch (JSONException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void args) { 
     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.list1); 
     // Pass the results into ListViewAdapter.java 
     adapter = new ListViewAdapter(SubMenu.this, arraylist); 
     // Set the adapter to the ListView 
     listview.setAdapter(adapter); 
     listview.setOnItemClickListener(this); 

    } 
} 

if条件を書いてください。

if(name.getText().toString().equals("Bian‌​‌​ca Pizza")){ 

}else{ 

Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show(); 

} 

        or 

if(name.getText().toString().matches("Bian‌​‌​ca Pizza")){ 

}else{ 

Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show(); 

} 
+0

私はそれを閉じるとエラーが表示されません –

+0

私はエラーです。 –

+0

エラーはありません。チーズピザと一緒に試してみましたが、ビアンカピザではありません。 –

1

変更する必要があるのは、onItemClickの親の代わりにビューを使用することです。以下を参照してください - クリックしたアイテムテキストを直接取得します。

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

     TextView name = (TextView) view.findViewById(R.id.type1); 


     Toast.makeText(SubMenu.this, "this is "+name.getText(), Toast.LENGTH_SHORT).show(); 



     if (name.getText().toString().equals("Bian‌​‌​ca Pizza")) 
     { 
     //open alertbox 
     } 
     else{ 
      Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show(); 
    } 
+0

トーストで正しい値を示していますが、まだ条件が満たされていないと比較すると - –

+0

なぜその両方が同じでないのか、それ以外の条件になるのは –

+0

です。メソッド 'name.getText()。toString()。equals(" Bian ca Pizza ")' - 私は自分の答えを更新しました。 – Bethan

関連する問題