这里我显示1个listview来自server.if我点击任何项目我想要一些警告框打开...但为此我需要匹配条件eg.if listitem字符串匹配" Bianca比萨饼"然后打开alertbox其他吐司"你好" ..在我的代码中这里没有采取listview项目字符串。
奇怪的是它在toast中获取listview项的第一个值(我之前提出的代码,如果条件只是为了检查它是否正确。)但是当我比较它不工作时。
像吐司一样,我得到了#Bianca pizza"但是当我想将它与Bianca Pizza"它应匹配并进入if条件但它不会进入并显示其他条件。这是我的代码:
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("Bianca Pizza"))
{
//open alertbox
}
else{
Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show();
}
这是我的listview适配器:
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 :(得分:1)
问题是您使用了parent
! parent是整个适配器视图,它包含许多行,其中有Textview
,其id为“R.id.type1”。当您使用父级时,它会在其层次结构中找到具有该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("Bianca Pizza"))
{
//open alertbox
}
else {
Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:1)
您需要做的唯一更改是在onItemClick上使用view而不是parent,请参阅下面的内容 - 这将直接获取点击的项目文本。
@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("Bianca Pizza"))
{
//open alertbox
}
else{
Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
我认为这对您有帮助。
首先在项目点击之前关闭DownloadJSON类,然后调用onitem点击功能。
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( name.getText().toString().equals("Bianca Pizza")){
}else{
Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show();
}
or
if( name.getText().toString().matches("Bianca Pizza")){
}else{
Toast.makeText(SubMenu.this, "hello", Toast.LENGTH_SHORT).show();
}