Issue
I am trying to open a website when I clicked a item from the list.For example when click item 'A' open www.google.com click 'b' open another website. How to do that? thanks.
public class MainActivity extends Activity {
private ListView lv;
///////////
///////////////////////
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Listview Data
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.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
}
});
}
AdapterView.OnItemClickListener adapterClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//要執行的事情
ListView lvMoogle5 = (ListView)findViewById(R.id.list_item);
lvMoogle5.setOnItemClickListener(adapterClick);
String[] links = getResources().getStringArray(R.array.link);
Uri uri = Uri.parse(links[position]);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
};
}
strings.xml
<resources>
<string name="app_name">Listview</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="link">
<item>http://finalfantasy.wikia.com/wiki/Sephiroth_(Crisis_Core_Boss)</item>
<item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
<item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
<item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
<item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
<item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
<item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
<item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
</string-array>
</resources>
I try this method from here but don't work. http://ithelp.ithome.com.tw/question/10106920
Solution
Try out this code:-
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object obj = lv.getAdapter().getItem(position);
Uri uri = Uri.parse(obj.toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Answered By - cool Boy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.