2017-03-06 49 views
0

我在創建具有不同選項卡的應用程序的過程中,幸運的是,我使用了Android Studio中的選項卡活動預設,並且爲三種不同的選項配置了3個片段標籤。但是,當我想顯示包含標題和圖像以及描述的列表視圖時,我發現我遇到了一些錯誤。如何在片段中顯示ListView Android Studio

嗯我想要的是顯示列表視圖,我有我的列表在Strings.xml文件中,我不知道該怎麼做才能使它工作。

這是我的片段Java文件:

package com.paradoxygo.guideforwwe2k; 

import android.content.Context; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

/** 
* Created by yugio on 04/03/2017. 
*/ 

public class MatchType extends Fragment { 

String [] theTitles; 
String [] theDesc; 
int [] images = {R.drawable.wide_cover_thumb}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

View rootView = inflater.inflate(R.layout.matchtype_tab, container, 
      false); 
ListView listView = (ListView) rootView.findViewById(R.id.Guide); 

Resources r = getResources(); 
theTitles = r.getStringArray(R.array.titles); 
theDesc = r.getStringArray(R.array.Desc); 

MyAdapter adapter = new MyAdapter(this, theTitles, images, theDesc); 
    listView.setAdapter(adapter); 





return rootView; 
} 

class MyAdapter extends ArrayAdapter <String> { 

public MyAdapter (MatchType c, String[] titles, int[] imgs, String[] Desc) { 

super(c, R.layout.single_row, R.id.text1, titles); 
    } 

} 
} 

這是我的MainActivity Java文件:

package com.paradoxygo.guideforwwe2k; 

import android.content.Context; 
import android.content.res.Resources; 
import android.support.design.widget.TabLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 

import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 



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


Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
// Create the adapter that will return a fragment for each of the three 
// primary sections of the activity. 
mSectionsPagerAdapter = new   SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

} 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    private String[] tabTitles = new String[]{"Guide", "Match Type", "Tricks"}; 
     public CharSequence getPageTitle(int position) { 
     return tabTitles[position]; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 

       GuideTab guideTab = new GuideTab(); 
       return guideTab; 
      case 1: 
       MatchType matchType = new MatchType(); 
       return matchType; 
      case 2: 
       Tricks tricks = new Tricks(); 
       return tricks; 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 


} 



} 
+0

您的適配器的代碼是wrog –

+0

ListView是舊的。你應該使用RecyclerView。 – BenjaminBihr

回答

0

當使用自定義ArrayAdapter你需要通過自己的模板,通過覆蓋渲染行getView(int position, View convertView, ViewGroup parent)方法。請參考this瞭解詳細示例。