在我的第二个Activity中,我放了一个ListView 通过点击任何项目,它会带您进入多个活动 我在它上面添加了一个搜索选项,因为它是一个巨大的选项 当我搜索获得一个项目时,它会转到上一个原始位置。
public class Main2Activity extends AppCompatActivity {
ListView lv;
EditText edtsearch;
//SearchView sv;
ArrayAdapter <String> adapter;
ArrayList<HashMap<String, String>> pcs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
lv = (ListView) findViewById(R.id.Lvid);
edtsearch = (EditText) findViewById(R.id.bbb);
final String pcs [] = {"BARURA\n(EB-0851) COMILLA","DAUDKANDI\n(EB-0079) COMILLA","COMPANIGONJ\n(EB-0087) COMILLA","GOURIPUR BAZAR \n(EB-0088) COMILLA","RAMCHANDRAPUR\n(EB-0112) COMILLA","CHANDINA SAMABAY\n(EB-0131) COMILLA","DHARMAPUR\n(EB-0200) COMILLA","SASHANGACHA\n(EB-0316) COMILLA","BATAKANDI\n(EB-0420) COMILLA","DEBIDWAR\n(EB-0853) COMILLA","CHOUDDAGRAM\n(EB-0859) COMILLA","HOMNA\n(EB-0891) COMILLA","COMILLA CO-OPERATIVE\n(JS-0026),COMILLA","LAKSHAM\n(JS-0117),COMILLA","BURICHANG\n(JS-0185),COMILLA","GUNABATI\n(JS-0260),COMILLA","RAMKRISNAPUR\n(JS-0308),COMILLA","JAFARGONJ\n(JS-0365),COMILLA","NALGHAR BAZAR\n(JS-0438),COMILLA","GANDAMATI BAZAR\n(JS-0479),COMILLA","PAYERKHOLA\n(JS-0507),COMILLA","AK FAZLUL HOQ ROAD CORP.\n(JS-0515),COMILLA","MOHANPUR BAZAR\n(JS-0560),COMILLA","SHASHIDAL BAZAR\n(JS-0594),COMILLA","FAKIRBAZAR\n(JS-0595),COMILLA","NANGOLKOT\n(JS-0601),COMILLA","BULAIN BAZAR\n(JS-0603),COMILLA","ALKARA\n(JS-0608),COMILLA","CHAWK BAZAR\n(JS-0634),COMILLA","SUNDALPUR\n(JS-0638),COMILLA","KAMALLA\n(JS-0643),COMILLA","BAIRA\n(JS-0687),COMILLA","DULALPUR BAZAR\n(JS-0696),COMILLA","BHOWKSHER BAZAR\n(JS-0721),COMILLA","KANDIR PARH\n(JS-0731),COMILLA","SHAHEBABAD BAZAR\n(JB-0836),COMILLA","COMILLA CADET COLLEGE\n(JB-0839),COMILLA","COMILLA EPZ\n(JB-0909),COMILLA","BHARASHAR BAZAR\n(JB-0941),COMILLA","COMILLA UNIVERSITY\n(JB-0945),COMILLA","ALAHABAD BAZAR\n(JB-0966),COMILLA","SUAGONJ BAZAR\n(JB-0968),COMILLA"};
adapter = new ArrayAdapter<String>(this, R.layout.layout, R.id.txtid, pcs);
lv.setAdapter(adapter);
edtsearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Main2Activity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String text= lv.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "CLICKED ON " + pcs[position], Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
Intent firstIntent = new Intent(Main2Activity.this,
Barura.class);
startActivity(firstIntent);
break;
case 1:
Intent secondIntent = new Intent(Main2Activity.this,
Daukandi.class);
startActivity(secondIntent);
break;
case 2:
Intent thirdIntent = new Intent(Main2Activity.this,
COMPANIGONJ.class);
startActivity(thirdIntent);
break;
case 3:
Intent fourthIntent = new Intent(Main2Activity.this,
GOURIPUR.class);
startActivity(fourthIntent);
break;
}
}
});
}
}
答案 0 :(得分:0)
而不是使用switch (position)
使用其他一些唯一值代替位置来检查点击事件。因此,检查您单击的列表项会很容易。
答案 1 :(得分:0)
如果我理解正确,你根本就不想换班。
您可以测试列表视图中位置的字符串的值,而不是位置本身。
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
final String text= lv.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "CLICKED ON " + text, Toast.LENGTH_SHORT).show();
Class clz = null;
if (text.startsWith("BARURA")) {
clz = Barura.class;
} // else if () ...
if (clz != null) {
Intent i = new Intent(Main2Activity.this, clz);
startActivity(i);
}
请注意,拥有超过20个活动类意味着您应该重新构建某些内容(个人意见)