开发者

Modifying this List Linstners (custom adapter)

Can someone please tell me how to create a custom adpater for this list, as i dont want toast to display when a user clicks the list item, but, When a user clicks on Google, he will be navigated to "www.google.com" and "www.yahoo.com" and same for msn.com,, cant figure out at all, stuck for last 1 week, even though i know how to create a intent and call a URI but not working or right for this, can someone just modify this please ?

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FurtherEducationCourses extends ListActivity  

{
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, FURTHER_EDUCATION));
      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });

    }



     static final String[] FURTHER_EDUCATION = new String[] {
         "GOOGLE", "YAHOO", "MSN"

}; }

xml file, dunno why u requ开发者_开发问答ired :s

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>


The trick is to get the item at that position, then depends on the position (or even the value at the position), you call the correct link.

So you need to index the value like this, put this code after your static final... statement.

HashMap<String, String> valueToLink = new HashMap<String, String>;// key is Google, Yahoo, value is www.google.com 

valueToLink.put("GOOGLE", "www.google.com");
//add yahoo,.etc.

In onItemClick() function, replace the toast by this:

String link = valueToLink.get(((TextView) view).getText());
//code to open the link here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜