在选项卡式android程序中显示列表

时间:2015-07-22 09:11:11

标签: android android-listview android-viewpager

我有一个包含3个标签的程序,我无法在第二个插槽中正确显示它。我已经能够在其他选项卡下显示一些正常的东西,但列表中没有我。我已经让它独立工作,没有标签,所以如果有人能告诉我为什么它不会显示为可以帮助的基于标签的可跳转程序的一部分。

list_layout.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">

<TextView
    android:id="@+id/shiftid"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1" />

<TextView
    android:id="@+id/datetime"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="0.5" />

<TextView
    android:id="@+id/starttime"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1" />
<TextView
    android:id="@+id/endtime"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="1" />
</LinearLayout>

listmain_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FAA000">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

activity_main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" />

ListActivity.java //这样就可以将其识别为标签页

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.listmain_layout, container, false);
    return view;
}
}

ListViewAdapter.java //我在哪里建立hashmap

import static onyx.shiftcreator.Constants.FIRST_COLUMN;
import static onyx.shiftcreator.Constants.SECOND_COLUMN;
import static onyx.shiftcreator.Constants.THIRD_COLUMN;
import static onyx.shiftcreator.Constants.FOURTH_COLUMN;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.list_layout, null);

        txtFirst=(TextView) convertView.findViewById(R.id.shiftid);
        txtSecond=(TextView) convertView.findViewById(R.id.datetime);
        txtThird=(TextView) convertView.findViewById(R.id.starttime);
        txtFourth=(TextView) convertView.findViewById(R.id.endtime);

    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));
    txtThird.setText(map.get(THIRD_COLUMN));
    txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;
 }

 }

ListDisplay.java //我加载并显示Hashmap的内容

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class ListDisplay extends Activity {

private ArrayList<HashMap<String, String>> list;

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

    ListView listView=(ListView)findViewById(R.id.listView1);

    list=new ArrayList<HashMap<String,String>>();

    HashMap<String,String> temp=new HashMap<String, String>();
    temp.put(FIRST_COLUMN, "Ankit Karia");
    temp.put(SECOND_COLUMN, "Male");
    temp.put(THIRD_COLUMN, "22");
    temp.put(FOURTH_COLUMN, "Unmarried");
    list.add(temp);

    HashMap<String,String> temp2=new HashMap<String, String>();
    temp2.put(FIRST_COLUMN, "Rajat Ghai");
    temp2.put(SECOND_COLUMN, "Male");
    temp2.put(THIRD_COLUMN, "25");
    temp2.put(FOURTH_COLUMN, "Unmarried");
    list.add(temp2);

    HashMap<String,String> temp3=new HashMap<String, String>();
    temp3.put(FIRST_COLUMN, "Karina Kaif");
    temp3.put(SECOND_COLUMN, "Female");
    temp3.put(THIRD_COLUMN, "31");
    temp3.put(FOURTH_COLUMN, "Unmarried");
    list.add(temp3);

    ListViewAdapter adapter=new ListViewAdapter(this, list);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
            int pos=position+1;
            Toast.makeText(ListDisplay.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
        }

    });

 }
 }

MainActivity.java //我在选项卡下建立页面位置主要管理操作栏

 public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

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

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

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

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // 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.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}


@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);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * 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);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        if(position == 0)
            return new SyncActivity();
        if(position == 1)
            return new ListActivity();
        if(position == 2)
            return new EntryActivity();


        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

在你的片段中,你必须设置列表及其适配器,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.listview, container, false);
    lv = (ListView)rootView.findViewById(R.id.listview);
    adapter = new ListViewAdapter(getActivity(), R.layout.listviewlayout, new ArrayList<YourObject>());
    lv.setAdapter(adapter);
return  rootView;

您仍然需要填充列表。这可以通过AsyncTask正常完成。

答案 1 :(得分:0)

我还建议从Activity切换到ListActivity。这可以简化您的布局。确切地说,您不需要列表本身的任何布局。同样适用于Fragment。如果它只应显示列表使用ListFragment。