2017-06-01 110 views
1

我做了一個自定義listView和自定義陣列適配器與1圖像和2 textview。我想要工作的是當你點擊列表視圖中的一個項目時,它打開一個有意圖的新活動。Android自定義列表視圖意圖如何讓它工作?

這是在MainActivity

@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> 

如果有人有一個解決方案請讓我知道

回答

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); 
    } 
}); 
+0

我是新的stackoverflow我需要更改arg0,arg1,arg 3值 – anomynous

+0

@anomynous,不,你不','位置'將有項目點擊列表視圖的索引,你只需要使用 –

+0

http://prntscr.com/fgfr79 – anomynous