Android自定义listview意图如何让它工作?

时间:2017-06-01 09:08:58

标签: android xml android-layout listview

我制作了一个自定义listView和自定义数组适配器,包含1个图像和2个textviews。我想要工作的是当你点击列表视图中的一个项目时,它会打开一个有意图的新活动。

这是主要活动

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

    ListView listView = (ListView) findViewById(R.id.list);
    ArrayList<Subjects> obsclassiclist = new ArrayList<Subjects>();

    obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));
    obsclassiclist.add(new Subjects("?", "?", R.mipmap.ic_launcher));


    WordAdapter adapter = new WordAdapter(this, obsclassiclist);
    listView.setAdapter(adapter);

    LinearLayout listViewIntent = (LinearLayout) findViewById(R.id.intent);




    listViewIntent.setOnItemClickListener(new 
     AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
        position, long id) {
            if (position==0) {

                Intent tutorial = new Intent(view.getContext(), 
       Cl_01.class);

                // Start the new activity
                startActivityForResult(tutorial,0);
            }

            if (position==1) {

                Intent requirements = new Intent(view.getContext(), 
    CL_02.class);

                // Start the new activity
                startActivityForResult(requirements,1);
            }
        }
    });
}
}

这是自定义适配器

public WordAdapter(Activity context, ArrayList<Subjects> subjects) {
 // Here, we initialize the ArrayAdapter's internal storage for the context 
 and the list.
// the second argument is used when the ArrayAdapter is populating a single 
  TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the 
  adapter is not
// going to use this second argument, so it can be any value. Here, we used 
  0.
 super(context, 0, subjects);
 }

 /**
  * Provides a view for an AdapterView (ListView, GridView, etc.)
  *
 * @param position The position in the list of data that should be displayed 
   in the
   *                 list item view.
  * @param convertView The recycled view to populate.
 * @param parent The parent ViewGroup that is used for inflation.
 * @return The View for the position in the AdapterView.
  */
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
   // Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
    listItemView = LayoutInflater.from(getContext()).inflate(
            R.layout.list_item, parent, false);
}

// Get the {@link subject} object located at this position in the list
Subjects currentSubjects = getItem(position);


// Find the TextView in the list_item.xml layout with the ID subject
TextView subjectTextView = (TextView) 
listItemView.findViewById(R.id.subject);
// Get the version name from the current wordadapter object and
// set this text on the subject TextView
subjectTextView.setText(currentSubjects.getObsclassicSubject());


// Find the TextView in the list_item.xml layout with the ID description
TextView descriptionTextView = (TextView) 
listItemView.findViewById(R.id.description);
// Get the version number from the current wordadapter object and
// set this text on the description TextView
descriptionTextView.setText(currentSubjects.getObsclassicDescription());


// Find the Imageview in the list_item.xml layout with the ID version_number
ImageView pictureImageView = (ImageView) 
listItemView.findViewById(R.id.image);
// Get the version number from the current wordadapter and
// set this image on the imageview
pictureImageView.setImageResource(currentSubjects.getObsclassicimageid());





// Return the whole list item layout (containing 2 TextViews and an 
ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}

这是xml自定义列表视图

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
   <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/intent"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/image"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:id="@+id/subject"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingBottom="10dp"
        android:textSize="18sp"
        android:textStyle="italic"
        android:id="@+id/description"/>

    </LinearLayout>
    </LinearLayout>
    </ScrollView>

和主要活动中的xml

    <?xml version="1.0" encoding="utf-8"?>
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/list">
    </ListView>

如果有人有解决方案,请告诉我

1 个答案:

答案 0 :(得分:0)

你做错了,你在线性布局上制作clicklistener,而是在ListView

上做

更改此

listViewIntent.setOnItemClickListener(new 
     AdapterView.OnItemClickListener()

listView .setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
      // Now if you want to startActivity
      Intent tutorial = new Intent(MainActivity.this, 
   Cl_01.class);

            // Start the new activity
            startActivityForResult(tutorial,position);
    }
});