1

我是android的新程序员。我想创建一个可点击的列表视图。我的意思是当用户点击一个项目时,它会进入一个新页面并开始一个新的活动。请帮我。提前致谢。这是我在 mainactivity.java 中的代码。

package first.app;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterViewFlipper;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

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

    setListAdapter(new MyAdapter(this,
    android.R.layout.simple_list_item_1,R.id.textView1, 
    getResources().getStringArray(R.array.games)));

}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list,parent, false);
        String[] items = getResources().getStringArray(R.array.games);

        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.textView1);

        tv.setText(items[position]);

        if (items[position].equals("فوتبال")){
            iv.setImageResource(R.drawable.football);
        }
        else if (items[position].equals("شنا")){
            iv.setImageResource(R.drawable.swimming);
        }
        else if (items[position].equals("شمشیربازی")){
            iv.setImageResource(R.drawable.fencing);
        }
        else if (items[position].equals("شطرنج")){
            iv.setImageResource(R.drawable.chess);
        }       
        return row;
    }


    }


}
4

2 回答 2

1

use Listview item click listener

getListView().setOnItemClickListener(new OnItemClickListener() {    
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        // your code is here on item click
    }
});
于 2013-06-11T07:08:11.767 回答
0
ListView lv = getListView();


        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
               String selval = ((TextView) view).getText().toString();

              Intent intnt = new Intent(getApplicationContext(), nextactivity.class);
                intnt.putExtra("selval ", selval);
              startActivity(i);

          }
        });
于 2013-06-11T07:11:10.283 回答