如何在Android中单击列表视图中的不同项目时制作不同的toast?

时间:2015-12-17 16:08:06

标签: android listview

当你试图在我的listview中搜索一个项目时,你好我的位置项目固定有问题。例如:当我在listview中点击一个项目(akim性能)时,我得到了一个字符串的toast(这个非常好;在position3),没有搜索界面;但是,当我使用相同项目的搜索界面时(我在另一个(position0)获得了不同的字符串吐司。我如何保持位置以及为每个人固定的字符串SAME的吐司!!

总之,我的目标是当我点击" akim表演"没有和有搜索界面,它应该给我吐司(这真的很好)!谢谢。帮帮我们

public class MainActivity extends AppCompatActivity{

private EditText editText;

private ArrayAdapter<String> listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event6icon);

editText = (EditText)findViewById(R.id.editCourseText);

ListView itemList = (ListView)findViewById(R.id.CoursesListView);

String [] listViewAdapterContent = {
        "rita performance",
        "ashely performance",
        "jenifer performance",
        "akim performance",
        "john performance",

    };

    listAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_list_item_2, android.R.id.text1, listViewAdapterContent);

    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
     public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
    String sMessage = "";
    switch(position) {
    case 0: sMessage = "This is exactly what should be done "; break;
    case 1: sMessage = "You are completely wrong! "; break;
    case 2: sMessage = "This is good "; break;
    case 3: sMessage = "This is really good "; break;
    case 4: sMessage = "This is perfect "; break;
   }
    Toast.makeText(getApplicationContext(), sMessage+position,    Toast.LENGTH_LONG).show();
}
        });

   editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence c, int start, int count,  int  after) {
    }

    @Override
    public void onTextChanged(CharSequence c, int start, int before, int count) {
        MainActivity.this.listAdapter.getFilter().filter(c);
    }

    @Override
    public void afterTextChanged(Editable c) {
    }
});
 }


 }

1 个答案:

答案 0 :(得分:0)

您可以在代码中使用简单的随机选择器逻辑;

 itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                if (Math.random() > 0.5) {

                Toast.makeText(getApplicationContext(), "This is exactly what should be done "+position,    Toast.LENGTH_LONG).show();
    }
                else {
                Toast.makeText(getApplicationContext(), "You are completely wrong! "+position, Toast.LENGTH_LONG).show();
    }

            }
        });