我正在尝试在特定选项卡中向ListView添加项目(当按下按钮时)。我希望将项目添加到用户当前所在的选项卡中。仅供参考:我正在使用Android Studio的默认ActionBar选项卡片段模板。另外,如果有任何不同,我正在使用自定义适配器。
我能够将list和item添加到listview,但它不一致,并不总是有效。有时,当我单击按钮时,没有任何反应,其他时候,我的新项目出现在另一个页面/选项卡上。此外,在标签之间来回滚动一点后,显示的项目会突然消失。
任何帮助都将不胜感激。
这是碎片:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
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.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.supercilex.common.ListViewAdapter;
import java.util.ArrayList;
import static com.supercilex.robotscouter.tempNewForm.PlaceholderFragment.addItemToList;
public class tempNewForm extends AppCompatActivity
{
private static ArrayList< String > textViewValues;
private static ArrayList< String > checkBoxValues;
private static ListViewAdapter listViewAdapter;
@Override
protected void onCreate ( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_temp_new_form );
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.
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager() );
// Set up the ViewPager with the sections adapter.
ViewPager 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 )
{
addItemToList(
"test",
"test2"
);
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_temp_new_form,
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 placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment ()
{
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance ( int sectionNumber )
{
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(
ARG_SECTION_NUMBER,
sectionNumber
);
fragment.setArguments( args );
return fragment;
}
public static void addItemToList (
String textViewValue,
String checkboxValue
)
{
textViewValues.add( textViewValue );
checkBoxValues.add( checkboxValue );
listViewAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView (
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
)
{
View rootView = inflater.inflate(
R.layout.fragment_temp_new_form,
container,
false
);
textViewValues = new ArrayList<>();
checkBoxValues = new ArrayList<>();
ListView listView = ( ListView ) rootView.findViewById( R.id.listView3 );
rootView.findViewById( R.id.fragment_temp_new_form_layout );
switch ( getArguments().getInt( ARG_SECTION_NUMBER ) )
{
case 1:
textViewValues.add( "Android List View from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
checkBoxValues.add( "first from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
break;
case 2:
textViewValues.add( "Android List View from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
checkBoxValues.add( "first from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
break;
case 3:
textViewValues.add( "Android List View from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
checkBoxValues.add( "first from " + getArguments().getInt( ARG_SECTION_NUMBER ) );
break;
default:
break;
}
listViewAdapter = new ListViewAdapter(
getContext(),
textViewValues,
checkBoxValues
);
listView.setAdapter( listViewAdapter );
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public static 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 );
}
@Override
public int getCount ()
{
// Show x total pages.
return 3;
}
@Nullable
@Override
public CharSequence getPageTitle ( int position )
{
switch ( position )
{
case 0:
return "TAB 1";
case 1:
return "TAB 2";
case 2:
return "TAB 3";
}
return null;
}
}
}
这是我的自定义适配器:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.supercilex.robotscouter.R;
import com.supercilex.robotscouter.tempNewForm;
import java.util.ArrayList;
public class ListViewAdapter extends BaseAdapter
{
private final ArrayList< String > textViewValues;
private final ArrayList< String > checkBoxValues;
private final Context context;
public ListViewAdapter (
Context context,
ArrayList< String > textViewValues,
ArrayList< String > checkBoxValues
)
{
this.textViewValues = textViewValues;
this.checkBoxValues = checkBoxValues;
this.context = context;
correctArrays();
}
@Override
public int getCount ()
{
if ( ( textViewValues != null || textViewValues.size() != 0 ) || ( checkBoxValues != null || checkBoxValues.size() != 0 ) )
{
if ( textViewValues.size() >= checkBoxValues.size() )
{
return textViewValues.size();
}
else
{
return checkBoxValues.size();
}
}
return 0;
}
@Override
public Object getItem ( int position )
{
return position;
}
@Override
public long getItemId ( int position )
{
return position;
}
@SuppressLint ( "InflateParams" )
@Override
public View getView (
final int position,
View convertView,
ViewGroup parent
)
{
View convertView1 = convertView;
if ( convertView1 == null )
{
LayoutInflater layoutInflater;
layoutInflater = LayoutInflater.from( context.getApplicationContext() );
convertView1 = layoutInflater.inflate(
R.layout.list_view_layout,
null
);
}
TextView textView = ( TextView ) convertView1.findViewById( R.id.rowTextView );
CheckBox checkBox = ( CheckBox ) convertView1.findViewById( R.id.listViewCheckBox );
if ( textViewValues.get( position )
.equals( "null" ) )
{
textView.setVisibility( View.GONE );
}
else
{
textView.setVisibility( View.VISIBLE );
textView.setText( textViewValues.get( position ) );
}
if ( checkBoxValues.get( position )
.equals( "null" ) )
{
checkBox.setVisibility( View.GONE );
}
else
{
checkBox.setVisibility( View.VISIBLE );
checkBox.setText( checkBoxValues.get( position ) );
}
convertView1.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick ( View view )
{
if ( context.getClass()
.toString()
.equals( "class com.supercilex.robotscouter.MainActivity" ) )
{
Intent intent = new Intent(
view.getContext(),
tempNewForm.class
);
context.startActivity( intent );
}
// Show Alert
Snackbar.make(
view,
"Position: " + position + " ListItem: " + textViewValues.get( position ),
Snackbar.LENGTH_LONG
)
.setAction(
"Action",
null
)
.show();
}
} );
return convertView1;
}
private void correctArrays ()
{
if ( textViewValues.size() > checkBoxValues.size() )
{
for (
int i = checkBoxValues.size();
i < textViewValues.size();
i++
)
{
checkBoxValues.add(
i,
"null"
);
}
}
else if ( textViewValues.size() < checkBoxValues.size() )
{
for (
int i = textViewValues.size();
i < checkBoxValues.size();
i++
)
{
textViewValues.add(
i,
"null"
);
}
}
}
}