Android ListView适配器无法正确显示项目

时间:2017-08-21 09:20:15

标签: android

下面是我的listview适配器,当我点击一个项目跳转到下一个活动并返回之后,它不会显示这些项目。数据从数据库接收。

public class CropSelectionActivity extends Activity implements
        android.widget.AdapterView.OnItemClickListener {

    private static final String TAG = CropSelectionActivity.class.getName();
    private static CropsAdapter listAdapter;
    private static ListView listView;
    public static String selectedCrop = null;
    private static PendingIntent pendingIntent;
    private static AlarmManager alarmManager;
    private static PendingIntent pendingIntent1;
    private static AlarmManager alarmManager1;
    private static final long SYNCALARAMINTERVEL = 15 * 60 * 1000;
    private static final long DELETIONINTERVEL = 15 * 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.displaypoints);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Log.i(TAG, "----onCreate()-----");
        fireAlarm();
        final String details[] = DALAccessLayer.getUserMailAndId(CropSelectionActivity.this);
        ActionBar actionbar = getActionBar();
        actionbar.setTitle("Esap Expert"+"("+details[1]+")");
        actionbar.setHomeButtonEnabled(true);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()   // or .detectAll() for all detectable problems
                .penaltyLog()
                .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .penaltyLog()
                .penaltyDeath()
                .build());
        listView = (ListView) findViewById(R.id.list);
        listView.setOnItemClickListener(this);
        listAdapter = new CropsAdapter(this,R.layout.listview);
    }
    @Override
    protected void onPostResume() {
        super.onPostResume();

        Map<String, Integer> list = DALAccessLayer.getCrops(getApplicationContext(), DatabaseConstantsString.status_unresolve.getValue(), "0");
        int pointsCount = 0;
        for (String key : list.keySet()) {
            CropPojo cp = new CropPojo();
            cp.setCropName(key);
            pointsCount = pointsCount+list.get(key);
            Log.i(TAG, "pointsCount ="+pointsCount);
            cp.setCount(list.get(key));
            listAdapter.add(cp);
        }
        listView.setAdapter(listAdapter);
        Log.i(TAG, "number of existing points ="+pointsCount);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        listAdapter.clear();
        listView.setAdapter(listAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        CropPojo cp = (CropPojo) listView.getItemAtPosition(position);
        selectedCrop = cp.getCropName();
        Intent intent = new Intent(this, DisplayPoints.class);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
    }


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

        switch (item.getItemId()){

            case R.id.logout:
                Intent i = new Intent(getApplicationContext(),MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                return true;

            case R.id.refresh:
                startService(new Intent(getApplicationContext(), MyService.class));
                return true;

            case R.id.getexpertporfile:
                CommunicationHandler.getExpertProfile(getApplicationContext());
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

&#13;
&#13;
public class CropsAdapter extends ArrayAdapter<CropPojo>{

	private int textViewResourceId;
	
	public CropsAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		this.textViewResourceId = textViewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View view = getWorkingView(convertView);
		final ViewHolder viewHolder = getViewHolder(view);
		final CropPojo cropPojo = getItem(position);
		viewHolder.titleView.setText(cropPojo.getCropName());
		viewHolder.locationView.setText(String.valueOf(cropPojo.getCount()));
		return view;
	}

	private View getWorkingView(final View convertView) {
		// The workingView is basically just the convertView re-used if possible
		// or inflated new if not possible
		View workingView = null;
		
		if(null == convertView) {
			final Context context = getContext();
			final LayoutInflater inflater = (LayoutInflater)context.getSystemService
		      (Context.LAYOUT_INFLATER_SERVICE);
			
			workingView = inflater.inflate(textViewResourceId, null);
		} else {
			workingView = convertView;
		}
		return workingView;
	}
	
	private ViewHolder getViewHolder(final View workingView) {
		// The viewHolder allows us to avoid re-looking up view references
		// Since views are recycled, these references will never change
		final Object tag = workingView.getTag();
		ViewHolder viewHolder = null;
		
		
		if(null == tag || !(tag instanceof ViewHolder)) {
			viewHolder = new ViewHolder();
			
			viewHolder.titleView = (TextView) workingView.findViewById(R.id.firstLine);
			viewHolder.locationView = (TextView) workingView.findViewById(R.id.location);
			
			workingView.setTag(viewHolder);
			
		} else {
			viewHolder = (ViewHolder) tag;
		}
		
		return viewHolder;
	}
	
	/**
	 * ViewHolder allows us to avoid re-looking up view references
	 * Since views are recycled, these references will never change
	 */
	private static class ViewHolder {
		public TextView titleView;
		public TextView locationView;
	}

	
}
&#13;
&#13;
&#13;

0 个答案:

没有答案