2015-02-11 59 views
0

我是新來的listView和搜索很多教程,但很困惑,因爲教程教導不同的風格和如此複雜。基本上我想創建一個listView與圖像和文本,當我按下收藏夾選項卡。使用Fragment創建簡單的listView Android

我唯一混淆的是FavouriteFragment.java的編碼,因爲我不確定它是否正確。 listView沒有出現。希望有人能幫助我。

這是我activity_favourite_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#5ba4e5" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

這是我single_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="90px" 
    android:layout_height="60dp" 
    android:layout_marginLeft="5px" 
    android:layout_marginRight="30px" 
    android:layout_marginTop="5px" 
    android:layout_weight="0.68" 
    android:src="@drawable/ic_launcher" > 

</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:layout_gravity="center" 
    android:text="@+id/label" 
    android:textSize="30px" /> 

</LinearLayout>  

MainActivity.java

import android.app.ActionBar; 
import android.content.Intent; 
import android.app.ActionBar.Tab; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.Menu; 
import com.example.sgrecipe.TabsAdapter; 

public class MainActivity extends FragmentActivity implements 
    ActionBar.TabListener { 

private ViewPager viewPager; 
private TabsAdapter mAdapter; 
private ActionBar actionBar; 
// Tab titles 
private String[] tabs = { "Recipes", "Favourites", "Quiz" }; 

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

    // Initilization 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getActionBar(); 
    mAdapter = new TabsAdapter(getSupportFragmentManager()); 

    viewPager.setAdapter(mAdapter); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Adding Tabs 
    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 

    /** 
    * on swiping the viewpager make respective tab selected 
    * */ 
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      // on changing the page 
      // make respected tab selected 
      actionBar.setSelectedNavigationItem(position); 
     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 
     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
     } 
    }); 
} 

@Override 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
} 

@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
    // on tab selected 
    // show respected fragment view 
    viewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
} 

// insert option menu 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

FavouriteFragment.java

import com.example.sgrecipe.MobileArrayAdapter; 
import android.app.Fragment; 
import android.app.ListActivity; 
import android.app.ListFragment; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.text.Layout; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FavouriteFragment extends Fragment{ 

String[] Recipe = new String[] { "Chinese Food", "Malay Food", "Indian  Food", "Others"}; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_favourite_fragment, null); 

    MobileArrayAdapter adapter = new MobileArrayAdapter(this.getActivity(), Recipe); 
    ListView listView = (ListView) getActivity().findViewById(R.id.listview); 

    listView.setAdapter(adapter); 

    return root; 
} 
} 

MobileArrayAdapter.java

import com.example.sgrecipe.R; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MobileArrayAdapter extends ArrayAdapter<String> { 
private final Context context; 
private final String[] values; 

public MobileArrayAdapter(Context context, String[] values) { 
    super(context, R.layout.single_row, values); 
    this.context = context; 
    this.values = values; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.single_row, 
      parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    textView.setText(values[position]); 
    // Change icon based on name 
    String s = values[position]; 
    System.out.println(s); 
    if (s.equals("Chinese Food")) { 
     imageView.setImageResource(R.drawable.chinese); 
    } else if (s.equals("Malay Food")) { 
     imageView.setImageResource(R.drawable.malay); 
    } else if (s.equals("Indian Food")) { 
     imageView.setImageResource(R.drawable.indian); 
    } else { 
     imageView.setImageResource(R.drawable.others); 
    } 
    return rowView; 
} 
} 
+1

你能也粘貼MobileArrayAdapter – Psypher 2015-02-11 16:07:38

+0

的只是上載的mobileArrayAdapter.java – 2015-02-12 03:33:59

回答

3

你的問題是你如何檢索ListView,只需修改這一行:

ListView listView = (ListView) getActivity().findViewById(R.id.listview); 

的樣子這個:

ListView listView = (ListView) root.findViewById(R.id.listview); 

列表視圖是片段的根視圖的一部分,因此需要在該視圖中被發現,而不是在你的MainActivity

+0

代碼但是TabsAdapter還存在另一個問題。 case 1: return new FavouriteFragment();它表示該類型無法從FavouriteFragment轉換爲Fragment – 2015-02-12 03:34:56

+0

FavouriteFragment需要擴展android.support.v4.app.Fragment – 2015-02-12 14:20:42