如何启动与ListView

时间:2017-02-20 23:38:07

标签: java android android-studio

我无法弄清楚如何从特定的列表视图项目启动特定网站。现在我让它按预期工作,但只启动google.com。我将如何编写此代码以启动3个网站的特定网址?任何帮助赞赏。谢谢。麻生注释了一个吐司,当点击一个特定的项目时,它会显示出来的效果很好。

MainActivity:

public class MainActivity extends AppCompatActivity {

//lets declare an array of icons that matches the order of descriptions
public static int[] images = {R.drawable.bbcnews, R.drawable.reddit, R.drawable.sciencemag};

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

    //lets get an array of descriptions from xml (string array resource)

    String[] descriptions = getResources().getStringArray(R.array.description_array);
    //in order to set up or listview we also need an ArrayAdapter
    //this Adaptor needs to use our custom_list layout.
    //Therefore we need to create a custom Arrayadaptor class

    //get a reference to our ListView so we can associate it with our custom ArrayAdapter
    ListView listView = (ListView) findViewById(R.id.listView);
    //create a new MyCustomAdapter
    MyCustomAdapter myCustomAdapter = new MyCustomAdapter(this, descriptions, images);
    //connect the ListView with myCustomAdapter
    listView.setAdapter(myCustomAdapter);


  }
}

适配器:

public class MyCustomAdapter extends BaseAdapter {
//lets declare some instance variables to hold essential data
String[] descriptions;
int [] images;
Context context;

//constructor to set up our instance variables
public MyCustomAdapter(MainActivity c, String[] d, int[] i){
    context = c;
    descriptions = d;
    images = i;
}

@Override
public int getCount() {
    return descriptions.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

//the android framework will call getView everytime it needs to render your listview
// int position indicates which row the framework is trying to draw
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row; // a reference to refer to each row
    //layoutInflator is a class that creates a java obj from the xml layout
    //we get a layoutINflator from the framework by calling getSystemService
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    //use the layoutInflater to create a new View of the correct type (custome_list)
    row = inflater.inflate(R.layout.custom_list, null);
    //get the textview and set its text
    TextView textView = (TextView) row.findViewById(R.id.textView);
    textView.setText(descriptions[position]);
    //get the img view and set its img icon
    ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
    imageView.setImageResource(images[position]);
    //we need to add an OnClickListener to respond to user clicks!
    row.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //Toast.makeText(context, descriptions[position], Toast.LENGTH_LONG).show();
            Uri uri = Uri.parse("http://www.google.com"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);





        }
    });
    return row;
  }
}

的strings.xml:

<resources>
<string name="app_name">My Fave Sites</string>
<string-array name="description_array">
    <item>Get your world news here</item>
    <item>Reddit is a sweet  forum</item>
 <item>Cool science stuff!</item>
</string-array>
<string-array name="links">
    <item>http://www.bbc.com/</item>
    <item>https://www.reddit.com/</item>
    <item>http://www.bbc.com/</item>
</string-array>

</resources>

2 个答案:

答案 0 :(得分:0)

你应该在你的onCreate方法中使用OnItemClickListener。

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
    });

答案 1 :(得分:0)

这样做。

    @Override
    public void onClick(View v) {
        String[] links = context.getResources().getStringArray(R.array.links);
        String url = links[position];
        Uri uri = Uri.parse(url); // missing 'http://' will cause crashed
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }

但有两点建议:

  1. 您可以考虑将String[] links = context.getResources().getStringArray(R.array.links);部分移动到您的适配器构造函数中,因为它不会在每次点击时发生变化。

  2. 您可以使用ListView.setOnItemClickListener来处理片段或活动中的click事件,因为适配器类只用于处理数据部分而不是逻辑。